We’re Officially Live β€” Lifetime Founding Membership Available for a Limited Time
Free preview

Variables & Data Types

Named boxes that hold a value. Pick a type, declare, assign, and learn why C makes you say what kind of thing each box holds.

20 min read

In this lesson:


A variable is a named box

When you write int score = 42;, you are doing three things at once:

  1. asking the compiler to set aside memory for a value,
  2. telling it the value will be an integer (int),
  3. giving that location a name (score) you can use later.

You can read from the box (printf("%d", score);) or overwrite it (score = 100;). The name is for your benefit; the CPU just sees an address.

Memory:   [........][..42....][........]
                      ↑
                    score

The four types you need first

C has many types, but four cover almost everything early on:

Type Holds Example value Size (typical)
int whole number, positive or negative 42, -7, 0 4 bytes
float number with a fractional part 3.14f, -0.5f 4 bytes
char a single character (or a tiny integer 0-255) 'A', '?', '\n' 1 byte
bool a truth value true or false 1 byte

bool is not built into C, you get it by including <stdbool.h>. Without that header, C programmers traditionally used int where 0 is false and any non-zero value is true.

#include <stdio.h>
#include <stdbool.h>
 
int main(void) {
    int   score   = 95;       // whole number
    float pi      = 3.14f;    // fractional; 'f' marks it as float
    char  grade   = 'A';      // single character in single quotes
    bool  passed  = true;     // from <stdbool.h>
 
    printf("score=%d pi=%.2f grade=%c passed=%d\n",
           score, pi, grade, passed);
    return 0;
}

The funny %d, %.2f, %c are format specifiers, they tell printf how to interpret each argument. They must match the variable's type, or you'll get garbage.


Declare, initialize, assign

These three operations look similar but are distinct:

int x;          // declare: reserve a box. Value is undefined garbage.
x = 5;          // assign: put 5 in the box.
 
int y = 7;      // declare and initialize in one line. Preferred.
 
y = y + 1;      // assign again: replace the value in the box.

Always initialize. A declared-but-never-initialized variable contains whatever bits were already in that memory location, unpredictable, machine-dependent, and a common source of "it works on my machine" bugs. Modern compilers warn about this; treat that warning as an error.

int x;          // ⚠ undefined value
printf("%d\n", x);   // prints garbage (could even crash)

Type rules and conversions

C will silently convert between numeric types in many situations. This is convenient but treacherous:

int   n   = 5;
float f   = 2.0f;
float avg = n / 2;        // integer division first! avg == 2.0, not 2.5
float ok  = n / 2.0f;     // float division. ok == 2.5

The first one bites every beginner. Both operands of / are int, so C performs integer division, fractional parts are discarded before the result gets stored into avg. To force float math, at least one operand must be a float.

Other quirks to expect:


'A' vs "A"

This trips up nearly everyone in their first week:

char  single = 'A';       // one character. Single quotes.
char *string = "A";       // a one-character string. Double quotes.

'A' is a single byte holding the value 65. "A" is two bytes, 'A' followed by a hidden '\0' (null terminator) marking the end of the string. We'll explore this fully in the Arrays and Strings lesson. For now: characters get single quotes, text gets double quotes.


Gotchas


TL;DR

This is just the start

Sign up free to track progress on this lesson, get coding problems with the Run button, and unlock the full interview Q&A.

More in Programming Fundamentals

What Is a Program?Operators
Input & Output
Conditionals
The switch Statement
while & do-while Loops