Hi, I was reading "Let us C" by Yashwant Kanektar (Thanks for this great book from the recomended books thread!) when i came accross this problem:
predict the outcome:
When i compiled the program and run it, this was what came out:Code:#include <stdio.h> int i = 0; int main() { printf("\nmain's i = %d", i); i++; val(); printf("\nmain's i = %d", i); val(); } val() { i = 100; printf("\nval's i = %d", i); i++; }
main's i = 0
val's i = 100
main's = 101
val's i = 100
I thought the answer should have been
main's i = 0
val's i = 101
main's = 101
val's i = 100
i dont get the flow of the program. I mean, I understand that whenever val() is called, i resets to 100, however, I don't get why main becomes 101.
Plus, the book talks about "stack" which is responsible for the flow of control. Can someone explain to me what happens inside the computer (the flow of the program when the stacking system is implemented).
BTW: I have already read up on the internet about stack, I know that stack is LIFO (last in first out), sadly, I think the information I have obtained is still lacking.
In addition, I have already read about the different variable storage types.
I don’t get why the answer came at like that in the program.
Can anyone please explain this to me? Thanks



LinkBack URL
About LinkBacks



