Data Types & Format Specifiers in C (With Examples)
▶ Watch this video for a clear Gujarati explanation of Data Types & Format Specifiers in C.
Introduction
Understanding how C handles different kinds of data and how to correctly display them using format specifiers is essential for effective programming. Let’s break it down clearly.
1. Data Types in C
In C, data types tell the compiler how much space to allocate and how to interpret a piece of data. Common data types:
- int – for integer values (e.g. 1, -20)
- float – for decimal numbers (single precision)
- double – for decimal numbers (double precision)
- char – for single characters (e.g. ‘a’)
2. Format Specifiers in C
Format specifiers are used in printf and scanf to tell the compiler how to interpret the type of data.
| Data Type | Format Specifier | Example |
|---|---|---|
| int | %d or %i | printf(“%d”, num); |
| float | %f | printf(“%f”, price); |
| double | %lf | printf(“%lf”, balance); |
| char | %c | printf(“%c”, ch); |
3. Example Code
#include <stdio.h>
int main() {
int num = 10;
float price = 5.75;
double balance = 15000.50;
char ch = 'A';
printf("Integer: %d\n", num);
printf("Float: %f\n", price);
printf("Double: %lf\n", balance);
printf("Character: %c\n", ch);
return 0;
}
© YourSite — Comprehensive guide on Data Types & Format Specifiers in C.