0
0
Cprogramming~20 mins

Why variables are needed in C - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Variable Mastery in C
Get all challenges correct to earn this badge!
Test your skills under time pressure!
๐Ÿง  Conceptual
intermediate
2:00remaining
Why do we use variables in C?

Which of the following best explains why variables are used in C programming?

ATo create permanent files on the computer automatically
BTo make the program run faster by skipping calculations
CTo store data values that can change during program execution
DTo display messages on the screen without using functions
Attempts:
2 left
๐Ÿ’ก Hint

Think about how programs remember information while running.

โ“ Predict Output
intermediate
2:00remaining
Output of variable usage in C

What will be the output of this C program?

C
#include <stdio.h>
int main() {
    int age = 25;
    age = age + 5;
    printf("%d", age);
    return 0;
}
A30
B25
C5
DError: variable age not initialized
Attempts:
2 left
๐Ÿ’ก Hint

Look at how the variable age changes before printing.

๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error with variable usage

What error will this C code produce?

C
#include <stdio.h>
int main() {
    int number;
    printf("%d", number);
    return 0;
}
AOutputs a random number
BOutputs 0
CRuntime error: division by zero
DCompilation error: variable 'number' used before initialization
Attempts:
2 left
๐Ÿ’ก Hint

Think about what happens when you print a variable without giving it a value first.

๐Ÿ“ Syntax
advanced
2:00remaining
Which variable declaration is correct?

Which of the following is a correct way to declare a variable in C?

Aint number = 'ten';
Bint number = 10;
Cint number-10;
Dint 1number = 10;
Attempts:
2 left
๐Ÿ’ก Hint

Remember variable names cannot start with a number and must match the data type.

๐Ÿš€ Application
expert
2:00remaining
How many variables are used in this program?

Consider this C program. How many variables are declared and used?

C
#include <stdio.h>
int main() {
    int a = 5;
    int b = 10;
    int c = a + b;
    printf("%d", c);
    return 0;
}
A1
B2
C4
D3
Attempts:
2 left
๐Ÿ’ก Hint

Count each variable declared with int.