C ++ Annotation 01 From C to C ++, talk about some small differences between C and C ++

Source: Internet
Author: User
Tags define local

Some minor differences between c ++ and c ++ C ++ type checks are more rigorous in c language. When a character is passed in as a function parameter, it is used as an integer int, sizeof ('C') = sizeof (int); further, the c compiler treats character constants as Integer constants: putchar (10) is equivalent to putchar ('\ n. In C ++, sizeof ('C') = 1. For more information, sizeof (wchar_t) = 4. Therefore, it is easy to represent 65,536 different Unicode characters. In C ++, the difference function depends not only on its function name, but also on its parameters. Therefore, putchar (10) calls the putchar (int) version, and putchar ('\ n') calls the putchar (char) version. The prototype check of c ++ functions is stricter. For example, the function definition in c: void func (); indicates that the function func () exists and has no return value. This statement does not specify the number of formal parameters. But in C ++, it indicates that it cannot be passed in as a parameter!

In the main function C ++, the main function can be written in two ways: int main () and int main (int argc, char ** argv). The return type in the main function is int, it is not void. If no return statement is displayed, 0 is returned by default. If a return statement is written, it must keep up with an integer value, such as return 1; write only return; which is a serious error.

When calling the type check function, the prototype must be strictly matched ., However, C ++ does not include the cstdio header file by default. Some compilers in the C language are sponsoring stdio. h. Function-related function overloading C ++ supports function overloading. Function overload must meet. the function name is the same. B. the parameter table is different from the c. the attributes of the parameters are different, that is, whether the const is used for the overload function during compilation. Another name is used internally for expression. The terminology is "name mangling", such as void show (int) --> VshowI, void show (char *) --> VshowCP.

The default value of the parameter must appear in the function definition rather than in the function implementation. The code is more elegant. 0-pointers and nullptrC ++ all zero values are 0. in C, NULL indicates 0 in the pointer context. In C ++, NULL should be avoided, although its macro definition is 0.c++ 11, a New Keyword nullptr is introduced to indicate a zero-value pointer, it can only be assigned to functions in the C Runtime Library of the pointer type, however, the prefix of extern "C" must be added to C89. Local variables must be defined at the beginning of the function or statement block! Note that you are still using VC6.0. For your better code career, discard it. C99 supports local definition, which is a major improvement of C. C ++ can define local variables anywhere. In C ++, the struct can omit the struct keyword C ++ when defining variables. The struct can contain functions, that is, the default attribute is a public class.

 

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.