Data types in C are fundamental building blocks that define the type of data a variable can hold and the operations that can be performed on it. They play a crucial role in memory allocation, value representation, and code execution.
Here are the primary data types in C:
1. Basic Data Types:
- int: Stores whole numbers (integers) within a specific range (typically -2147483648 to 2147483647).
- float: Stores single-precision floating-point numbers (numbers with decimals).
- double: Stores double-precision floating-point numbers (more precise than floats).
- char: Stores a single character (letters, digits, symbols).
2. Modifiers:
- short: Reduces size of int and long (e.g., short int, short long).
- long: Increases size of int and double (e.g., long int, long double).
- signed: Can represent both positive and negative values (default for int and char).
- unsigned: Can only represent non-negative values (0 and positive numbers).
3. Derived Data Types:
- Arrays: Collections of elements of the same data type, accessed using an index.
- Pointers: Variables that store memory addresses, used for indirect data access.
- Structures: User-defined composite types that group variables of different types under a single name.
- Unions: Similar to structures, but all members share the same memory location.
4. void:
- Represents the absence of a value, often used for functions that don’t return a value.
Key Points:
- Each data type has a specific size in memory (e.g., int is typically 4 bytes).
- Operations must be performed on compatible data types to avoid errors.
- Choosing appropriate data types is essential for efficient memory usage and accurate calculations.
- C also supports type modifiers like const and volatile for additional control over data.
Example:
C
int age = 25; // Integer variable
float pi = 3.14159; // Floating-point variable
char initial = 'A'; // Character variable