The idea of this exercise was to show the effect of variable scope and the notion of name collision. You can have several x
active in your program, and the notion of visibility of a variable --- scope, Scope --- is what you try to show with your program.
I will rewrite your program and try to explain. But first I will write about your program
your original program
Although your small program has several problems beyond what can lead to your doubt, and below is the original with numbered lines. Here are some notes and when the case is the line number will be between [ ]
.
1 #include<stdio.h>
2 #include<stdlib.h>
3 int x;
4 int param( ){
5 x = x + 10;
6 return x;
7 }
8 int main(){
9 int y;
10 printf("Digite um numero: ");
11 scanf(" %d", &x);
12 y = param();
13 printf("\nO valor de y e %d \n", y);
14 printf("\nO valor de x e %d \n", x);
15 system("pause");
16 }
- Use some alignment in your code. It helps you and others who come to read your program
- every C program begins with
main()
and if you expect to see this early on, especially if it’s a program that you didn’t write. In general main()
is even in a separate file, for reasons that will soon learn. Use only prototypes at the beginning.
- [4] if your function does not receive parameters indicate this to the compiler, declaring
void
as in
int param(void){
- [8] the prototype of
main()
is
int main(int argc, char** argv);
If you will not use the declare parameters as well main(void)
- [11] Never stop reading the return of
scanf()
. Your program reads a single number. If scanf()
not read anything, what is the purpose of following the program?
- [11] What is the purpose of reading a value if nothing adds to your program? It only takes longer to test. Why not use something like
x = 200;
and move on? What difference does it make what value the user will enter if you will only add up 10 and see if it changes?
- [15] do not use
system()
. You will not be doing anything special, nor learning anything. system() just passes a command to the shell run. And is insecure and prohibited in many circles.
- [15] Do not use
pause
to stop the program. If you do this because of some IDE simply switch to another that stops the program by itself while running on the console. For years we have had several quality and free Eids: change to a better one. There is no reason to use one with such a stupid disability and have to change programs for example before delivering, just for this problem. If your instructor or school uses an automatic test system your program may not work properly. And if your teacher runs the program in a terminal he’ll stand there at the end for no reason...
- do not test your programs just within the IDE. It is a special and less significant environment: the code generated is different, the behavior is different. Learn to test on the console or, better still, on Windows Terminal, if you use Windows. And with the production code, this Release code, which is smaller and much faster.
The same program realigned
your slightly altered program with some of the things I explained
#include<stdio.h>
#include<stdlib.h>
int x;
int param(void);
int main(void)
{
int y;
x = 200;
printf( "\nEm main() x vale %d\n", x);
y = param();
printf( "\nDepois de chamar a funcao:\n\n");
printf("O valor de y e %d \n", y);
printf("O valor de x e %d \n", x);
}
int param( )
{
x = x + 10;
return x;
}
The difference is small, but maybe you’ll see an easier way to read the program.
A way to run, using any version of gcc on Windows
PS C:\chp21-0203-array> gcc -o tst -Wall -std=c17 so0207.c
PS C:\chp21-0203-array> ./tst
Em main() x vale 200
Depois de chamar a funcao:
O valor de y e 210
O valor de x e 210
The notion of scope and a more convenient program for testing
Note that this 3-line C program
1
2 int x = 300;
3
Compile without problems:
PS C:\chp21-0203-array> cat p1.c
int x;
PS C:\chp21-0203-array> gcc -c -Wall -std=c17 p1.c
PS C:\chp21-0203-array>
What does it mean?
Only declares a variable int
by name x
and that can be used in other programs. It is valid for the whole P1.c file. The fact that there is nothing else in it is irrelevant to the compiler. But if you have more code within P1.c that variable x
will be set to the entire file. If you have multiple functions, whether or not you have main()
the variable will be set. It says that x
has global scope for P1.c.
In the case of your program, x
exists in param()
or in main()
and in any other code in the file.
But whether inside a function --- or even inside a loop for
--- you declare another x
this variable, this new x
overrides the initially declared value. When returning from the function or exiting the loop this value disappears and again the "global" value of x
.
a more expressive example
Let’s consider two files: P1.c
int x = 300; // [1]
// fim de p1.c
and so0207.c
#include<stdio.h>
#include<stdlib.h>
extern int x; // [2]
int param(void);
int param2(int);
int main(void)
{
int x = 223; // novo x em main()! [3]
int y;
printf( "\nEm main() x vale %d\n", x);
y = param();
printf( "\nDepois de chamar a funcao sem argumentos:\n\n");
printf("O valor de y e %d \n", y);
printf("O valor de x e %d \n", x); // [4]
y = param2(-2345);
printf( "\nDepois de chamar a segunda funcao com o argumento -2345:\n\n");
printf("O valor de y e %d \n", y);
printf("O valor de x e %d \n", x);
}
int param()
{
x = x + 10; // [5]
printf( "\nValor de 'x' ao retornar da funcao param(): %d\n", x);
return x;
}
int param2(int x)
{ // como declarou x ele vai se sobrepor ao valor global
// o valor de main() e local e nao seria visivel aqui de todo modo
x = x + 10; // [6]
printf( "\nValor de 'x' ao retornar da funcao param(int x): %d\n", x);
return x;
}
Compiling and running this:
PS C:\chp21-0203-array> gcc -o tst -Wall -std=c17 so0207.c p1.c
PS C:\chp21-0203-array> ./tst
The output, with numbered lines:
1
2 Em main() x vale 223
3
4 Valor de 'x' ao retornar da funcao param(): 310
5
6 Depois de chamar a funcao sem argumentos:
7
8 O valor de y e 310
9 O valor de x e 223
10
11 Valor de 'x' ao retornar da funcao param(int x): -2335
12
13 Depois de chamar a segunda funcao com o argumento -2345:
14
15 O valor de y e -2335
16 O valor de x e 223
And why after all?
- In line 2: in main x is worth 223. The x value declared in line with the comment
[2]
comes from outside, and comes from P1.c, of course. Worth 300 as it is in the listing above. But in main on line 11 appears another x
with the value of 223. This is the value vis;ivel in main and the previous value disappears.
- in row 4: in param the value of
x
is the global value, 300 plus the increment of 10 and this is what the function returns to main and thus the value of y
is 310. As the program came out of param()
all of param()
is gone, and the value of x
becomes the value of x
in main, 223. and this explains the lines 8 and 9 of the output.
- when calling param2(int) version with
param2(-2345)
this will be the value of x within param2 and so the value in row 11 is -2345 + 10
- And so when returning to main y worth -2335.
x
that was never changed in main remains clear worth 223
I used the x
in a different file only to show that even in this case the value is available in the program, but only if it is not hidden by any other variable with the same name.
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.
– Maniero