Showing posts with label static variables. Show all posts
Showing posts with label static variables. Show all posts

Sunday, January 10, 2010

#include
void func()
{
int x = 0;
static int y = 0;
x++; y++;
printf( "%d -- %d\n", x, y );
}

int main()
{
func();
func();
return 0;
}

What will the code above print when it is executed?

a) 1 -- 1
1 -- 1
b) 1 -- 1
2 -- 1
c) 1 -- 1
2 -- 2
d) 1 -- 0
1 -- 0
e) 1 -- 1
1 -- 2

Tuesday, May 26, 2009

State the output

#include
int a = 20;
void main()
{
fun1();
printf(" ");
fun1();
printf(" ");
fun1();
printf(" ");
}

fun1()
{
static int b = 10;
b++;
printf("%d", b++);
b=12;
printf(" ");
printf("%d", b++);
}

A 10 12 10 12 10 12
B 11 12 11 12 11 12
C 11 12 14 12 14 12
D Error "static variable re-assignment"