Data Types in C
Data types in C define the type of data that a variable can hold. They specify the size, range, and format of the variable. Understanding data types is essential for effective memory utilization and ensuring that the operations performed on variables are valid.
Categories of Data Types in C
1. Primary (Built-in) Data Types
These are predefined by the C language.
Data Type Description Size (Typical) Range (Typical)
int Integer type 4 bytes -2,147,483,648 to 2,147,483,647
float Floating-point 4 bytes ±3.4E-38 to ±3.4E+38
double Double precision floating-point 8 bytes ±1.7E-308 to ±1.7E+308
char Character type 1 byte -128 to 127 (or 0 to 255)
void No value 0 bytes N/A
2. Derived Data Types
Derived from primary data types.
Data Type Description
array Collection of elements of the same type.
pointer Stores the memory address of another variable.
structure Combines variables of different types into one.
union Similar to structures but shares memory for all members.
3. Enumeration (enum)
Used to define a set of named integral constants.
Example:
c
Copy
Edit
enum Days { Sunday, Monday, Tuesday };
4. Type Modifiers
Modifiers alter the size and range of basic data types.
Modifier Effect
short Reduces the size of an integer.
long Increases the size of an integer.
signed Allows both positive and negative values.
unsigned Allows only non-negative values.
Size and Range Examples
Type Size (bytes) Range
short int 2 -32,768 to 32,767
unsigned short int 2 0 to 65,535
long int 8 -2^63 to 2^63 – 1
unsigned long int 8 0 to 2^64 – 1
long double 16 ±3.4E-4932 to ±1.1E+4932
Special Considerations
Type Casting: Converting one data type to another.
Example:
c
Copy
Edit
float a = 5.5;
int b = (int)a; // b becomes 5
Size Determination: The sizeof operator determines the size of a data type.
Example:
c
Copy
Edit
printf(“%zu”, sizeof(int)); // Outputs the size of int
Conclusion
Data types in C are crucial for defining variables, managing memory, and ensuring proper operations. Choosing the appropriate data type improves program efficiency and reliability.