In C programming, each variable is associated with a specific data type that defines the kind of data it can store, such as integers, characters, floating-point numbers, doubles, and more. Each data type requires varying amounts of memory and supports specific operations tailored to its characteristics. Understanding and utilizing the right data types is crucial for efficient programming.
There are the following data types in C language.
Types | Description |
---|---|
Primitive Data Types | Primitive data types are the most basic data types that are used for representing simple values such as integers, float, characters, etc. |
User Defined Data Types | The user-defined data types are defined by the user himself. |
User Defined Data Types | The data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. |
geeksforgeeks |
Primitive Data Types in C Language:
In C programming, primitive data types play a fundamental role in defining variables. These data types include integers (int), characters (char), floating-point numbers (float), and double-precision floating-point numbers (double). Each primitive data type has specific characteristics, such as memory requirements and allowable operations, making them essential building blocks for any C program.
User-Defined Data Types in C:
Beyond the built-in primitive data types, C allows developers to create their own data types, known as user-defined data types. These types enable programmers to encapsulate related variables under a single name, enhancing code organization and readability. Examples of user-defined data types include structures and enums. Leveraging these types allows for the creation of more complex and meaningful structures in your programs.
Derived Data Types in C Programming:
Derived data types in C are formed by combining primitive or user-defined data types. Two prominent examples are arrays and pointers. Arrays enable the grouping of elements of the same data type, providing a convenient way to manage related data. Pointers, on the other hand, store memory addresses, offering a powerful mechanism for dynamic memory allocation and manipulation.
Different data types also have different ranges
byjusexamprep |
Data type in C, including their range, size, format specifier, and syntax:
1. Integer Data Type (int):
Range: -32,768 to 32,767 (for a standard int).
Size: 4 bytes.
Format Specifier: %d in printf and scanf.
Example:
#include <stdio.h> int main() { int integerVariable = 42; printf("Integer Variable: %d\n", integerVariable); return 0; } |
2. Character Data Type (char):
Range: -128 to 127 (or 0 to 255 for an unsigned char).
Size: 1 byte.
Format Specifier: %c in printf and scanf.
Example:
#include <stdio.h> int main() { char charVariable = 'A'; printf("Character Variable: %c\n", charVariable); return 0; } |
3. Floating-Point Data Type (float):
Range: Approximately 3.4E-38 to 3.4E+38.
Size: 4 bytes.
Format Specifier: %f in printf and scanf.
Example:
#include <stdio.h> int main() { float floatVariable = 3.14; printf("Float Variable: %f\n", floatVariable); return 0; } |
4. Double Precision Floating-Point Data Type (double):
Range: Approximately 1.7E-308 to 1.7E+308.
Size: 8 bytes.
Format Specifier: %lf in printf and scanf.
Example:
#include <stdio.h> int main() { double doubleVariable = 2.71828; printf("Double Variable: %lf\n", doubleVariable); return 0; } |
These examples demonstrate the declaration and usage of variables for each primitive data type in C, along with appropriate format specifiers for input and output.