in Programming
131 views
0 votes
0 votes

what is the output of the program?

#include<stdio.h>
int main(){
    
    int p=10;
    p<<2>>1;
    printf("%d",p);
    return 0;
    
}

doubt - shift operator has the same precedence so its evaluation done by according to associativity and associativity of shift operator is left to right then how it gives the result 10.
 

in Programming
131 views

1 comment

p << = 2 ====> p = p << 2

but you written, p << 2 ===> it is stored in temporary variables in stack.

∴ you didn't modify the p, it will give it's initial value only.

 

approaches 1 :-

if you assign the final value to some variable and print it, then it is 20.

#include<stdio.h>
int main()

{
    
    int p=10;
    int t = p<<2>>1;
    printf("%d",t);
    return 0;
    
}

 

approaches 2 :-

#include<stdio.h>
int main()

{
    
    int p=10;
    int t = ;
    printf("%d",p<<2>>1 );
    return 0;
    
}

 

approaches 3 :- ( In this case, the value of p is updated, in approach 1 and approach 2 value of p doesn't update. )

#include<stdio.h>
int main()

{
    
    int p=10;
    p<<=2>>1;
    printf("%d",t);
    return 0;
    
}

But Note that the execution of the  p <<= 2 >> 1 statement is like

p = p << ( 2 >> 1)

which is equivalent to p = ( p << 2 ) >> 1.

0
0

Please log in or register to answer this question.

Related questions