2

can somebody please tell me the difference between the following two code snippets:

//Code snippet A: Compiles fine
int main()
{
    if(int i = 2)
    {
        i = 2 + 3;
    }
    else
    {
        i = 0;
    }
}

//Code Snippet B: Doesn't compile :(
int main()
{
    if((int i = 2))
    {
        i = 2 + 3;
    }
    else
    {
        i = 0;
    }
}

If you notice the diff is just an extra parenthesis at if statement. I am using g++ compiler.Error is "expected primary-expression before âintâ"

graham.reeds
  • 16,230
  • 17
  • 74
  • 137
White_Pawn
  • 63
  • 4

4 Answers4

18

Section 6.4 of the C++ standard (draft n2914 of c++0x) has this to say about the format of if statements:

Selection statements choose one of several flows of control.
   selection-statement:
      if ( condition ) statement
      if ( condition ) statement else statement
      switch ( condition ) statement
   condition:
      expression
      type-specifier-seq attribute-specifieropt declarator = initializer-clause
      type-specifier-seq attribute-specifieropt declarator braced-init-list

That bit at the end means a condition can be either an expression or a decalarator-type construct.

And the minute the parser hits that second parenthesis, it becomes an expression, so no declarations allowed, I'm afraid.

The snippet:

if (int i = 2) { ... } else { ... }

is perfectly valid C++ in which the if section defines an integer i for the duration of the if/else and sets it to 2. It then uses that 2 as the input to the if (2 is always true, being non-zero).

The snippet if((int i = 2)) is no different syntactically to int x = (int i = 2;); if (x) which is not valid C++.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
10

Snippet A is fine - the if condition delares and initialises a variable that can be interpreted as a boolean. The variable is defined within the if and any else blocks.

Snippet B is wrong, because you can't put parentheses around a declaration - you can only put them round expressions. The following is also wrong for the same reason:

int main()
{
    int i;       // OK
    (i = 2);     // OK

    (int x = 2); // compile error
}
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Thanks Mike! simple and the best answer :) ..wish i could vote..not registered yet. – White_Pawn Nov 11 '09 at 14:01
  • Well, register so you can. Although I can't agree it's the *best* answer, it's certainly a good succinct one, especially for those here not willing to sit through my usual essays to find a gem of good information:-) I've already upvoted it. – paxdiablo Nov 11 '09 at 14:10
  • upvoting is to encourage..right? ..and am philanthropist anyways :) – White_Pawn Nov 11 '09 at 14:50
5

Here is another variant of second snippet error:

int main()
{
    int i = (int j = 0);
    return 0;
}

You can't declare variables inside ANY expression. Second () inside for is the same case - you can declare variable i inside for() but not inside nested expression placed into ().

Hope I explained it in proper words, maybe more correct explanation exists. Actually both code fragments are odd.

Roman Nikitchenko
  • 12,800
  • 7
  • 74
  • 110
1

Both look wrong.

Just because code compiles it doesn't mean it's valid. I'm surprised that the first case is compiling, since it seems like i isn't defined in the else case.

edit: I'm wrong here, definitions in the if condition are valid for that whole if / else statement.

John Carter
  • 53,924
  • 26
  • 111
  • 144