Data Types in C
Data types in C define the type of data that a variable can store. They tell the compiler how much memory to allocate and what type of operations can be performed on that data. Choosing the correct data type is essential for efficient and error-free programming.
Types of Data Types in C
- Basic (Primary) Data Types: int, char, float, double
- Derived Data Types: array, pointer, structure, union
- Enumeration Data Type: enum
- Void Data Type: void (represents no value)
Basic Data Types
- int: Stores integers (whole numbers). Example: int age = 25;
- char: Stores a single character. Example: char grade = 'A';
- float: Stores single-precision floating-point numbers. Example: float salary = 5500.50;
- double: Stores double-precision floating-point numbers. Example: double pi = 3.14159;
Derived Data Types
- Array: Stores multiple values of the same type. Example: int marks[5] = {90, 85, 78, 92, 88};
- Pointer: Stores memory addresses. Example: int *ptr = &age;
- Structure: Stores a collection of different types. Example: struct Student { char name[20]; int roll; };
- Union: Stores different types in the same memory location. Example: union Data { int i; float f; char c; };
Enumeration Data Type
- enum: Represents a set of named integer constants. Example: enum Week {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
Void Data Type
- void: Represents no value, used in functions that do not return anything. Example: void display() { printf("Hello World\n"); }
Conclusion
Understanding data types is fundamental in C programming. They define how data is stored, how operations are performed, and how memory is managed.
Using the correct data type improves efficiency, prevents errors, and ensures that programs behave as expected.
Codecrown