2

If I declare a variable in an if condition in C, is that variable also available to the else branch? For example:

if((int x = 0)){
  foo();
} else{
  x++;
  bar(x);
}

Couldn't find the answer, at least not the way that I worded it. Please help.

JohnFilleau
  • 4,045
  • 1
  • 15
  • 22
  • Maybe consider doing an experiment, and post an answer to your own question? – user12986714 May 01 '20 at 02:45
  • Experiment result: one can't declare variables in `if` conditions. Won't compile – user12986714 May 01 '20 at 02:52
  • 1
    @rsjaffe it doesn't appear to be a duplicate; the one you cited is about variables declared in `if` block, but this one is about those in `if` conditions. – user12986714 May 01 '20 at 02:54
  • No in C. Yes in C++, see [this](https://en.cppreference.com/w/cpp/language/if) for example. – dxiv May 01 '20 at 02:55
  • @rsjaffe Not a duplicate, the other question is about variables declared *inside* `if` statements in *C++*. Voted to reopen. – dxiv May 01 '20 at 03:06
  • 2
    Declarations in `if` conditions in C++ only became legal in C++17. – Keith Thompson May 01 '20 at 03:08
  • 1
    @user12986714 an answer referring to the Standard would be much better, since the results of experiments might only reveal some peculiarity of a particular compiler – M.M May 01 '20 at 03:15
  • 1
    reopened - the question is about C but the "duplicate" was C++-only – M.M May 01 '20 at 03:16
  • @M.M yes, standard should always be better than mere experiments. However where can I find those standards? ISO wants to charge me quite a few dollars to get a copy :-( – user12986714 May 01 '20 at 03:24
  • 1
    @user12986714 https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents – M.M May 01 '20 at 03:26

3 Answers3

4

You can't declare a variable in an if condition in C...

Declare variable in if statement (ANSI C)

If you declare inside the if scope, for example:

if(something){
  int x = 0;
} else{
  x++; // will cause a compilation error
  bar(x);
}

x in 'else' is undeclared because in C a local variable can only be used by statements contained within the code block where they are declared.

shu
  • 139
  • 1
  • 4
1

You can't declare a variable like this

 if((int a = 0))

The compiler does not allow the code to run and you get an error

and if you try this

if(something_that_is_false){
        int a = 12;
    }
    else{
        do_something;
    }

again error because they are on the same level and they do not have access to their local variables.

Warning: you can use this code and runs without error

int a;
    if(a=0){
         printf("True");
    }
    else{
        printf("False");
    }

and you will see 'False' in screen because It's like writing

if(0) // and its false!

and for the last

int a;
    if(a=0){
         printf("True");
    }
    else{
        printf("False");
    }

you will see 'True' in screen because It's like writing

if(5) // any number other than zero is true!

hdeez
  • 26
  • 1
0

Experiment result: one can't declare variables in if conditions. Won't compile.

user12986714
  • 741
  • 2
  • 8
  • 19