-
Hajipur, Bihar, 844101
In C programming, data types are used to define the kind of data a variable can hold. Every variable in C has a data type, which tells the compiler how much memory to allocate and what operations can be performed on the data. Understanding data types is essential for writing efficient, reliable, and error-free programs.
This chapter explains the different data types in C, their sizes, usage, and practical examples.
A data type is an attribute of a variable that determines:
The type of value the variable can store (e.g., integer, floating-point, character).
The amount of memory allocated to store the variable.
The operations that can be performed on the variable.
For example, an int can store whole numbers, while a float stores decimal numbers. You cannot perform arithmetic with a character in the same way as with an integer.
C data types can be broadly categorized into four groups:
Basic (Primitive) Data Types
Derived Data Types
Enumeration Data Types
Void Data Type
Let’s look at each category in detail.
These are the fundamental data types used to store simple values.
| Data Type | Description | Size (bytes) | Example |
|---|---|---|---|
| int | Integer numbers | 2–4 | int age = 25; |
| float | Single-precision decimals | 4 | float price = 99.5; |
| double | Double-precision decimals | 8 | double pi = 3.14159; |
| char | Single characters | 1 | char grade = 'A'; |
Example of using basic data types:
#include <stdio.h>
int main() {
int age = 20;
float height = 5.8;
char grade = 'B';
double pi = 3.14159;
printf("Age: %d\n", age);
printf("Height: %.2f\n", height);
printf("Grade: %c\n", grade);
printf("Pi: %.5lf\n", pi);
return 0;
}
Derived data types are based on the basic data types but allow more complex structures:
Arrays: Collection of elements of the same data type.
int numbers[5] = {1, 2, 3, 4, 5};
Pointers: Store the memory address of a variable.
int a = 10;
int *ptr = &a;
Structures (struct): Group of variables of different types under a single name.
struct Student {
int id;
char name[20];
float marks;
};
Unions: Similar to structures but share the same memory location for all members.
union Data {
int i;
float f;
char c;
};
Enumeration (enum) allows you to define a set of named integer constants. It improves code readability by replacing numeric values with meaningful names.
Example:
enum Days {Monday, Tuesday, Wednesday, Thursday, Friday};
enum Days today = Wednesday;
printf("Today is %d\n", today);
Here, Monday is 0, Tuesday is 1, and so on.
The void data type represents the absence of value. It is commonly used for:
Functions that do not return any value.
void greet() {
printf("Hello World\n");
}
Pointers that do not have a specific type.
void *ptr;
The size of a data type may vary depending on the system and compiler. A general guideline:
| Data Type | Typical Size (bytes) |
|---|---|
| char | 1 |
| int | 2–4 |
| float | 4 |
| double | 8 |
| void | 0 |
Knowing the size is important for memory management, especially in programs with large data.
C also provides type qualifiers to modify the basic data types:
signed / unsigned:
Signed variables can store both positive and negative numbers.
Unsigned variables store only positive numbers.
unsigned int age = 25;
short / long:
Short reduces memory usage for small integers.
Long increases the range of numbers.
long int population = 1000000;
short int day = 31;
#include <stdio.h>
int main() {
int age = 25;
float price = 49.99;
double pi = 3.141592;
char grade = 'A';
unsigned int visitors = 100;
printf("Age: %d\n", age);
printf("Price: %.2f\n", price);
printf("Pi: %.6lf\n", pi);
printf("Grade: %c\n", grade);
printf("Visitors: %u\n", visitors);
return 0;
}
Output:
Age: 25
Price: 49.99
Pi: 3.141592
Grade: A
Visitors: 100
This program demonstrates multiple basic and qualified data types in action.
Choose appropriate data types:
Use the smallest data type sufficient for the purpose to save memory.
Use type qualifiers wisely:
Unsigned types are useful for values that cannot be negative. Long types are useful for large numbers.
Comment complex declarations:
For arrays, structures, or pointers, add comments to explain their purpose.
Be consistent:
Maintain consistency in variable types across your program for easier maintenance.
Data types in C define the type and size of variables and determine what operations can be performed on them. Understanding basic, derived, enumeration, and void data types, along with type qualifiers, is crucial for effective programming. Proper use of data types helps in memory management, error prevention, and program readability. Mastering data types sets the foundation for working with constants, operators, and advanced programming concepts in C.
What is a data type in C, and why is it important?
List the basic data types in C and give an example of each.
Explain the difference between float and double with an example.
What is an enumeration (enum) in C, and how is it used?
Identify the error in the following code:
unsigned int age = -5;
Write a declaration for a long int variable named population and assign it a value of 1,000,000.
Explain the difference between signed and unsigned integers with an example.
Write a small C program using int, float, char, and unsigned int variables and print their values.
What is the purpose of the void data type in C?
Describe a situation where using a short int would be better than using a regular int.