The Visual C + + compiler automatically assumes the band. C file extension is a C file instead of C + + file, and C + + syntax and keywords are rejected (C language can only declare variables at the top of curly braces)

Source: Internet
Author: User

Today, when compiling OpenGL Red Book with the light.c file in the source code, you encounter a strange problem:

Light. c, compile OK without making any changes. However, if you write executable code in some places, you will not be able to compile through the compiler!

(These lines of code are OK if the first sentence in the main function is written)

I used the VS08. I sent the document to other friends (VS10), which is the same problem.

However, after I changed the file name to Light.cpp, the problem was solved.

The question now is, how will the compiler be affected by the suffix behind the code file?

You can do a simple test:

[CPP]View PlainCopy
    1. int main ()
    2. {
    3. #ifdef __cplusplus
    4. printf ("defined __cpluslus");
    5. #else
    6. printf ("undefined __cpluslus");
    7. #endif
    8. return 0;
    9. }


The test results are: If the CPP file is compiled, the compiler will automatically help you to join the __cplusplus macro and use the C + + compilation rules. This point of MSDN has in fact clearly stated:

Give the file a. c extension, such as mysource.c.

The Visual C + + compiler automatically assumes the band. The C extension file is a C file instead of a C + + file and rejects C + + syntax and keywords (such as public,private, and Class).

C + + files use the. cpp extension.


One more non-mainstream test. What if the compilation is neither a. c file nor a. cpp file?

The author changed the file extension to light. DD, the result is that the defined __cplusplus is displayed. That is, the VS default, except for. c files, is considered to be a C + + file to compile.

Oddly, this is what is described on MSDN:

The file name extension determines how the file is processed. C and C + + files with the extension. C,. cxx, or. cpp will be compiled. Other files, including. obj files, library (. lib), and module definition (. def) files, are passed to the linker without processing.


Now back to the original question.

Why does light.c add the execution code (even the empty code) to compile? If this is because of a compiler problem, why not add those lines of "devil Code" before the compile OK?

Is this just a compiler bug?

It's clear.
The C language can only declare variables at the top of the curly braces ...
Hang Daddy. http://blog.csdn.net/lsldd/article/details/6890943

The

Visual C + + compiler automatically assumes the band. The C extension file is a C file instead of a C + + file, and the C + + syntax and keywords are rejected (the C language can only declare variables at the front of the curly braces)

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.