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

C and C++ Interview Questions

7) Define and explain about! Operator?

Ans:
The logical operator! NOT is a unary operator that is used before a single operand. It returns the inverse value of the given operand so if the variable “c” had a value of true then! C would return value of false. The not operator is very much useful in C programs because it can change the value of variables with successful iterations. This ensures that on each pass the value is changed.

Ex - 1: What is the output?

main()
{
int i=5;
printf(“%d”,i=++i ==6);
}

Ans:
1

Explanation:
The expression can be treated as i = (++i==6), because == is of higher precedence than = operator. In the inner expression, ++i is equal to 6 yielding true(1). Hence the result.

Ex - 2: What is the output?

main()
{
int i = 3;
for (;i++=0;) printf(“%d”,i);
}

Ans:
Compiler Error: Lvalue required.

Explanation:
As we know that increment operators return rvalues and hence it cannot appear on the left hand side of an assignment operation.

Ex - 3: What is the output?

main()
{
int i=10,j=20;
j = i, j?(i,j)?i:j:j;
printf("%d %d",i,j);
}

Ans:
10 10

Explanation:
The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question can be written as:
if(i,j)
{
if(i,j)
j = i;
else
j = j;
}
else
j = j;


SLogix Student Projects
bottom