C Language track

Lesson 02 · Level 1: Basics

Variables & Types

Integers, floats and characters, declarations, initialisation and sizeof

Level 1: Basics5 min readFree lesson

A variable is something that stores a value in memory and allows it to be called by name. Data type determines what type of value and how many bytes are stored in the box.

Learning Objectives

  • Separate Declaration/initialization/assignment variables.
  • Distinguish between the size and use of C's basic data types (int, float, double, char).
  • Check the data type size with the sizeof operator.
  • Define constants as const and #define.

Why are there multiple data types?

A large box may seem like a good idea if it can hold value, but in reality, that's not the case.

  • If you store the integer 100 in 8 bytes long, 7 bytes are wasted.
  • If 3 billion (about 3.1 billion) is stored in 4 bytes int, overflow will occur.
  • 3.14 can never be expressed with int.

The data type determines “what type of value this memory slot holds” and allows the compiler to determine. It determines the Arithmetic/Input/Output/Storage method.

Key concepts

1) Lifetime of a variable

int age;              //declaration only (value is garbage value)
age = 20;             //college admissions
int year = 2026;      //Initialized upon declaration

In terms of memory:

Immediately after declaration, after substitution
┌──────────────┐          ┌──────────────┐
│  age :  ???  │          │  age :   20  │
└──────────────┘          └──────────────┘

2) Basic data type (based on 64-bit Linux)

data type size Expression range (approximately)
char 1 byte -128 ~ 127
short 2 byte -32,768 ~ 32,767
int 4 bytes -2.1 billion ~ +2.1 billion
long 8 bytes very large
float 4 bytes Approximately 7 digit precision
double 8 bytes Approximately 15 digit precision

Adding unsigned gives up the negative range and doubles the positive range.

3) Constants: Naming unchanging values

#define PI 3.14159        //Preprocessor macros — replace text before compilation
const int MAX = 100;       //Normal variable but cannot be changed

Instead of scattering literals (3.14159) throughout the code, put the name in one place. If the value changes, you only need to change one part of the definition.

4) sizeof operator

It tells you how many bytes a data type or variable takes up. It may vary depending on the operating system and compiler, so printing it directly is useful for debugging.

printf("%zu\n", sizeof(int));  //Normal 4

See as example

Example 1 — ex01_variables.c: Declaration → Assignment → Update

int age = 20;
int year;
year = 2026;
printf("Age: %d years old\n", age);
printf("Year: Year %d\n", year);
age = age + 1;
printf("Age next year: %d years old\n", age);

Execution result

Age: 20
Year: 2026
Age next year: 21

Key point: = is not “equal to” but “substitute the value on the right into the left”.

Example 2 — ex02_types.c: Output format for each data type

int    score = 95;
float  ratio = 0.75f;
double price = 19999.99;
char   grade = 'A';

Execution result

Integer (%d): 95
Real (%f): 0.750000
Real (%.2f): 0.75
Double precision (%lf): 19999.990000
Character (%c): A
ASCII of characters (%d): 65

Key point: The character ('A') is also output as integer code (65). That is, char is a small integer.

Example 3 — ex03_sizeof.c: Check data type size directly

Execution result

char   = 1 byte
short  = 2 byte
int    = 4 byte
long   = 8 byte
float  = 4 byte
double = 8 byte
Size of variable a: 4 bytes

Key point: The result of sizeof is of type size_t, so printf is output as %zu.

Example 4 — ex04_const.c: Two ways of a constant

Execution result

Circumference (macro): 3.141590
Maximum users (macro): 100
Week (const): 7 days
Area of a circle with radius 5.0: 78.539750

Key point: const undergoes data type checking like a variable, and #define is a simple text substitution.

A different perspective — “Labeled Boxes”

variable = labeled memory box
Data type = size and shape of the box

   ┌─────────┐  ┌─────────┐  ┌─────────────┐
   │  age    │  │ score   │  │   price     │
   │  int    │  │  int    │  │   double    │
   │ 4 byte  │  │ 4 byte  │  │   8 byte    │
   │  [20]   │  │  [95]   │  │ [19999.99]  │
   └─────────┘  └─────────┘  └─────────────┘

If you try to put a different shape value in the same box (assigning 3.14 to int), the compiler Cut off the decimal part (truncation) and insert the value.

Common Mistakes

  1. Use of uninitialized variable: int x; printf("%d", x); → Output garbage value.
  2. Integer overflow: int n = 3000000000; → It may be negative due to exceeding the expression range.
  3. Floating point error: Even if you add 0.1 100 times with float, it will not be exactly 10.
  4. %d vs %f confusion: If the data type and format do not match, garbage is output.
  5. 'A' vs "A" confusion: Single quotation marks are characters (1 byte), double quotation marks are strings (2 bytes, 'A'+'\0').

Cleanup

  • A variable is a name given to memory, and a data type is the size of that memory and how it is interpreted.
  • In 64-bit Linux, int=4 and double=8 bytes are default.
  • Give constants meaningful names to reduce magic numbers.
  • Let’s get into the habit of checking the size directly with sizeof.
  • Data types and format specifiers (%d, %f, etc.) must match.

Try it yourself

cd src
gcc -std=c11 -Wall -o ex01 ex01_variables.c && ./ex01
gcc -std=c11 -Wall -o ex02 ex02_types.c     && ./ex02
gcc -std=c11 -Wall -o ex03 ex03_sizeof.c    && ./ex03
gcc -std=c11 -Wall -o ex04 ex04_const.c     && ./ex04

Application:

  • Try printing by adding long long and unsigned int to ex03_sizeof.c.
  • Uncomment DAYS_IN_WEEK = 8; of ex04_const.c and check what error the compiler generates.

Example files

The runnable files used in this lesson. Copy them into your own project and experiment.

src/ex01_variables.cC
#include <stdio.h>

int main(void) {
    int age = 20;
    int year;

    year = 2026;

    printf("Age: %d years old\n", age);
    printf("Year: Year %d\n", year);

    age = age + 1;
    printf("Age next year: %d years old\n", age);

    return 0;
}
src/ex02_types.cC
#include <stdio.h>

int main(void) {
    int    score = 95;
    float  ratio = 0.75f;
    double price = 19999.99;
    char   grade = 'A';

    printf("Integer (%%d): %d\n", score);
    printf("Real number (%%f): %f\n", ratio);
    printf("Real number (%%.2f): %.2f\n", ratio);
    printf("Double precision (%%lf): %lf\n", price);
    printf("Character (%%c): %c\n", grade);
    printf("ASCII for character (%%d): %d\n", grade);

    return 0;
}
src/ex03_sizeof.cC
#include <stdio.h>

int main(void) {
    printf("char   = %zu byte\n", sizeof(char));
    printf("short  = %zu byte\n", sizeof(short));
    printf("int    = %zu byte\n", sizeof(int));
    printf("long   = %zu byte\n", sizeof(long));
    printf("float  = %zu byte\n", sizeof(float));
    printf("double = %zu byte\n", sizeof(double));

    int a = 0;
    printf("Size of variable a: %zu byte\n", sizeof(a));

    return 0;
}
src/ex04_const.cC
#include <stdio.h>

#define PI 3.14159
#define MAX_USERS 100

int main(void) {
    const int DAYS_IN_WEEK = 7;
    double radius = 5.0;

    printf("Circumference (macro): %f\n", PI);
    printf("Maximum users (macro): %d\n", MAX_USERS);
    printf("One week (const): %d days\n", DAYS_IN_WEEK);
    printf("Area of a circle with radius %.1f: %f\n", radius, PI * radius * radius);

    /*DAYS_IN_WEEK = 8;  // Compilation error - const cannot be changed*/

    return 0;
}

Assignment

Problem 1

Declare height (height, unit cm) and weight (weight, unit kg) as variables. Print in the following format. The value can be freely determined.

Height: 175.5 cm
Weight: 68.2 kg
  • File name: hw01.c

Problem 2

Print the volume and surface area of a cube whose side length is side = 4.

Volume: 64
Surface area: 96
  • File name: hw02.c

Problem 3

Define LIGHT_SPEED as #define as 299792458 (m/s), Print the distance light travels in 1 second in km.

  • File name: hw03.c
Show the reference solution

Try the assignment yourself first — comparing your attempt with a working solution is where most of the learning happens.

hw01.cC
#include <stdio.h>

int main(void) {
    float height = 175.5f;
    float weight = 68.2f;

    printf("Key: %.1f cm\n", height);
    printf("Weight: %.1f kg\n", weight);

    return 0;
}
hw02.cC
#include <stdio.h>

int main(void) {
    int side = 4;
    int volume = side * side * side;
    int surface = 6 * side * side;

    printf("Volume: %d\n", volume);
    printf("Surface area: %d\n", surface);

    return 0;
}
hw03.cC
#include <stdio.h>

#define LIGHT_SPEED 299792458   /* m/s */

int main(void) {
    long meters = LIGHT_SPEED;
    long km = meters / 1000;

    printf("Light travels approximately %ld km in 1 second.\n", km);
    return 0;
}

Learn this lesson with a mentor

Work through the C Language track on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now