Data Type in C
In C programming language, data types specify the type and size of data that can be stored in variables. C provides several built-in data types, which can be broadly categorized as follows:
-
Basic (Primitive) Data Types:
- int: Used to store whole numbers (e.g., 10, -5, 0). The size of int is typically 4 bytes.
- char: Used to store individual characters (e.g., 'a', 'B', '$'). The size of char is 1 byte.
- float: Used to store floating-point numbers with single precision (e.g., 3.14, -0.5). The size of float is typically 4 bytes.
- double: Used to store floating-point numbers with double precision (e.g., 3.14159, -0.5678). The size of double is typically 8 bytes.
- _Bool: Used to store boolean values (true or false). The size of _Bool is typically 1 byte.
-
Derived Data Types:
- Arrays: Used to store a collection of elements of the same type. The size of an array depends on the number of elements and the size of each element.
- Pointers: Used to store memory addresses. The size of a pointer depends on the system architecture (e.g., 4 bytes on a 32-bit system, 8 bytes on a 64-bit system).
- Structures: Used to group related data items under a single name. The size of a structure depends on the size of its members.
- Unions: Similar to structures, but can store only one member at a time. The size of a union is determined by the size of its largest member.
- Enums: Used to define named constants. The size of an enumeration depends on the number of elements and the size of the underlying type (usually int).
-
Void Data Type:
- void: Represents the absence of any type. It is commonly used as a function return type or to indicate no value.
Modifiers can be applied to the basic data types to specify additional properties such as signedness and size. For example:
- signed int or signed: Represents a signed integer (can represent positive and negative values).
- unsigned int or unsigned: Represents an unsigned integer (can represent only non-negative values).
- short int or short: Represents a shorter range of integers.
- long int or long: Represents a larger range of integers.
- long long int or long long: Represents an even larger range of integers.
C also allows the creation of user-defined data types using structures, unions, and typedefs, which provide flexibility and abstraction in organizing and manipulating data.
Here's an example that demonstrates the usage of some basic data types in C:
#include
int main() {
int age = 30;
char grade = 'A';
float salary = 2500.75;
double pi = 3.14159;
_Bool isTrue = 1;
printf("Age: %d\n", age);
printf("Grade: %c\n", grade);
printf("Salary: %.2f\n", salary);
printf("Pi: %lf\n", pi);
printf("Is True? %d\n", isTrue);
return 0;
}
In the example above, we declare variables of different data types (int, char, float, double, and _Bool), assign values to them, and print their values using the printf function.