Originally Posted by
mousanony
I mean, I understand that whenever val() is called, i resets to 100, however, I don't get why main becomes 101.
Observe the i++; statement in val().
By the way, you should indent the code. The val function should also be forward declared with a void return type, and you should specify void as the parameter list to indicate that it takes no arguments rather than an unknown number of arguments, although that is not a problem here:
Code:
#include <stdio.h>
void val(void);
int i = 0;
int main(void)
{
printf("\nmain's i = %d", i);
i++;
val();
printf("\nmain's i = %d", i);
val();
return 0; /* optional in C99 */
}
void val(void)
{
i = 100;
printf("\nval's i = %d", i);
i++;
}
That said, maybe this can be a lesson to you as to why you should avoid global variables: they can make it more difficult for you to reason about your program.