C language variable declaration problem-must variable definitions be placed at the top of all execution statements/statement blocks ?, The beginning of the Variable
Error message: error C2065: 'salary ': undeclared identifier
# Include <stdio. h> void main () {printf ("My score is 100! "); // Do not use the variable int salary; // use the variable salary = 100; printf (" My score is % d! ", Salary); return 0 ;}
Root Cause: Compiler problems-C89 and C99
C89 specifies that all local variables should be declared at the beginning of the block before any statement is executed.
There is no such restriction in C99 and C ++, that is, variables can be declared anywhere in the block before the first use.
No error will be reported if the variable declaration is placed before it ······
# Include <stdio. h> void main () {int salary; // use the variable salary = 100; printf ("My score is 100! "); // Do not use the variable printf (" My score is % d! ", Salary); return 0 ;}
Advantages and disadvantages of different styles
Variables are defined at the beginning of the function:Easy to modify
Variable proximity definition:Easy to read
Variables are defined at the beginning of a block statement:Some temporary variables are placed at the beginning of the statement block. You do not need to define them at the beginning of the function and release space after use. You can do this, but do not add curly brackets for no reason to improve the code structure.
To achieve maximum compatibility and cross-platform compatibility, Let's define it at the beginning of the function.
Which method is used? Depends on the actual situation + personal preferences!
I personally prefer the definition of proximity, which is more convenient.
However, in VC6.0, the compiler may be C89, so it must be defined at the beginning.
By the way, the file suffix is. c.
I checked it online. I have a lot of arguments to list one or two (meaning similar)
1. If the source file is saved in. c format, the variable can only be defined at the beginning. If it is saved in. cpp format, the variable definition is more flexible and not at the beginning.
2. in C language, variables can only be declared and defined at the beginning of a function. Variables used in functions must be declared and defined at the beginning. No other non-declarative statements are available before the Declaration definition. Global variables are declared and defined externally in the function body. In C ++ (The. CPP file is compiled according to the C ++ standard), you only need to declare and define the variable before the variable is used, and the location is not particularly required. C language requires that the required variables be declared in a module (such as functions and cyclic bodies) before relevant operations can be performed.
However, an error is reported after the suffix is changed to. cpp.
Change void main () to int main (void ).
# Include <stdio. h> int main (void) {int salary; // use the variable salary = 100; printf ("My score is 100! "); // Do not use the variable printf (" My score is % d! ", Salary); return 0 ;}
Should it be the Data Type of the language ??? Right? Where are the gods ~
(End, to be continued)