Loading content...
Loading content...
Data types are one of the fundamental concepts in C programming. Every variable in C must have a data type that defines the type of value it can store, such as integers, characters, decimal numbers, and more.
C is a statically typed language, which means the data type of a variable must be declared before it is used. Once declared, the data type cannot be changed during program execution.
Data types help the compiler:
Allocate the correct amount of memory.
Understand the type of data being stored.
Perform valid operations on variables.
Improve program efficiency and reliability.
#include <stdio.h>
int main()
{
int age = 20;
float height = 5.7;
double pi = 3.14159;
char grade = 'A';
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Pi: %.5lf\n", pi);
printf("Grade: %c\n", grade);
return 0;
}Age: 20
Height: 5.7
Pi: 3.14159
Grade: ANote: The exact size of data types may vary depending on the compiler and operating system.
C provides the following fundamental data types:
Used to store whole numbers.
Typical size is 4 bytes.
Range is approximately -2,147,483,648 to 2,147,483,647.
Format specifier: %d
Example
int age = 22;
printf("%d", age);Used to store a single character.
Typical size is 1 byte.
Can store letters, digits, and special symbols.
Format specifier: %c
Example
char grade = 'A';
printf("%c", grade);Used to store decimal numbers.
Typical size is 4 bytes.
Provides single precision floating-point storage.
Format specifier: %f
Example
float price = 12.45;
printf("%f", price);Used to store decimal numbers with higher precision than float.
Typical size is 8 bytes.
Suitable when greater accuracy is required.
Format specifier: %lf
Example
double pi = 3.1415926535;
printf("%lf", pi);Represents the absence of a value.
Used in functions that do not return any value.
Can also be used with generic pointers (void *).
Example
void greet()
{
printf("Hello, Welcome!");
}The sizeof() operator is used to determine the amount of memory occupied by a data type.
#include <stdio.h>
int main()
{
printf("Size of int: %zu\n", sizeof(int));
printf("Size of char: %zu\n", sizeof(char));
printf("Size of float: %zu\n", sizeof(float));
printf("Size of double: %zu\n", sizeof(double));
return 0;
}Size of int: 4
Size of char: 1
Size of float: 4
Size of double: 8Data type modifiers are used to change the size or range of a data type.
Reduces memory usage for integer values.
Example:
short int age;Increases the storage capacity of integer values.
Example:
long int population;Allows both positive and negative values.
Example:
signed int marks;Allows only positive values.
Example:
unsigned int salary;A literal is a fixed value directly assigned to a variable.
int age = 25;
float price = 99.99;
char grade = 'A';
char name[] = "Hello";Integer Literal: 100
Floating Literal: 10.5
Character Literal: 'A'
String Literal: "Hello"
Type conversion is the process of converting one data type into another.
There are two types of type conversion in C.
This conversion is performed automatically by the compiler.
int num = 10;
float result = num;In this case, the integer value is automatically converted into a float value.
This conversion is performed manually by the programmer.
float value = 12.75;
int num = (int)value;
printf("%d", num);12The decimal part is removed because the float value is converted to an integer.
Data types define the type of data a variable can store.
C provides five fundamental data types: int, char, float, double, and void.
The sizeof() operator is used to find the size of a data type.
Modifiers such as short, long, signed, and unsigned modify the range and size of data types.
Literals are fixed values assigned directly to variables.
Type conversion allows values to be converted from one data type to another.