Click here to Skip to main content
16,018,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This gets a compile error on the line where BOOL y is declared.

C#
int main(int argc, char *argv[])
{
    BOOL x;

    if (x)
        return (1);
    else
        return (2);

    BOOL y;

    y = FALSE;
}


But this compiles fine. Windows.h is included.

C#
int main(int argc, char *argv[])
{
    BOOL x;

    BOOL y;

    y = FALSE;
    if (x)
        return (1);
    else
        return (2);

}
Posted
Updated 15-Jun-11 13:21pm
v2
Comments
Peter_in_2780 15-Jun-11 18:56pm    
Looks like C language behaviour, as compared to C++.
Sergey Alexandrovich Kryukov 15-Jun-11 19:02pm    
"Is there some compile option that forces all data to be declared ahead of code?" -- that's not it -- it's required in all cases. You don't use "off" in next block if code.
--SA
TRK3 15-Jun-11 19:17pm    
What's the definiton of BOOL -- might not be what you think.
Use the /P compiler option to generate the C preprocessor output.

If this code is in a file ending in .c, then the answer is that all variables must be declared before any code.

If the file is .cpp, then this error doesn't make sense, however I would expect a warning for unreachable code since main will return before the code.
 
Share this answer
 
Comments
Lou Seitchik 16-Jun-11 11:16am    
Figured that out, then came to update the thread and saw your answer. Thanks very much. Evidently this is a Microsoft VS "feature" :)
krmed 16-Jun-11 11:22am    
Actually, the .c or .cpp isn't VS specific... the C language requires all variables to be defined before any code, but C++ does not. If you mean the unreachable code warning, I'm not sure if that VS specific or not.
"C" does not have "bool", "true" or "false".

"C++" does have them (as does "C#") as recognizable data types.

The Visual Studio IDE editor highlights "bool", "true", and "false" in blue because it does recognize them as language keywords and even does that in files with the ".C" file type, even though the compiler will complain (this is a bug in the IDE in my opinion).

Microsoft defined "BOOL", "TRUE", and "FALSE" in one of their ".H" files and most of the windows APIs, MFC APIs, etc all return things of those types. However, those types are nothing more than "int" or specific values of "int". Because of this, you will often see a mix of "bool" (lower case) and "BOOL" uppercase in "C++" programs. I try to keep the uses apart, only using "BOOL" when dealing with API returned values or API parameters.
 
Share this answer
 
Comments
Albert Holguin 16-Jun-11 2:05am    
my 5... BOOL is sort of an annoyance in C++ today, but it served its purpose before bool was standardized...
Your code compiles just fine (VS 2008, 2010) provided #include <windows.h> is used.

"Is there some compile option that forces all data to be declared ahead of code?" — there is not such rule; there is a requirement to declare all variables before they are used. It's irrelevant as you don't use the variable off.

Perhaps you need to show more of your code.

—SA
 
Share this answer
 
v2
In the first version your declaration of y comes within a block of dead code! All execution paths before that return from main, so everything below the if statement will never be executed.

Solution: Don't use premature return statements. It is a bad practice anyhow. Instead use a variable to hold the return value, and execute the remainder of the code dependend on that value, e. g. like this:
int main() {
   BOOL x;
   int result = 0; // return value - initialized to 'ok'
   if (x)
      result = 1;
   else
      result = 2;

   if (result == 0) {
      BOOL y;
      y = FALSE;
   }
   return result;
}
Note that while the declaration of y is still semantically dead code (i. e. the semantics of your code prevent it from being executed), the compiler will no longer complain, because syntactically the code is still 'alive'.

P.S.: I just wondered why this would produce an error - it should at worst produce a warning. The error you got is quite probably caused by something different. After looking at your posting again, I noticed that you mentioned having included windows.h only in the second version - and as others have pointed out you'd need that for the definition of the symbols of BOOL and FALSE.

Sorry for bothering you with a solution to a problem you hadn't asked about ;)
 
Share this answer
 
v2
Comments
Lou Seitchik 16-Jun-11 11:17am    
MS Visual Studio requires data declaractions in .c files to immediately follow scope specifier (opening bracket). .cpp files have no such restriction. thanks for your time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900