Data Types Tutorials
Programming
Data Types in C
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.
Why Do We Need Data Types?
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.
Example of Different Data Types in C
#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;
}Output
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.
Types of Data Types in C
C provides the following fundamental data types:
Integer (int)
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);Character (char)
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);Float (float)
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);Double (double)
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);Void (void)
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!");
}Finding the Size of Data Types
The sizeof() operator is used to determine the amount of memory occupied by a data type.
Example
#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;
}Output
Size of int: 4
Size of char: 1
Size of float: 4
Size of double: 8Data Type Modifiers
Data type modifiers are used to change the size or range of a data type.
short
Reduces memory usage for integer values.
Example:
short int age;long
Increases the storage capacity of integer values.
Example:
long int population;signed
Allows both positive and negative values.
Example:
signed int marks;unsigned
Allows only positive values.
Example:
unsigned int salary;Literals in C
A literal is a fixed value directly assigned to a variable.
Examples
int age = 25;
float price = 99.99;
char grade = 'A';
char name[] = "Hello";Common Types of Literals
Integer Literal:
100Floating Literal:
10.5Character Literal:
'A'String Literal:
"Hello"
Type Conversion in C
Type conversion is the process of converting one data type into another.
There are two types of type conversion in C.
Implicit Type Conversion
This conversion is performed automatically by the compiler.
Example
int num = 10;
float result = num;In this case, the integer value is automatically converted into a float value.
Explicit Type Conversion (Type Casting)
This conversion is performed manually by the programmer.
Example
float value = 12.75;
int num = (int)value;
printf("%d", num);Output
12The decimal part is removed because the float value is converted to an integer.
Summary
Data types define the type of data a variable can store.
C provides five fundamental data types:
int,char,float,double, andvoid.The
sizeof()operator is used to find the size of a data type.Modifiers such as
short,long,signed, andunsignedmodify 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.