c operator question
TRANSCRIPT
-
7/27/2019 C Operator Question
1/6
C operator questions with answers
(1)
What will be output of the following program?
#includeintmain(){ floata=0.7; if(a
-
7/27/2019 C Operator Question
2/6
What will be output of the following program?#include
intmain(){ inti=5,j; j=++i+++i+++i; printf("%d %d",i,j); return0;}
Output:
Turbo C++ 3.0: 8 24
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Explanation:
Rule :- ++ is pre increment operator so in any arithmetic
expression it first increment the value of variable by
one in whole expression then starts assigning the finalvalue of variable in the expression.
Compiler will treat this expression j = ++i+++i+++i; as
i = ++i + ++i + ++i;
Initial value of i = 5 due to three pre increment
operator final value of i=8.
Now final value of i i.e. 8 will assigned to each
variable as shown in the following figure:
So, j=8+8+8
j=24 and
i=8
---------------------------------------------------------
-
7/27/2019 C Operator Question
3/6
(5)
What will be output of the following program?
#include
voidmain(){ intx; x=10,20,30; printf("%d",x); return0;}
Output:
Turbo C++ 3.0: 10
Turbo C ++4.5: 10
Linux GCC: 10
Visual C++: 10
Explanation :
Precedence table:
Operator Precedence Associative
= More than , Right to left, Least Left to rightSince assignment operator (=) has more precedence than
comma operator .So = operator will be evaluated first
than comma operator. In the following expression
x = 10, 20, 30First 10 will be assigned to x then comma operator will
be evaluated.
-------------------------------------------------------
(9)
What will be output of the following program?
#include
intmain(){
intx=100,y=20,z=5; printf("%d %d %d");
-
7/27/2019 C Operator Question
4/6
return0;}
Output:
Turbo C++ 3.0: 5 20 100
Turbo C ++4.5: 5 20 100
Linux GCC: Garbage values
Visual C++: 5 100 20
By default x, y, z are auto type data which are stored in
stack in memory. Stack is LIFO data structure. So in
stack first stores 100 then 20 then 5 and program counter
will point top stack i.e. 5. Default value of %d inprintf is data which is present in stack. So output is
revere order of declaration. So output will be 5 20 100.
---------------------------------------------------------
(11)
What will be output of the following program?
#include
intmain(){ inta; a=sizeof(!5.6); printf("%d",a); return0;}
Output:
Turbo C++ 3.0: 2
Turbo C ++4.5: 2
Linux GCC: 4Visual C++: 4
Explanation:
! is negation operator it return either integer 0 or 1.
! Any operand = 0 if operand is non zero.
! Any operand = 1 if operand is zero.
So, !5.6 = 0
Since 0 is integer number and size of integer data type
is two byte.
--------------------------------------------------------
-
7/27/2019 C Operator Question
5/6
(12)
What will be output of the following program?
#include
intmain(){ floata; (int)a= 45; printf("%d,a); return0;}
Output:
Turbo C++ 3.0: Compilation error
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Explanation:
After performing any operation on operand it always
return some constant value.
(int) i.e. type casting operator is not exception for
this. (int) a will return one constant value and we
cannot assign any constant value to another constant
value in c.
(int)a = 45; is equivalent to
3456 = 45 ( Here 3456 in any garbage value of int(a)).
--------------------------------------------------------
(3)
What will be output of the following program?
#include
intmain(){ inti=1; i=2+2*i++; printf("%d",i); return0;}
Output:
Turbo C++ 3.0: 5
Turbo C ++4.5: 5
-
7/27/2019 C Operator Question
6/6
Linux GCC: 5
Visual C++: 5
Explanation:i++ i.e. when postfix increment operator is used any
expression the it first assign the its value in the
expression the it increments the value of variable by
one. So,
i = 2 + 2 * 1
i = 4
Now i will be incremented by one so i = 4 + 1 = 5
---------------------------------------------------------