+7 votes
2.6k views

The following program

main()
{
inc(); inc(); inc();
}
inc()
{
static int x;
printf("%d", ++x);
}
1. prints 012
2. prints 123
3. prints 3 consecutive, but unpredictable numbers
4. prints 111
| 2.6k views

3 Answers

+15 votes
Best answer

answer is B)

As static is implicitly initialized to 0

and inc() is called 3 times

hence output will be 1,2,3

by Veteran (50.9k points)
selected by
0
why is it picking up next value as 1 for the next increment function .Why it is not again starting with 0? please explain.
0
when the control goes to inc() function,

static is implicitly initialized default to 0

printf says ++x , so first increment will be there ,hence x = 1, will be printed....
0
I meant to ask in function main we have 3 inc() function rt? when 1 inc() will be called then increment will happen in value of x and it will become 1 .its done here now it turn for 2nd inc () which is going t be called by fn main now i am asking why this new function call not considering initial value as 0 and againn incrementing to 1 if we do like this o/p will be 111 .....this is i want you to explain

i understood you statement about static initialization ....
0
this is why we use static keyword , so that its value changes after each increment.

it is not constant, everytime we enter the function...

otherwise, we could have used only int , why static then....
0

@DUW  Get the difference Here .

Output

0
@kapil Am i right ?
0
@shekhar sir,

right one.

well explained !!
0
@Kapil and Shekhar sir , but here static variable declared in a function , So its scope and lifetime for that function only...So after  1st inc(); terminates , the memory location where 'x' is stored should also be destroyed and new x=0 will allcate for next inc() function... Please correct me if I'm wrong
0

@ankit "  but here static variable declared in a function , So its scope and lifetime for that function only."

The statement is not correct.

Because the scope of static variable is entire file i.e. value of static variable is retained even after the function call ends.So this the difference between local variable and local static variable.

See the difference:


#include<stdio.h>
int fun()
{
static int x = 0;
x++;
return x;
}

int main()
{
printf("%d ", fun());
printf("%d ", fun());
return 0;
}

So output:1,2

Now use normal local variable.

#include<stdio.h>
int fun()
{
int x = 0;
x++;
return x;
}

int main()
{
printf("%d ", fun());
printf("%d ", fun());
return 0;
}

So ouput :1,1

+1
ok sir now I understood... Thank u sir :)
0

its a static variable .... and static variables use the same copy of varible for  every function....

+2 votes
Answer (B)

x is static so during initialization it's value is 0,with every call it increases by 1

first call = 0+1 =1

second call= 1+1=2

third call = 2+1 = 3

so 123 is printed
by Loyal (9.9k points)
0 votes

Important points about the static variables:-

• They're initialised right at the start, and their value are stored inside the Runtime Environment.
• Once initialised, they're never initialised again.
• If in the beginning, value is not assigned for initialisation, then it is assigned 0 by default.
    static int x;


Value assigned 0 implicitly. So, x = 0.

Combine this fact with pre-increment, we get option B

by Loyal (6.4k points)
Answer:

+8 votes
5 answers
1
+10 votes
4 answers
2
+5 votes
3 answers
3
+8 votes
5 answers
4