## INDEX

### C operator questions with answers

C programming tricky objective type operators questions and answers with explanation for written test and interview

(1)
What will be output of the following program?

#include<stdio.h>
int main(){
float a=0.7;d
if(a<0.7){
printf("C");
}
else{
printf("C++");
}
return 0;
}

## Explanation

Output:

Turbo C++ 3.0: c

Turbo C ++4.5: c

Linux GCC: c

Visual C++: c

Explanation:

The binary value of the double constant 0.7 in 64 bits is 0.1011001100110011001100110011001100110011001100110011. However, when assigned to a floating-point variable 'a' with only 32 bits, 'a' will hold the truncated value 0.10110011001100110011001100110011. As a result, 'a' is less than the original double constant 0.7.

This highlights the impact of precision limitations when representing floating-point numbers with fewer bits than their original precision.

### Hide

(2)
What will be output of the following program?

#include<stdio.h>
int main(){
int i=5,j;
j=++i+++i+++i;
printf("%d %d",i,j);
return 0;
}

## Explanation

Output:

Turbo C++ 3.0: 8 24

Turbo C ++4.5: Compilation error

Linux GCC: Compilation error

Visual C++: Compilation error

Explanation:

The expression j = ++i+++i+++i; is equivalent to i = ++i + ++i + ++i;. The initial value of 'i' is 5, and due to the three pre-increment operators, the final value of 'i' becomes 8. This final value is then assigned to the variable 'j', not 'i'. Each pre-increment operation increments 'i' before its value is used in the expression.

So, the result is that 'j' will be assigned the value of 'i' after the three increments, and 'i' will be 8.

So, j=8+8+8
j=24 and
i=8

### Hide

(3)
What will be output of the following program?

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

## Explanation

Output:

Turbo C++ 3.0: 5

Turbo C ++4.5: 5

Linux GCC: 5

Visual C++: 5

Explanation:

When the postfix increment operator (i++) is used in an expression, it first assigns the current value of the variable in the expression and then increments the variable by one.

In the expression i = 2 + 2 * 1, the initial value of 'i' is 2. After the expression is evaluated, 'i' becomes 4. Subsequently, the postfix increment is applied, resulting in 'i' being incremented to 5.

### Hide

(4)
What will be output of the following program?

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

## Explanation

Output:

Turbo C++ 3.0: 0

Turbo C ++4.5: 0

Linux GCC: 0

Visual C++: 0

Explanation:

== is a relational operator that returns:

• 1 if the comparison a == b is true
• 0 if the comparison a == b is false

Given the values a = 2 and b = 7, the expression a == b is false (2 is not equal to 7). Therefore, b is assigned the value 0.

### Hide

(5)
What will be output of the following program?

#include<stdio.h>
void main(){
int x;
x=10,20,30;
printf("%d",x);
return 0;
}

## Explanation

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 right
Because the assignment operator (=) has higher precedence than the comma operator, the expression x = 10, 20, 30 is evaluated as follows: first, 10 is assigned to 'x', and then the comma operator is evaluated.

### Hide

(6)
What will be output of the following program?

#include<stdio.h>
int main(){
int a=0,b=10;
if(a=0){
printf("true");
}
else{
printf("false");
}
return 0;
}

## Explanation

Output:

Turbo C++ 3.0: false

Turbo C ++4.5: false

Linux GCC: false

Visual C++: false

Explanation:
The '=' operator is an assignment operator, and 'a = 0' assigns zero to the variable 'a'. In C, zero represents false, and any non-zero number represents true. Therefore, 'if(0)' always evaluates to false, and the code inside the else block will execute.

### Hide

(7)
What will be output of the following program?

#include<stdio.h>
int main(){
int a;
a=015 + 0x71 +5;
printf("%d",a);
return 0;
}

## Explanation

Output:

Turbo C++ 3.0: 131

Turbo C ++4.5: 131

Linux GCC: 131

Visual C++: 131

Explanation:

The octal number 015 represents $5×{8}^{0}+1×{8}^{1}=5+8=13$ in decimal. Similarly, the hexadecimal number 0x71 translates to $1×1{6}^{0}+7×1{6}^{1}=1+112=113$ in decimal. Consequently, the expression $�=13+113+5$ yields a value of 131.

### Hide

(8)
What will be output of the following program?

#include<stdio.h>
int main(){
printf("%d %d %d",sizeof(3.14),sizeof(3.14f),sizeof(3.14L));
return 0;
}

## Explanation

Output:

Turbo C++ 3.0: 8 4 10

Turbo C ++4.5: 8 4 10

Linux GCC: 8 4 12

Visual C++: 8 4 8

Explanation:

The floating-point constant 3.14f has a size of 4 bytes. The double constant 3.14 (default) has a size of 8 bytes. The long double constant 3.14L has a size of 10 bytes. The sizeof() operator, a keyword in C, returns the size of the data type specified inside the parentheses.

### Hide

(9)
What will be output of the following program?

#include<stdio.h>
int main(){
int x=100,y=20,z=5;
printf("%d %d %d");
return 0;
}

## Explanation

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, variables x, y, and z are of auto type, stored in the stack, which is a Last In First Out (LIFO) data structure. The stack first stores 100, then 20, and finally 5. The program counter points to the top of the stack, which is 5. The default value of %d in printf is the data present in the stack. Therefore, the output is in reverse order of declaration, resulting in the output: 5 20 100.

### Hide

(10)
What will be output of the following program?

#include<stdio.h>
int main(){
int a=2;
a=a++ + ~++a;
printf("%d",a);
return 0;
}

## Explanation

Output:

Turbo C++ 3.0: -1

Turbo C ++4.5: 0

Linux GCC: 0

Visual C++: 0

Explanation:
Same theory as question (2) and (13).

### Hide

(11)
What will be output of the following program?

#include<stdio.h>
int main(){
int a;
a=sizeof(!5.6);
printf("%d",a);
return 0;
}

## Explanation

Output:
Turbo C++ 3.0: 2
Turbo C ++4.5: 2
Linux GCC: 4
Visual C++: 4

Explanation:

The negation operator (!) in C returns an integer value of either 0 or 1. The expression !5.6 is evaluated as follows:

• 1. If the operand is non-zero, ! returns 0.
• 2. If the operand is zero, ! returns 1.

In this case, !5.6 results in 0 because 5.6 is non-zero. It's important to note that the result is of integer type, and the size of the integer data type in this context is typically 4 bytes, not 2 bytes.

### Hide

(12)
What will be output of the following program?

#include<stdio.h>
int main(){
float a;
(int)a= 45;
printf("%d,a);
return 0;
}

## Explanation

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)).

### Hide

(13)
What will be output of the following program?

#include<stdio.h>
int main(){
int i=5;
int a=++i + ++i + ++i;
printf("%d",a);
return 0;
}

## Explanation

Output:

Turbo C++ 3.0: 21

Turbo C ++4.5: 21

Linux GCC: 22

Visual C++: 24

Explanation:
Rule : ++ (in ++i) is pre increment operator so in any arithmetic expression it first increment the value of variable by one in whole equation up to break point then start assigning the value of variable in the equation. There are many break point operators in. For example:

(1) Declaration statement.
(2) && or operator.
(3) Comma (,) operator etc.

In the following expression:
int a=++i + ++i + ++i;

Here break point is due to declaration .It break after each increment i.e. (initial value of i=5) after first increment value 6 assign to variable i then in next increment will occur and so on.
So, a = 6 + 7 + 8;

### Hide

Looping questions

If you have any queries or suggestions in above C operator question with answers, please suggest us. Anonymous said...

The Questions was gud we require more tough Questions Anonymous said...

please clearly explain me how the output
for question 10 is 0 or -1 ......... Anonymous said...

plz xplain 13 question

Unknown said...

Hiiiii,
Ques no 2 & 13 show contradictory concepts.
a = 2;
d = ++a + ++a + ++a;
printf("d: %d",d);
O/P:Ques_02:- 15
O/P:Ques_13:- 12

why and how ?????
Plz clear this doubt........

Thanks:-)

Unknown said...

can we add address of any variable we like inside the pointer. If so then we cud edit the value of any memory address we like.

Jhon said...

when we write the c program as:
main()
{
clrscr();
int a=10;
printf("output%d",a);
}
here generate error in clrscr() function.please tell me deeply why generate error..

Priyanka kumari said...

Hi Sanjiv,

In C language declaration statement must before the definition.

so correct way to write it:

main()
{
int a=10;
clrscr();

printf("output%d",a);
}

DATTATRAYA OZARKAR said...

can any one tell me logic for exchange of two number using only 2 variable

anilgontla said...

swaping without using third variable

int a,b;
a=a+b;
b=a-b;
a=a-b;
now a and b are swaped

Abul said...

Can anyone explain the difference between the question 2 & 13? I could not understand why the difference is there ?

Pl explain me Suresh said...

In tubo c 3.0,The answer for question 13 is 24 only because the a value incremented by 3times before you run the program and the final value 8 will be stored in the variable a;so j=++i + (++i) + (++i)8+8+8=24. Mukesh said...

in example 13, instead of writing
int a=++i+++i+++i, we declare a separately, we would get same answer as in question 2...i.e.
first declare int a;
then write the expression
a=++i+++i+++i; Anonymous said...

a=a++ + ~++a; /* here a=2;
in the case of post increment 1st a++ not assign...and ++a comes to picture and final value of a=3;
that a=3 assign to a++ and ++a.
so
a=3 + ~3;
a=-1;
and than after increment of a++ so a=0;
ans is a=0. Anonymous said...

I Like It......... GP

hafeehathim said...

Hi,
There are many break up points in C. Here the break up point is declaration..
see this example...
int a=2,d;
d=++a + ++a + ++a;
printf("%d",d);
Output: 5+5+5=15 (d is declared first and then value is assigned)

int a=2;
int d=++a + ++a + ++a;
printf("%d",d);
Output: 3+4+5=12 (d is declared and assigned in a single statement)

hafeehathim said...

Hi,
the answer for question 13 is 21. because the variable is declared and assigned in a single statement. here the break up point is declaration. Anonymous said...

hey how it becomes a=3 + ~3= -1 it is 0

Unknown said...

question no.9
What will be output of the following program?

#include
int main(){
int x=100,y=20,z=5;
printf("%d %d %d");
return 0;
}
ans
the given solution of this question is wrong
actually during the run of the program it'll give garbage value not correct ans Anonymous said...

no no.. its is not zero.. ~3=-4 it returns the 2's cmpliment

Unknown said...

explain 2 and 13...i got confused

rakesh said...

Hi Ritu,
Actually the solution i.e output is compiler dependent.. i am using turbo C compiler ... and the solution is correct ... it prints whatever is recently present on the top of the stack... Anonymous said...

int a=10;
int b=20;
a=a+b;
b=a-b;
a=a-b;
Printf("%d\n %d\n",a,b); Anonymous said...

no no ....
a=2 + ~3 (in preincrement a will be used first then will be incremented )
a=2 + -4
a= -2
now we will increment a by 1 as I stated above
a= -2 +1
a=-1 Anonymous said...

20
10 Anonymous said...

Please explain again 2 and 13 Anonymous said...

Q. no.1
compilation error: d undeclared
line 3 : float a=0.7;d Anonymous said...

Sir, please give some problem on Bitwise Operators..

Unknown said...

what does its means ~3 and << >> in c language

MAHENDRA KALSANGRA said...

how to find binary value of 32bit OR 64bit number..?
in above how to write .7 in 64 bit binary number...? Anonymous said...

Arithmetically :
~a = -a -1

sophitorium engineering college said... Anonymous said...

Abdullah Naeem said...

Suresh ...
in code :: block 12.11(Application)
The answer for question 13 ... only because the a value incremented by 3or2 times before you run the program and the final value 8 will be stored in the variable a; but j=++i + (++i) + (++i)8+8+8=22 but not 24
that why tell me it?

Unknown said...

When I write this code in Turbo c++ 4.5 compiler will display 8 , but i cannot understand .. please help>>
code :
# include
void main()
{
int a=2;
a=++a + ++a;

printf("%d",a);
} Manav said...

.7x2=1.4 (1)
.4x2=.8 (0)
.8x2=1.6 (1)
.6x2=1.2 (1)
.2x2=.4 (0)
.4x2=.8 (0)
.8x2=1.6 (1)
.6x2=1.2 (1)
.
.
.
so on

.7 binary will be = 0.1011 0011.......

emon said...

how can i find ceiling and floor without using library funtion ,u can use conditional operator

emon said...

#include
int max_(int x,int y);

int main ( )
{
int a,b,c,d;
scanf("%d%d%d%d", &a, &b, &c, &d);
int z=max_(a,b);
int m=max_(z,c);
int n=max_(m,d);
printf("max number is : %d",n);
return 0;
}

int max_(int x,int y)
{

int z=(x>y) ? x : y ;
return z;

}
pls fulfill this program,i cant get the output of ceiling .i want output ceiling without using library funtion

Unknown said...

a=++a + ++a
a= 4 + 4
a= 8 Anonymous said...

int a=1,b=0,c;
c= b++ || a++ && --a;
printf("%d %d %d ",a,b,c);

plz help me in this program...

Unknown said...

negative value of3 it mean -3

Unknown said...

i has a break point so it increment imeediately
a=++a + ++a
a=3 + 4// there is break point so it inc immediately after 3 to 4
a=7

Unknown said...

a=1
b=1
c=1

Koushik Ahmed said...

hafeehathim@thanks good point

eric948470 said...

For question 3, I got 4 as the output, compiling with gcc.

eric948470 said...

For question 10, I got -3 as the output, compiling with gcc, which is different from the given answer.

Unknown said...

These are all called bitwise rightshift, leftshift and complement operators. if u want to perform operation on this if u want to convert to binary then only it is possible

Unknown said...

These are all called bitwise rightshift, leftshift and complement operators. if u want to perform operation on this if u want to convert to binary then only it is possible

Unknown said...

For question 3, I got 4 as the output check it

Unknown said...

For question 3, I got 4 as the output check it

nimi said...

why answer of 2 and 13 different

Unknown said...

int t,a=10,b=20;

t=a;
a=b;
b=t;
print the values of a and b then also we get the values swapped
this is with help of third variable

Unknown said...

what will be value of i
i=1;
i=i+++++i;

Unknown said...

int a=20,b=30;
a=a^b; //a=20,b=30
after bitwise ex-or a wil become 10
b=a^b; //a=10,b=30
after bitwise ex-or b wil become 20
a=a^b; //a=10,b=20
after bitwise ex-or a wil become 30

Unnikrishnan.R said...

because its mandatory in c to declare varibles first
main()
{
int a=10;
clrscr();
printf("output%d",a);
}

Jitendra Kumar said...

Good Questions!
What will be the value of the given expression:
s = ++a - ++a + a-- - ++a where a = 1

Tips by IITian on Personal Finance Management said...

plzzzz plzz can anyone give a detailed and easy solution of Q.2 and Q.13

Unknown said...

in gcc int i=2;
i=i++ + ~++i;// 2 + -5(binary"100""sign 1's compliment is 1011 so -8+3=-5")
o/p -3

rajkamal said...

@ tejasvi
only two variables should be used not three variables.....

Unknown said...
This comment has been removed by the author.
Unknown said...

ansr 12 is rit one.....becoz pre increament opr fst increase the value of the varriable and the assign the value one bye one so thts why 2x is rit ansr............initially i=2 when first ++i occur thn it increase the value 3 and assign the value 3 in equation and after agggin it increase in sme scenario and assign the value 4 and then 5 thn after we get 12................

Unknown said...

a= ++i + ++i + ++i
associativity of ++ operator is right to left, hence, a= ++i + (++i + ++i)

inside brackets, both operators are incremented before adding, hence totally, it is incremented twice and becomes 7
a=++i + (7+7)
a= ++i + 14

Now i is incremented again and becomes 8

a=8+14
a=22
:)

Unknown said...
This comment has been removed by the author.
Unknown said...

Can someone please explain how we get 0 for Question 10 in linux?

Unknown said...

can any one please explain below code:

#include
int main(){
int a=2;
a=a++ + ~++a;
printf("%d",a);
return 0;
}

Unknown said...

In question number 2, as per the explanation given the value of j comes out to be 24,but when i am executing the same code,I am getting answer as 22,i.e. the value of j as 22.

(j=++i + ++i + ++i;)

mehul said...

what is lvalue required ? can anyone tell it?

Unknown said...

question no:- 3 is giving output 4 when run on linux gcc. and I feel increment is done just after it places value in expression and before final '=' operation is done.

Unknown said...

What are the application of 1's compliment and 2's compliment in the operator related questions.. I little bit confused in applications.

Unknown said...

Explanation for question 10 for gcc linux.

int a = 2;
a = a++ + ~++a;
1 = 2 + 3
let's name the operands 1, 2 & 3 respectively.

Now, a = 2 {Initially}
operand 2 is post increment & operand 3 is pre increment.
So value of a at operand 2 & 3 becomes 3

Now, expression becomes
a = 3 + ~3;

Now ~3 = -4

Therefore, expression becomes
a = 3 + -4;
a = 3 - 4;
so a = -1;
Since there was a post increment operator in operand 2, now the value of a will be incremented by 1.
So, a = 0 (Final Answer)

Unknown said...

Plz explain Q-12