HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to CVariablesData TypesOperatorsStringsArraysConditions (if / switch)Loops (for / while)FunctionsPointers
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
👉 Pointer
C›Variables

Variables

int, double, char types; const constants and format specifiers.

Simple enough for a 5-year-old to understand

Typed variables

Example code
1
#include <stdio.h>
2
int main() {
3
int age = 36;
Integer.
VariableInteger.
4
double height = 1.72;
Decimal.
VariableDecimal.
5
char letter = 'A';
Single character (single quotes!).
VariableSingle character (single quotes!).
6
printf("%d %.2f %c\n", age, height, letter);
36 1.72 A
Output / Input36 1.72 A
7
return 0;
8
}
VariableOutput / Input
💡 Hint: char is written with single quotes ('A'), text with double quotes ("Alex").

About this topic

When declaring a variable in C, you must specify its type. The reason for this is that the compiler needs to know how many bytes to allocate in memory: four bytes for an integer, one byte for a character. In Python, this information is kept at runtime, while in C, it is fixed at compile time.

The consequence of this decision is that the type of a variable in C cannot be changed later. You cannot assign text to a variable defined as an int. This is called static type checking, and it catches most errors before the program runs.

This is where C's most dangerous behavior emerges: a local variable to which you haven't assigned a value is NOT zero. It contains whatever happened to be in memory at that moment. The program may produce a different result every time it runs, and this is the hardest type of bug to find.

What you will learn

  • That variable declaration requires a type
  • Why unassigned variables are dangerous
  • defining constants with const
  • Naming rules and case sensitivity

When to use?

Use const if the value will not change. The compiler will report accidental assignments as errors, and anyone reading the code will see that the value is constant.

Naming magic numbers instead of hardcoding them into the code both improves readability and allows you to change the value from a single place.

Common mistakes

Wrong

#include <stdio.h>

int main() {
    int total;
    total = total + 10;
    printf("%d\n", total);
    return 0;
}

total was never initialized. It contains a random value left over from memory, so the result may change on every run. When the GCC warning option is enabled, it gives the warning "'total' is used uninitialized". Local variables are not automatically initialized to zero.

Correct

#include <stdio.h>

int main() {
    int total = 0;
    total = total + 10;
    printf("%d\n", total);
    return 0;
}

Wrong

#include <stdio.h>

int main() {
    int count = 5;
    printf("%d\n", Count);
    return 0;
}

C is case-sensitive: count and Count are two different names. The compiler gives the error "'Count' undeclared". This is the most common typo in C and happens more frequently as variable names get longer.

Correct

#include <stdio.h>

int main() {
    int count = 5;
    printf("%d\n", count);
    return 0;
}

Wrong

#include <stdio.h>

int main() {
    const int LIMIT = 100;
    LIMIT = 200;
    printf("%d\n", LIMIT);
    return 0;
}

A variable defined with const cannot be reassigned; the compiler gives the error "assignment of read-only variable 'LIMIT'" . This is not a restriction but a protection: you told the compiler that the value shouldn't change, and it protected you from your own mistake.

Correct

#include <stdio.h>

int main() {
    const int LIMIT = 100;
    int current = 200;
    printf("%d %d\n", LIMIT, current);
    return 0;
}

Frequently asked questions

Can I define the variable close to where I use it?

Yes. The old C standard required all declarations to be at the beginning of a block, but in standards after 1999, you can define a variable right where it is first used. This produces more readable code because the distance between declaration and usage is shortened.

Why do I need to give an initial value to every variable?

Because in C, if you don't, it reads whatever is in memory. This type of bug is insidious: your program might work correctly on your computer and give incorrect results on another machine. Make it a habit to initialize every variable the moment you declare it.

What characters can I not use as variable names?

Names cannot start with a digit, cannot contain spaces or Turkish characters, and cannot be the same as keywords like int or return. Names starting with an underscore are technically valid, but you should avoid using them because the standard library reserves them for itself.

Summary

The type of every variable must be specified when it is declared, and this type cannot be changed later.

Local variables are not automatically zeroed out; reading them without initialization is undefined behavior.

const informs the compiler that the value will not change.

Names are case-sensitive.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code