C Data Types
A data type tells the compiler what kind of value a variable will store, and how much memory it needs.
Basic data types
| Type | Used for | Example | Format |
|---|---|---|---|
char | Single character | 'A' | %c |
int | Whole numbers | 120 | %d |
float | Decimal number (6 digits precision) | 3.14f | %f |
double | Decimal number (15 digits precision) | 3.141592 | %lf |
void | No value | function with no return | none |
Size of data types
The size depends on the system. This program prints it on your computer with sizeof.
#include <stdio.h>
int main(void) {
printf("char : %zu byte\n", sizeof(char));
printf("short : %zu bytes\n", sizeof(short));
printf("int : %zu bytes\n", sizeof(int));
printf("long : %zu bytes\n", sizeof(long));
printf("long long : %zu bytes\n", sizeof(long long));
printf("float : %zu bytes\n", sizeof(float));
printf("double : %zu bytes\n", sizeof(double));
return 0;
}
Output:
Output of the above program is
char : 1 byte
short : 2 bytes
int : 4 bytes
long : 8 bytes
long long : 8 bytes
float : 4 bytes
double : 8 bytes
Note:
This output is from a 64 bit Mac. On 64 bit Windows long is 4 bytes, so your result can be different. int is normally 4 bytes.
Type modifiers
You can change the size or range with short, long, signed and unsigned. An unsigned int stores only zero and positive numbers, but its upper limit is double.
#include <stdio.h>
#include <limits.h>
int main(void) {
printf("int max : %d\n", INT_MAX);
printf("int min : %d\n", INT_MIN);
printf("unsigned max : %u\n", UINT_MAX);
return 0;
}
Output:
Output of the above program is
int max : 2147483647
int min : -2147483648
unsigned max : 4294967295
Next: C Constants.
Congratulations! Chapter Finished. Learn more about the similar topics: