0
0
Cprogramming~15 mins

Why variables are needed in C - See It in Action

Choose your learning style9 modes available
Why Variables Are Needed
📖 Scenario: Imagine you want to keep track of the number of apples you have in a basket. You need a way to store this number so you can use it later in your program.
🎯 Goal: You will create a simple C program that uses a variable to store the number of apples and then prints that number.
📋 What You'll Learn
Create an integer variable named apples and set it to 5
Create an integer variable named oranges and set it to 3
Add these two variables and store the result in a variable named total_fruits
Print the value of total_fruits using printf
💡 Why This Matters
🌍 Real World
Variables are like labeled boxes where you keep things you want to remember and use later, such as counting items or storing user input.
💼 Career
Understanding variables is essential for all programming jobs because they are the basic way to store and manipulate data in software.
Progress0 / 4 steps
1
Create variables for apples and oranges
Write two lines of code to create integer variables named apples and oranges. Set apples to 5 and oranges to 3.
C
Need a hint?

Use int to declare whole number variables and assign values with =.

2
Create a variable to hold the total fruits
Create an integer variable named total_fruits and set it to 0 before adding the fruits.
C
Need a hint?

Variables can be created and initialized before using them in calculations.

3
Add apples and oranges to get total fruits
Write a line of code to add apples and oranges and store the result in total_fruits.
C
Need a hint?

Use the + operator to add two variables.

4
Print the total number of fruits
Write a line of code to print the value of total_fruits using printf with the message: "Total fruits: " followed by the number.
C
Need a hint?

Use printf("Total fruits: %d\n", total_fruits); to print the message and number.