C/C ++ basic answers C/C ++ common mistakes

Source: Internet
Author: User

Before starting this article, I suggest you read this article, which also introduces some misunderstandings. This article introduces several misunderstandings of ThreadLocal for your reference. The following is an introductionC/C ++Common Mistakes:

1.C ++Although it is a new language developed based on C, she is not a substitute for C, not an upgrade of C, and C ++ and C are siblings. No one is more advanced than anyone else. More importantly, the standards committees of C and C ++ are independent, and the latest standards of C ++ are C ++ 98, the latest C standard is C99. therefore, I didn't talk about C ++ first, and I didn't pay attention to this "no".) The C ++ syntax is the superset of C syntax.

2. C ++/CLI and C # are Microsoft's, and they have nothing to do with C and C ++, although some syntaxes are similar. But which two languages are not similar? The 26 letters abc.

3. do not use ancient compilers such as TC/TC ++/BC/CB to learn C/C ++, because they are too old to support new C/C ++ standards. Do not use compilers that do not support the C/C ++ standard, such as CBX/VC ++ 6.0/VC2005. Although these compilers are suitable for work, they are not suitable for learning, because they have many syntax traps. Remember that the only suitable compiler for learning is gcc/mingw. [antigloss note: the compiler used by Dev-C ++ is gcc & g ++].

4. Do not use "" Instead <> to include system header files. Although some compilers allow you to do so, it does not comply with the C/C ++ standard.

Example of an error: # include "stdio. h ", # include" iostream ". [antigloss Note: Traditionally, <> is used to include standard and system header files. "" is used to include custom header files. The standard does not seem to clearly stipulate that "" is not allowed to contain standard header files and system header files. The use of "" containing standard header files or system header files can only be said to be a bad style.]

5. Do not define the return type of the main function as void. Although some compilers allow you to do so, it does not comply with the C/C ++ standard. Do not omit or write the int return type of the function. In C ++, the compiler must give at least one warning. Example of an error: void main) {}, main) {} [antigloss Note: Both C99 and C ++ 98 require the compiler to issue at least one warning for omitting int]

6. Do not paste # include "stdafx. h" in VC ++. It is a pre-compiled header file. It is like not putting the cook in the tray when serving the food.

7. [C ++] Do not # include <iostream. h>, do not # include <string. h> because they have been explicitly removed from the C ++ standard, change to # include <iostream> and # include <cstring>. the rule is:

A. If the header file is unique to the old C ++ file, remove it. H suffix, and put it into the std namespace. For example, iostream. h is changed to iostream.

B. If the header file is C, remove it. H suffix, add a c prefix, such as string. h to cstring; stdio. h to cstdio, and so on.

BTW: Do not confuse string, cstring, and string. h files.

BTW: windows. h is not a standard file of C/C ++, so it is named C/C ++.

8. instead of writing a char * p = "XXX" statement, you must write it as const char * p = "XXX". The reason why the compiler compiled the former is to be compatible with a large amount of old code. [Antigloss Note: This section is correct for C ++. However, the current C99 standard does not seem to define "XXX" as a constant.]

BTW: const TYPE * p is the same as TYPE const * p, and the style is different.

BTW: the const keyword is also found in C.

9. do not include multiple ++/-- variables in the same statement, because their Parsing is not specified in the C/C ++ standard and depends entirely on the individual behavior of the compiler.

10. C/C ++ is a platform-independent language. Therefore, process/GUI related to the system is not in the Standard C/C ++ library. For example, graphics. h and windows. h are provided by a compiler rather than C/C ++.

11. C/C ++ is only a language and platform-independent language. Some people on the forum even think that C is dos and C ++ is windows. What is linux?

12. [C ++] object-oriented language used to be the predecessor of C with classC ++), but C ++ is not. C ++ is a multi-model language. It mainly supports four programming models: Process calling, Object-based, object-oriented, and generic programming. Of course, it also supports functional, generative, metaprogramming and other models.

13. Syntax writers are not writers. Therefore, when you learn a computer language, you also need to learn Data institutions and algorithms, and master the usage of tools and platform APIs.

14. C/C ++ is a common language, so the syntax is very complicated. You should cut it into a set of suitable syntaxes, such as better C and ADT.

15. c/C ++ is a common language. Therefore, it only contains a common library. You should enrich the library you need, for example, the automobile industry association has its own C/C ++ function/class/template library.

This article introduces 15 common mistakes in C/C ++, and hope to help you.

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.