recategorized by
729 views
0 votes
0 votes

i am getting 4,3,4,3,8,2 is it right?????

recategorized by

2 Answers

1 votes
1 votes

......

1 votes
1 votes
int a,b;   /* Here a,b are global variable whose inital value is 0 for both a,b*/
void f();
main()
{ 
    static int a =1;  /*static keyword is one time initialization, use many time*/
    f();
    a*=2;         /* line no 7 is same as a=a*2*/
    f();
    printf("%d%d",a,b);   /* printf function print the value a,b in main scope but here in main function b is not define so it will take value from global variable b */
}
void f()
{
    static int a =2; /*same use as line no 5 */
    int b =2  /* here b is define in the f function scope, it's not static so every time when b is initialized, it will assigned as b = 2 */
    a*=b++;   /* line no 15 can be written as a = a*b++ */
    printf("%d%d",a,b);   /* this printf print the values of a,b which is define in function f scope */
    
    
    
    
}
    

execution of program is as follow:

Line no 1 is executed first and assign the value 0 to both GV a,b. After that main function is executed.

In main function a is static variable assign the value 1, then call the function f().

In function F() a is static variable & b is normal variable, both assign the value 2. Line no 15 use 2 operator * and ++(post-increment). Here post-increment has high priority than * and associativity is R2L. This gives the value of a,b as 4,3.

after that comes to the main function & assigned the new value of a as 2, again call function f() and do the same things. static var a use current value 4 and variable b again initialized as 2, again execute line no 15 which gives the value of a,b as 8,3.

returning to the main function, printf function print a & b value, here b is not defined so it used the value of GV as 0  so printf gives 2,0.

so values of a & b are respectively according to their printf statements:

 

       a         b
       4        3
       8        3
       2        0

 

Related questions

0 votes
0 votes
0 answers
1
Tushar Kumar 1 asked Jul 20, 2018
206 views
How is IIIT Allahabad for mtech IT ? Which NIT’s are comparable to that ?
0 votes
0 votes
1 answer
2
Warlock lord asked Sep 12, 2017
394 views
I was referring to this question :https://gateoverflow.in/132053/self-doubt-in-linked-listAlthough I agree with option A (deletion) being better that singly linked list, ...
0 votes
0 votes
2 answers
3
viral8702 asked Sep 21, 2023
285 views
The Total Combinations Possible of Min heap with 8 Distinct elements are ?
0 votes
0 votes
1 answer
4
iamdeepakji asked Jan 27, 2019
278 views
If there is negative edge cycle then dijkstra algorithm will give correct path or not same thing about bellman ford also?Bellman ford always halts or not?