In C programming, data types define the nature of the data that can be stored in variables. They determine the size of the data and the operations that can be performed on it. C provides several data types that are categorized into basic, derived, enumeration, and void types.
| Types | Data Types |
|---|---|
| Basic Data Type | int, char, float, double |
| Derived Data Type | array, pointer, structure, union |
| Enumeration Data Type | enum |
| Void Data Type | void |
Basic data types are the fundamental data types in C, and they are further classified into integer and floating-point types. The size of these data types can vary based on the system architecture (e.g., 32-bit or 64-bit).
Basic Data Types and Their Sizes (for 32-bit Architecture):
| Data Type | Memory Size | Range |
|---|---|---|
| char | 1 byte | −128 to 127 |
| signed char | 1 byte | −128 to 127 |
| unsigned char | 1 byte | 0 to 255 |
| short | 2 bytes | −32,768 to 32,767 |
| signed short | 2 bytes | −32,768 to 32,767 |
| unsigned short | 2 bytes | 0 to 65,535 |
| int | 2 bytes | −32,768 to 32,767 |
| signed int | 2 bytes | −32,768 to 32,767 |
| unsigned int | 2 bytes | 0 to 65,535 |
| short int | 2 bytes | −32,768 to 32,767 |
| signed short int | 2 bytes | −32,768 to 32,767 |
| unsigned short int | 2 bytes | 0 to 65,535 |
| long int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| signed long int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| unsigned long int | 4 bytes | 0 to 4,294,967,295 |
| float | 4 bytes | Approx. ±3.4 × 1038, 6 decimal places |
| double | 8 bytes | Approx. ±1.7 × 10308, 15 decimal places |
| long double | 10 bytes | Larger range and precision than double |
<#include <stdio.h>>
int main() {
int age = 25;
char grade = 'A';
float temperature = 98.6;
double pi = 3.14159265359;
printf("Age: %d\\n", age);
printf("Grade: %c\\n", grade);
printf("Temperature: %.1f\\n", temperature);
printf("Pi: %.11f\\n", pi);
return 0;
}
Age: 25
Grade: A
Temperature: 98.6
Pi: 3.14159265359
Derived data types are built from the basic data types and include arrays, pointers, structures, and unions.
An array is a collection of elements of the same type stored in contiguous memory locations.
<#include <stdio.h>>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
printf("Values in the array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\\n");
return 0;
}
Values in the array: 10 20 30 40 50
A pointer holds the memory address of another variable. It is used for dynamic memory allocation, function arguments, and arrays.
<#include <stdio.h>>
int main() {
int num = 42;
int *ptr = #
printf("Value of num: %d\\n", *ptr);
return 0;
}
Value of num: 42
A structure allows the grouping of variables of different types under a single name. Structures are useful for representing complex data.
<#include <stdio.h>>
<#include <string.h>>
struct Person {
char name[50];
int age;
float height;
};
int main() {
struct Person person1;
strcpy(person1.name, "John Doe");
person1.age = 30;
person1.height = 1.8;
printf("Name: %s\\n", person1.name);
printf("Age: %d\\n", person1.age);
printf("Height: %.2f\\n", person1.height);
return 0;
}
Name: John Doe
Age: 30
Height: 1.80
A union allows storing different data types in the same memory location. All members share the same memory, so only one member can hold a value at a time.
<#include <stdio.h>>
union NumericValue {
int intValue;
float floatValue;
char stringValue[20];
};
int main() {
union NumericValue value;
value.intValue = 42;
printf("Integer Value: %d\\n", value.intValue);
value.floatValue = 3.14;
printf("Float Value: %.2f\\n", value.floatValue);
return 0;
}
Integer Value: 42 Float Value: 3.14
An enumeration defines a set of named integer constants. It improves code readability and maintainability.
<#include <stdio.h>>
enum DaysOfWeek {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
int main() {
enum DaysOfWeek today = Wednesday;
printf("Today is %d\\n", today);
return 0;
}
The void data type indicates the absence of data or type. It is commonly used for functions that do not return a value and for generic pointers.
<#include <stdio.h>>
void printHello() {
printf("way to code\\n");
}
int main() {
printHello();
return 0;
}
<#include <stdio.h>>
void printHello() {
printf("way to code\\n");
}
int main() {
printHello();
return 0;
}
<#include <stdio.h>>
int main() {
int number = 10;
void* dataPtr = &number;
printf("Value of number: %d\\n", *(int*)dataPtr);
return 0;
}
Data types in C are crucial for defining the nature of data that variables can hold. They affect memory allocation, data manipulation, and overall program performance. Basic data types include integer and floating-point types, while derived data types allow complex data management through arrays, pointers, structures, and unions. Enumeration provides named constants for better readability, and the void type is used for functions that do not return values and generic pointers. Understanding these data types helps in writing efficient and well-structured C programs.