In this blog, we will see some exciting C programs, I want you to look at those programs and try to guess the output without executing. But of course, you can execute the programs after you have solved them yourself. Don’t worry it’s not going to be a complex program. So let the fun begin!
Have a look at below code snippets and guess the output
Explanation:
At one glance, you might think the output will be “a and b are not equal”. The catch here is the assignment operator used in the if statement. It simply assigns the value of b to a and hence the condition becomes if(13.65).
The condition evaluates to true since 13.65 being a non-zero positive constant is a truth value and hence executes the first print()
Snippet 1 Output: a and b are equal
Explanation:
This is a bit tricky, we might be led to believe that while evaluating z what would be added is 3 and 4. But this is wrong! It’s because while calculating z, the very first operation that is performed is ++x, which increments the value of x to 4. So by the time condition (x+x) is performed x initial value has now become 4. Thus 4+4 is performed not 3+4. After this, the result ‘8’ is assigned to variable z. And then x is incremented to 5 because of x++
If you didn't understand this, read about post increment and pre increment in C
Snippet 2 Output: x=5 z=8
Explanation:
Firstly, (!x) is evaluated. Since x has a truth value (a non-zero positive value), ! negates this value and yields a zero. Thus the condition is reduced to if(!(0) && x)
!(0) becomes 1. Note that the value of x is still 10, hence the condition becomes if(1 && 10)
Since both, the conditions yield truth and they both have been combined with &&(AND operator) the whole thing is treated as true, and hence the first printf() is executed.]
Didn’t get it? Read about logical operators in C
Snippet 3 Output: x=10
Explanation:
+ has higher priority over /. Therefore firstly x is incremented to 4 so by the time division is carried out the initial value of variable x is now 4 and 4/4 is performed and the result is assigned to variable z.
Snippet 4 Output: x=4 z=1
Explanation:
While evaluating z, the highest priority is enjoyed by unary minus, which attaches a minus sign to x, making it -3. Note that in doing this, the variable of x remains unchanged. The next operation performed is (- -y) which decrements y to 2. So the operation becomes (-3–2) and this evaluates to -5 and then -= results in ‘8’ (3- -5) which is assigned to variable z. Lastly, x is decremented to 2.
Didn’t get it, read about operators precedence in C.
Snippet 5 Output: x=2 y=2 z=8
Explanation:
We can see that z and y are assigned the value of x which is 3. If you look at the expression closely, the evaluation of the expression starts from right to left. Firstly the value -z is assigned to x. Hence x gets a value of -3. Then y+=x is evaluated, this is the same as y=y+x and 3+-3, ie 0, and is assigned to y. The value of y is then assigned to z. Thus z also turns out to be zero.
Snippet 6 Output: x=-3 y=0 z=0
Explanation:
According to the priorities of logical operators, the conditions to be evaluated are (++x) and (++y && ++y). Since the second and the first conditions have been combined with || operator, the second condition would be evaluated only if the first condition turns out to be false and the second condition indeed turns out to be false since incrementing x by 1 makes it 0 which is false. Therefore the second condition is evaluated. And the second condition to ‘0’ meets the same fate since on incrementing y it becomes 0. Therefore the condition becomes (0 && z++), which turns out to be false. Here z doesn’t get a chance to get incremented, as the condition before && is already 0. Now since both the conditions have been evaluated to be false, the false value 0 is assigned to z.
Snippet 7 Output: x=0 y=0 z=0
Snippet 8 Output: Shoot
Explanation:
Whaat! How? 0.7 is never less than 0.7 so the condition should evaluate as false. The reason here is, in the C programming language 0.7 by default is a double and only is considered float when we provide the datatype as a float. So we know that the value of variable a is a float but the constant 0.7 in the if statement is a double. As float and double have different properties, due to precision consideration the value of a is stored as something less than 0.7, and Shoot is printed.
This blog will get too long if I start talking about precision, data types, and everything so I’m going to provide some links that you can check out if you find snippet 8 interesting.
https://ecomputernotes.com/what-is-c/types-and-variables/precision-setting
Explanation:
The for loop begins with an initial value of i=-1
, so the condition (-1< 5) is satisfied hence the continue statement is executed. This sends the control to i++ in the for statement. On incrementing i=0, the condition in the for loop is tested and satisfied, and hence once again the control reaches inside the loop. Once again the, if statement is satisfied and continue takes control to i++. This is repeated until the value of (i<5) fails and when it happens, the control moves to the else block and executes the break statement. Here break takes control outside the loop and because there’s no statement outside the loop, nothing is printed on the screen.
Snippet 9 Output:
Snippet 10 Output: 300
Explanation:
c > 1 fails since the value of c is 0, and the control reaches 300
which is assigned to a. It would become easier to understand the statement if we parenthesize the expression as shown below.a = (c>1 ? (d>1 ||e>1?100:200):300);
Similarly, let’s say the value of c=2, this time the condition (c>1) is true, so the expression (d>1 ||e>1?100:200)
will be evaluated and the result would be 100.
Didn’t get? Learn about ternary operators in C.
That’s it for today, hope you enjoyed solving these.
Thank you for Reading!