0
0
Cprogramming~15 mins

Scope of variables - Mini Project: Build & Apply

Choose your learning style9 modes available
Scope of variables in C
📖 Scenario: Imagine you are writing a small C program to understand how variables behave inside and outside functions.
🎯 Goal: You will create a program that shows how variables declared inside a function are different from variables declared outside it.
📋 What You'll Learn
Create a global variable named count with value 10
Create a function named showCount that declares a local variable named count with value 5
Inside showCount, print the local count
Outside the function, print the global count
💡 Why This Matters
🌍 Real World
Understanding variable scope helps avoid bugs when different parts of a program use the same names.
💼 Career
Knowing scope is essential for writing clear and correct code in any programming job.
Progress0 / 4 steps
1
Create a global variable
Create a global integer variable called count and set it to 10.
C
Need a hint?

Global variables are declared outside any function.

2
Create a function with a local variable
Create a function named showCount that declares a local integer variable called count and sets it to 5.
C
Need a hint?

Local variables are declared inside functions.

3
Print the local variable inside the function
Inside the showCount function, add a printf statement to print the local count variable with the text "Local count: %d\n".
C
Need a hint?

Use printf to show the value of the local variable.

4
Print the global variable outside the function
In the main function, call showCount() and then print the global count variable with the text "Global count: %d\n" using printf.
C
Need a hint?

Call the function first, then print the global variable.