Variables
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
In C a variable must be declared with a type before it is used. The type sets how much memory it takes and what it holds:
int count = 0; // a declaration with initialisation
double price; // a declaration without a value
price = 9.99; // assignment
int a = 1, b = 2; // several at once
An important difference from Python: an uninitialised local variable contains garbage — whatever was in that memory before. The compiler isn't required to zero it.
int x;
printf("%d\n", x); // who knows what! Always initialise
A variable's type can't be changed: if count is declared as an int, it is an int forever.
Names: letters, digits, _, not starting with a digit; case matters. C uses the snake_case style: total_price, max_len.
Swapping values is a classic that in C is written with a temporary variable:
int tmp = a;
a = b;
b = tmp;
Task. Read two whole numbers a and b, swap the values of the variables and print a b.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.