C/C++ Arena

C variables and data types

int, double, char and friends. How to declare variables in C, what each type holds, and how big they are.

A variable is a named piece of memory with a type that decides what it can hold:

Type Holds Typical size
int whole numbers 4 bytes
double decimal numbers 8 bytes
char one character (a small number) 1 byte
long long bigger whole numbers 8 bytes

Always give a variable a value when you declare it: a local variable that was never set holds garbage. sizeof tells you how many bytes a type or variable uses. For exact sizes, <stdint.h> has int32_t, uint8_t and friends.

Example

#include <stdio.h>

int main(void) {
    int lives = 3;
    double speed = 2.5;
    char grade = 'A';
    printf("%d %.1f %c\n", lives, speed, grade);
    printf("int is %zu bytes\n", sizeof(int));
    return 0;
}

Output:

3 2.5 A
int is 4 bytes

Practice it