Data Types in C Programming
On this page (11sections)
Introduction
Every variable in C has a data type that tells the compiler how much memory to allocate and what operations are allowed. C provides several fundamental types and lets you build complex types from them (arrays, structures, pointers).
Fundamental Data Types
| Type | Typical size | Purpose | Example |
|---|---|---|---|
char | 1 byte | Single character or small integer | 'A', 65 |
int | 4 bytes | Whole numbers | 42, -100 |
float | 4 bytes | Single-precision decimal | 3.14f |
double | 8 bytes | Double-precision decimal | 3.14159 |
void | — | No value (functions with no return) | void printMsg(void) |
Use sizeof(type) to check actual sizes on your system:
#include <stdio.h>
int main(void) {
printf("char: %zu byte(s)\n", sizeof(char));
printf("int: %zu byte(s)\n", sizeof(int));
printf("float: %zu byte(s)\n", sizeof(float));
printf("double: %zu byte(s)\n", sizeof(double));
return 0;
}
Sample Output (typical 64-bit system)
char: 1 byte(s)
int: 4 byte(s)
float: 4 byte(s)
double: 8 byte(s)
Type Modifiers
C extends basic types with modifiers:
| Modifier | Effect |
|---|---|
signed / unsigned | With or without sign (default for int is signed) |
short | Smaller range, less memory (short int) |
long | Wider range on some platforms (long int, long double) |
unsigned int count = 100;
short int age = 25;
long int bigNumber = 1000000L;
Format Specifiers for printf
| Type | Specifier | Example |
|---|---|---|
int | %d | printf("%d", n); |
unsigned int | %u | printf("%u", n); |
float / double | %f | printf("%.2f", pi); |
char | %c | printf("%c", ch); |
char* (string) | %s | printf("%s", name); |
Example — Declaring and Printing Values
#include <stdio.h>
int main(void) {
int age = 20;
float height = 5.9f;
char grade = 'A';
printf("Age: %d years\n", age);
printf("Height: %.1f feet\n", height);
printf("Grade: %c\n", grade);
return 0;
}
Sample Output
Age: 20 years
Height: 5.9 feet
Grade: A
Best Practices
- Choose the smallest type that fits your data to save memory.
- Use
doubleinstead offloatunless you have a specific reason. - Append
fto float literals (3.14f) to avoid unintended double promotion. - Always match format specifiers to the actual variable type.
Common Mistakes
- Using
%dto print afloat— causes undefined behaviour; use%f. - Assuming fixed sizes — always use
sizeofor<stdint.h>types (int32_t) for portability.
Related Pages
Continue learning with these related tutorials and programs:
- C Tutorials — Browse all C Tutorials.
- C Introduction — Start here — what C is and why it matters.
- Command Line Arguments in C (argc and argv) — More in c basics.
- C History — More in c basics.
Frequently Asked Questions
What is the size of int in C?
The size of int is platform-dependent. On most modern systems it is 4 bytes (32 bits), but C only guarantees a minimum range — use sizeof(int) to check on your compiler.
What is the difference between float and double?
double typically has twice the precision and range of float. Use float for memory-sensitive graphics; use double for general numeric calculations.