Data Types in C
C has a small set of basic data types:
Integer Types:
- char: 1 byte (also used for characters)
- short: At least 2 bytes
- int: At least 2 bytes (usually 4)
- long: At least 4 bytes
- long long: At least 8 bytes
Floating Point:
- float: Single precision (4 bytes)
- double: Double precision (8 bytes)
- long double: Extended precision
Modifiers:
- signed: Can hold negative values (default)
- unsigned: Only positive values
sizeof Operator: Returns the size in bytes of a type or variable.
Type Qualifiers:
- const: Value cannot be changed
- volatile: May change unexpectedly (hardware)
Code Examples
C Data Typesc
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main() {
// Integer types
char c = 'A';
short s = 32767;
int i = 2147483647;
long l = 2147483647L;
long long ll = 9223372036854775807LL;
printf("char: %c (ASCII: %d)\n", c, c);
printf("int: %d\n", i);
// Unsigned types
unsigned int ui = 4294967295U;
printf("unsigned int: %u\n", ui);
// Floating point
float f = 3.14159f;
double d = 3.141592653589793;
printf("float: %.5f\n", f);
printf("double: %.15f\n", d);
// sizeof operator
printf("\nSizes in bytes:\n");
printf("char: %zu\n", sizeof(char));
printf("int: %zu\n", sizeof(int));
printf("long: %zu\n", sizeof(long));
printf("float: %zu\n", sizeof(float));
printf("double: %zu\n", sizeof(double));
// Constants
const double PI = 3.14159265359;
printf("\nPI: %.11f\n", PI);
// Type limits
printf("\nInt range: %d to %d\n", INT_MIN, INT_MAX);
return 0;
}C data type sizes can vary by platform. Always use sizeof() when you need exact sizes. limits.h contains type range constants.
Quick Quiz
1. What's the minimum guaranteed size of an int in C?
Was this lesson helpful?