main creates its own x
This x belongs to main's scope.
x5
C programming · Lesson
Compare two variables with the same name that belong to different frames.
incrementa's parameter x is distinct from main's x: changing the local copy does not automatically change the caller's variable.
main x=5 → incrementa x=5 → local x=6 → return 6 → main x stays 5
For functions the flowchart shows the algorithmic effect. The Call Stack Explorer below shows what really happens between main, parameters, frames and return.
Move through the steps: frames show which variables exist at that moment and the source highlights the involved lines.
This x belongs to main's scope.
x5
Both variables are named x, but they live in different frames.
x5
x5
x = x + 1 changes the function's x; main's x remains 5.
x6
x5
The value 6 is assigned to y. main's x is still 5.
x5
y6
Run C executes only the example shown on the left, using stdin on the right. The coding challenge below has a separate editor and test button.
1#include <stdio.h>
2
3int incrementa(int x)
4{
5 x = x + 1;
6 return x;
7}
8
9int main(void)
10{
11 int x = 5;
12 int y = incrementa(x);
13 printf("%d\n", x);
14 printf("%d\n", y);
15 return 0;
16}
Write incrementa(int x) returning x+1. Read x, call it and print the returned value then the unchanged x.
This is a different program from lab 1. Complete the starter code: Check all test cases compiles this editor and tests every input, including boundary cases.
After y = incrementa(x), what is main's x?
Challenge 1 / 5 · Understanding
Progress: 0 / 5 · correct: 0
ConceptStep separates what is required from what is recommended or useful as reinforcement. Only required prerequisites block your progress.
Relationships: solid line = required · dashed line = recommended · dotted line = reinforcement.