Output of C programs | Set 49 (Operators)

Prerequisite : Operators in C

1. What will be the output of the following program?

 `#include ` `int` `main(``void``) ` `{ ` `    ``int` `i = 40 >> 5 << 3 >> 2 << 1; ` `    ``printf``(``"%d"``, i); ` `    ``return` `0; ` `} `

Options:

```1. 4           2. 0
3. 40          4. 1
```
`Answer : (1)`

Explanation: The answer is option(1). Here first 40 >> 5 means 40 / 32 which is 1, then 1 2 brings 2, then 2 << 1 becomes 4.
For details refer bit shift operators

2. What will be the output of the following program?

 `#include ` `int` `main(``void``) ` `{ ` `    ``int` `i = 10 > 9 > 7 < 8; ` `    ``printf``(``"%d"``, i); ` `    ``return` `0; ` `} `

Options:

```1. 1           2. 20
3. 10          4. 0
```
`Answer : (1)`

Explanation: The answer is option(1). Here 10 > 9 returns 1, then 1>7 returns 0 then 0<8 returns 1.

3. What will be the output of the following program?

 `#include ` `int` `main(``void``) ` `{ ` `    ``int` `x = 4, y = 4, z = 4; ` `    ``if` `(x == y == z) { ` `        ``printf``(``"Hello"``); ` `    ``} ``else` `{ ` `        ``printf``(``"GEEKS"``); ` `    ``} ` `    ``return` `0; ` `} `

Options:

```1. Hello           2. 0
3. 1               4. GEEKS
```
`Answer : (4)`

Explanation : The answer is option(4). Here x == y comparison gives 1 and the returned 1 is compared with z which is false and returns 0. Therefore it prints GEEKS.

4. What will be the output of the following program?

 `#include ` `int` `main(``void``) ` `{ ` `    ``int` `x = 10, y = 15; ` `    ``x ^= y ^= x ^= y; ` `    ``printf``(``"%d%d"``, x, y); ` `    ``return` `0; ` `} `

Options:

```1. 44                  2. 1510
3. 55                  4. 45
```
`Answer : (2)`

Explanation: The answer is option(2). In the above example, x and y are interchanged in a single line statement using the compound assignment operator, where its order of evaluation is from right to left. So, the value y is XORed with x and the result is assigned to x(i.e. x=15). In the second compound assignment operator 5 is XORed with y and the result is assigned to y(i.e. y=5). Finally, y is again XORed with x and the result is assigned to x and x becomes 10. So, both are swapped.

5. What will be the output of the following program?

 `#include ` `int` `main(``void``) ` `{ ` `    ``int` `a; ` `    ``int` `i = 4; ` `    ``a = 24 || --i; ` `    ``printf``(``"%d %d"``, a, i); ` `    ``return` `0; ` `} `

Options:

```1. 1 4           2. 4 4
3. 4 1           4. 1 1
```
`Answer : (1)`

Explanation: The answer is option(1). Here the 24||–i return 1 because logical || operators returns 1 if any one condition is true and the –i is not evaluated when 1st one returns true.

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.