C language Variable declaration problem--variable definition must be placed at the top of all execution statement/statement blocks?

Source: Internet
Author: User

Error message: Error C2065: ' Salary ': undeclared identifier

#include <stdio.h>void main () {printf ("My score is 100 points! ")///Do not use variable int salary;//to use variable salary = 100;printf (" My score is%d! ") ", salary); return 0;}

Problem root: Compiler issues--c89 and C99

C89 specifies that all local variables are declared at the beginning of the block before any execution of the statement.

There is no restriction in C99 and C + + that variables can be declared anywhere in the block before first use.

The variable declaration is placed in front of the error.

#include <stdio.h>void main () {int salary;//uses the variable salary = 100;printf ("My score is 100 points! ");//Do not use the variable printf (" My score is%d! ") ", salary); return 0;}

Pros and cons of different styles

variables are defined at the beginning of the function: easy to Modify

variable nearest definition: easy to read

variables are defined at the beginning of the block statement: Some temporary variables are placed at the beginning of the statement block, can not be defined at the beginning of the function, and free space after use, you can do so, but do not add curly braces without reason to improve the structure of the code.

To achieve the maximum compatibility cross-platform, or the function at the beginning of the definition of it

Which way to use, see the actual situation + personal preferences!

I personally prefer the nearest definition, feel more convenient.

However, the VC6.0 used now, the compiler may be C89, so it has to be defined with the beginning.

Incidentally: The suffix of the file is. C

Looked up on the internet, there are many statements, listing one or two (the meaning is similar)

1. If the source file is saved in. c format, variables can only be defined at the top, and if saved in. cpp format, the variable definition is more flexible than the first.

2. In c language, variables can only be declared and defined at the beginning of a function. The variables to be used in the function must be declared at the beginning of the definition. The declaration definition cannot have any other non-declaration-defined statements before it. Global variables are declared outside the body of the function definition. and C + + (. CPP files are compiled by C + + standard, as long as the variable is declared before the variable is used, the location does not require special requirements. The C language is defined in a module (function, loop body, etc.) first declare all the required variables before the relevant operation of the provisions.

However, after changing the suffix name to. cpp, it is still an error.

void Main () is changed to int main (void).

#include <stdio.h>int main (void) {int salary;//uses the variable salary = 100;printf ("My score is 100 points! ");//Do not use the variable printf (" My score is%d! ") ", salary); return 0;}

Should be the problem of the data type of the language??? Isn't it? Where the great God is ~

(end, to be continued)

C language Variable declaration problem--variable definition must be placed at the top of all execution statement/statement blocks?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.