prabhu satyam
prabhu satyam

Reputation: 3

Assignment in an 'if' statement. How is the output correct?

Code in case if the image not is visible.

#include <stdio.h>

int fun(int n)
{
    if(n=4)
        return n;
    else
        return 2*fun(n+1);
}

int main()
{
    printf("%d", fun(2));
}

This is the code snippet and the output is given as 4 by the professor.

How is the output correct?

Is it possible that n=4 is assigned in the 'if-else' statement as the assignment operator is correct, but the "if" condition will not work as the syntax is wrong and the output will be directly given as 4.

Upvotes: 0

Views: 1056

Answers (4)

TohpiMou
TohpiMou

Reputation: 56

Assignment in a if-else statement is valid syntax and the branching will depend on the value of n.

Example:

#include <iostream>

int main() {
    int n = 2;

    if (n = 0) {
        std::cout << "Never printed\n";
    }

    if (n = 4) {
        std::cout << "Always printed\n";
    }

    return 0;
}

Compiler explorer: https://godbolt.org/z/fEYPcq

Upvotes: 1

Samleo
Samleo

Reputation: 605

The answer is correct, and there aren't any syntax errors.

= is an assignment operator, and in C/C++, (n = 4) is a valid expression that evaluates to true as long as the expression is not (n = 0), because n will then be considered as false by C. Note that in C/C++, 0 is false and everything else is true.

Hence, if (n = 4) is perfectly valid and always evaluates to true. Of course, in the process, there will also be an assignment involved.

Thus, what happens in the code above is that

  1. the integer n is assigned the value 4 in n = 4
  2. (n=4) as an expression returns true.
  3. return n (4).

So the answer is 4.

Upvotes: 2

"Is it possible that n = 4 is assigned in the if-else statement as the assignment operator is correct, but the if condition will not work as the syntax is wrong....?"

The syntax is not wrong and the if condition does work. It is perfectly valid. And yes, the assignment is also valid/correct.

With if (n = 4) you assign the value of 4 to the function-local variable n, although this makes less sense since n is a parameter and is meant to be feed with different values at each call to the function fun().

But I guess the intention of your professor is exactly to demonstrate this trickery, so it makes sense.

So the value of n is not 2 anymore; It is 4.


This is a valid expression for the if condition and evaluates to 1/true since the value/expression to be assigned is or does not evaluate not 0.

Usually a compiler will warn you about doing so nonetheless to avoid any undesired result here by suggesting optional parentheses around the assignment like: if ((n = 4)).

Clang:

warning: using the result of an assignment as a condition without parentheses [-Wparentheses]

GCC:

warning: suggest parentheses around assignment used as truth value [-Wparentheses]

If you explicitly want to remove these warnings, use the -Wno-parentheses flag. But it is recommended not to do so.


Since the if condition is true, it doesn't get to the recursive part in the else condition and the function fun immediately returns n which is 4.

So is the return value of 4 displayed by the call to printf() in the caller.


My favorite to remember that is this:

int war = false;
if (war = true) { launch nuke; }

Credits go to WTP.

Maybe you'll catch the joke. ;-)

Upvotes: 0

Luigi V.
Luigi V.

Reputation: 79

You should use == operator to make a confront between two compatible values. if(n = 4) assign 4 to n and then the if statement is always true. So the return value will always be 4.

Upvotes: 0

Related Questions