Question about variable definitions in functions.
Posted
by Kaan Tekelioglu
on Stack Overflow
See other posts from Stack Overflow
or by Kaan Tekelioglu
Published on 2010-04-01T20:06:08Z
Indexed on
2010/04/01
20:13 UTC
Read the original article
Hit count: 231
Hi.
#include <stdio.h>
main()
{
int a;
for(a=1; a<=4 && printf("%d ",a); a++)
{
int a;
static int b=a;
printf("%d ",(a++)-b);
}
getchar();
getchar();
}
In this code, the printout is 1 0 2 1 3 2 4 3. I understand why the int a; part works differently then the int a which was defined outside the for function, and why static int b; is only defined once with the primary value of a ; but why does the (a++) part in printf affect proceeding values of a? Don't we redefine int a; each time the for function runs? Thanks in advance.
© Stack Overflow or respective owner