int num1 = 40;
int num1
in newfun()
is the declaration of a new function-local variable with the same identifier name1
as the global variable. So you don't address the global variable with this statement.
Instead, you initialize the local variable num1
by the value of 40
, which is destroyed after the function has been executed since its storage class is auto
matic.
The global variable still contains the value of 10
as assigned in main()
and this is what is printed for it at the call to printf()
, which is completely correct.
Side Notes:
void main()
is not correct. You need to use at least int main()
or even better int main (void)
to provide a fully prototype.
If this is really the exact same example as in the book and it is saying anything else would be the output, you urgently need a better source of knowledge. F.e.: Modern C by Jens Gustedt.
Others and including this one, you can find in this amazing collection of books regarding C programming:
The Definitive C Book Guide and List