In C programming language, data types specify the type of data that a variable can hold. There are various data types available in C, which are broadly classified into two categories:
- Primary Data Types
- Derived Data Types
Let's see some examples of both primary and derived data types:
Primary Data Types
- int: The int data type is used to store integer values. It takes 2 or 4 bytes of memory depending on the compiler.
- char: The char data type is used to store a single character value. It takes 1 byte of memory.
- float: The float data type is used to store floating-point values. It takes 4 bytes of memory.
- double: The double data type is used to store double-precision floating-point values. It takes 8 bytes of memory.
- void: The void data type is used to specify that the function does not return any value.
Example:
int num = 10;
char ch = 'A';
float num = 3.14;
double num = 3.14159;
void printHello() {
printf("Hello");
}
Derived Data Types
- Array: An array is a collection of similar data types stored in contiguous memory locations.
- Pointer: A pointer is a variable that stores the memory address of another variable.
- Structure: A structure is a user-defined data type that groups together variables of different data types.
- Union: A union is a user-defined data type that allows storing different data types in the same memory location.
- Enumeration: An enumeration is a user-defined data type that consists of a set of named integer constants.
Example:
int arr[5] = {1, 2, 3, 4, 5};
int num = 10;
int *ptr = #
struct employee {
char name[50];
int age;
float salary;
};
union data {
int num;
char ch;
float fnum;
};
enum weekDays {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
Comments