C Data Types
A data type tells C what kind of value a variable holds and how much memory it needs. Choosing the right type keeps your programs correct and efficient. This page introduces the basic built-in types you will use every day.
The Basic Types
C has a small set of core data types. The three you will meet first are int for whole numbers, float and double for numbers with a decimal point, and char for a single character.
One variable of each basic type
#include <stdio.h>
int main() {
int myInt = 15;
float myFloat = 5.99;
char myChar = 'D';
printf("%d %f %c\n", myInt, myFloat, myChar);
return 0;
}Basic data types
Note: The exact sizes can vary between systems, but the values in the table are true for most common desktop compilers today.
Choosing the Right Type
Use int when you are counting whole things such as items or people. Use float or double when a value can have a fractional part, like a price or a temperature. Use char for a single letter or symbol.
Matching type to purpose
#include <stdio.h>
int main() {
int students = 30;
double temperature = 36.6;
char section = 'B';
printf("%d students in section %c at %.1f degrees\n", students, section, temperature);
return 0;
}Why Types Matter
- They decide how much memory a value uses.
- They decide the range of values a variable can hold.
- They decide whether decimals are allowed.
- They must match the format specifier you use in printf.
Note: Prefer double over float for most decimal work. double is more precise, and the small extra memory cost is rarely a problem on modern machines.
Exercise: C Data Types
Which data type would you use to store a whole number like 42?