Data types in C are used to define the type of data that a variable can hold. They play a crucial role in ensuring the accurate and consistent execution of your C programs.
Table of Contents
Main Types
C provides four basic arithmetic type specifiers:
char:
- Stores a single character (letter, digit, or symbol).
- Size: 1 byte.
- Range: -128 to 127 (ASCII characters).
- Example:
char character = 'a'; // Stores the character 'a'
int:
- Stores whole numbers (positive, negative, or zero).
- Size: Typically 4 bytes (can vary on different systems).
- Range: -2147483648 to 2147483647.
- Example:
int age = 25; // Stores the integer 25
short int small_number = 10; // Stores the integer 10 within a smaller memory space
long int large_number = 1000000; // Stores the integer 1000000 with a larger range
float:
- Stores single-precision floating-point numbers (decimal numbers).
- Size: 4 bytes.
- Precision: Approximately 6-7 decimal digits.
- Example:
float price = 12.50; // Stores the floating-point number 12.50 with single precision
double:
- Stores double-precision floating-point numbers.
- Size: 8 bytes.
- Precision: Approximately 15-16 decimal digits.
- Example:
double pi = 3.14159; // Stores the floating-point number 3.14159 with double precision
Modifiers: In addition to these basic types, C provides modifiers to further define the size and range of these types:
- signed: Indicates that the integer can be positive, negative, or zero.
// Explicitly declaring signed integers:
signed int temperature = -15; // Stores a negative temperature
signed short age = 20; // Stores an age (usually a non-negative value, but signed for flexibility)
signed char grade = 'B'; // Stores a grade (also typically non-negative, but using signed for consistency)
// Default signed behavior for int and char:
int distance = 100; // Implicitly signed
char initial = 'A'; // Implicitly signed
- unsigned: Indicates that the integer can only be positive or zero.
unsigned int count = 100; // Stores a non-negative count
unsigned short pixel_value = 255; // Stores a pixel intensity (usually 0-255)
unsigned long file_size = 4294967295UL; // Stores a large file size
- short: Reduces the size of the integer type (e.g., short int).
short int age = 25; // Stores age as a 2-byte integer
short small_number = 123; // Stores a small integer within the range
- long: Increases the size of the integer type (e.g., long int).
long int big_number = 999999999; // Stores a large integer
long long int population = 8000000000; // Stores an even larger integer
long double pi = 3.14159265358979323846L; // Stores pi with extended precision
Derived Types
C also provides derived types based on the basic types:
- Pointer: Stores the memory address of another variable.
int a = 10;
int *ptr = &a; // ptr stores the memory address of variable 'a'
- Array: Stores a fixed-size collection of elements of the same type.
int numbers[5] = {1, 2, 3, 4, 5}; // Array of 5 integers
char name[20] = "John Doe"; // Array of 20 characters
- Structure: Organizes multiple variables of different types into a single unit.
struct Person {
char name[20];
int age;
};
struct Person person = {"John Doe", 30}; // Structure variable with name and age
- Union: Stores one of several variables of different types within the same memory location.
union Data {
int number;
char character;
};
union Data data;
data.number = 10; // Stores the integer 10
data.character = 'a'; // Stores the character 'a' (only one value can be stored at a time)
- Function: Defines a block of code that performs a specific task and can receive and return values.
int sum(int a, int b) {
return a + b;
}
int result = sum(5, 10); // Function call to sum 5 and 10, storing the result in 'result'
- void: Represents the absence of a value.
void print_message() {
printf("This function doesn't return a value");
}
print_message(); // Function call that just prints a message
Additional Types
- bool: Introduced in C99, explicitly stores Boolean values (true or false).
- Enumerated type: Enumeration is used to define named constant values that represent integer values. For example:
enum Day {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};
Size of data types
Data Type | Size (bytes) |
---|---|
char | 1 |
short int | 2 |
int | 4 |
long int | 8 |
long long int | 16 |
float | 4 |
double | 8 |
long double | 16 |
bool | 1 |
char * | 8 |
void * | 8 |
Choosing the Right Data Type
Choosing the appropriate data type for your variables is crucial for program efficiency and memory management. Consider the range of values your variable needs to represent and choose the type that offers the best balance of size and precision.
See also: