edited by
585 views
2 votes
2 votes

What will be the output of the following C program:
void print1(void)

{

static int x=10;

x+=5;

printf("%d",x);

}

void print2(void)

{

static int x;

x=10;

x+=5;

printf("%d",x);

}

int main()

{

print1();

print1();

print2();

print2();

return 0;}

  • 15, 20, 25, 30
  • 15, 20, 15, 20
  • 15, 15, 15, 15
  • None of these
edited by

1 Answer

Best answer
3 votes
3 votes

yes answer is none of above answer should be 

15,20,15,15....

solution:

print 1 initialize an static variable x=10;...now we add 5 ...x=15 ...PRINTED= 15 ...

next print 1 .....x=15(static are declared once ) now we add 5 hence x=20..PRINTED=20;

next print 2    x=20 but we overwrite it by 10 and add 5 hence x=15..PRINTED =15....

next print 2 x=15 but again written by 10 and we add 5 hence x=15 PRINTED=15....

output expected=15,20,15,15

selected by

Related questions

1 votes
1 votes
1 answer
2
Na462 asked Jan 8, 2019
1,402 views
#include <stdio.h>main (){unsigned x = -10;int X = 20;if (X x) printf ("Hello");else{ printf ("%d",x); printf ("Jello"); }}
0 votes
0 votes
0 answers
3
aimhigh asked Jan 8, 2019
1,235 views
#include<stdio.h>void main(){int p=-8;int i= (p++,++p);printf("%d\n",i);}
0 votes
0 votes
0 answers
4