© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

11. what is the precedence of the logical operator?

Ans:
The logical opearot precedence is !,&& and ||.

Ex - 1: What is the output?

main()
{
printf("%x",-1<<4);
}

Ans:
fff0

Explanation :
-1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are filled with 0's.The %x format specifier specifies that the integer value be printed as a hexadecimal value.

Ex - 2: What is the output?
main()
{
int c=- -2;
printf("c=%d",c);
}

Ans:
c=2;

Explanation:
Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus= plus.
Note:
However you cannot give like --2. Because -- operator can only be applied to variables as a decrement operator (eg., i--). 2 is a constant and not a variable.

Ex - 3: What is the output?

main()
{
int i=10;
i=!i>14;
Printf ("i=%d",i);
}

Ans:
i=0

Explanation:
In the expression !i>14 , NOT (!) operator has more precedence than ‘ >’ symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).


SLogix Student Projects
bottom