A brief talk on some small differences between C and C + + _c language

Source: Internet
Author: User

C + + type checking is more stringent

In C, when the character is passed as a function parameter, the character is used as integral int, sizeof (' c ') = sizeof (int); Further, the C compiler equates the amount of character constants with an integer constant: Putchar (10) is equivalent to Putchar (' \ n ').

However, in C + +, sizeof (' C ') = = 1, supplemented by a point, sizeof (wchar_t) ==4. So it's easy to represent 65,536 different Unicode characters. In addition, in C + +, the difference function depends not only on his function name, but also on its parameters. Therefore, Putchar (10) calls the Putchar (int) version

, Putchar (' \ n ') invokes the Putchar (char) version.

C + + 's function prototype check is more stringent. The function definition in C: void Func (), indicating that the function func () exists, and there is no return value. The declaration does not specify how many formal parameters are specific. But in C + +, it means that it must not have formal parameters passed in!

Main function

In C + +, main functions are mainly written in two ways: int main () and int main (int argc, char** argv);

The return type in the main function is int, not void

If no write return statement is displayed, the default returns 0;

If you write a return statement, you must keep up with an integer value, such as return 1; Write return only; is a serious mistake.

Type checking

When a function is called, the prototype must match strictly. The C language contains stdio.h by default, but C + + does not default to include, so you must include the Cstdio header file. C language Some compiler friendship sponsored the stdio.h.

Function-related differences

Function overload

C + + supports function overloading. The function overload must satisfy the

A. The same function name

B. Different formal parameter lists

C. The attributes of a formal parameter are different, that is, const

At compile time, in fact, for overloaded functions, the internal use of a different name, the professional term is "name mangling", such as void Show (int)--> vshowi, void Show (char*)-->VSHOWCP, such a name.

Formal parameter Default value

The default parameter must appear in the function definition, not in the function implementation.

Code more elegant

Single-line comment, multiple single-line comment in C + +

Null-pointers vs. 0-pointers and nullptr in c++11

All of the 0 values in C + + are 0. In C, NULL represents 0 in the pointer context, and in C + +, you should avoid using NULL, although its macro definition is 0.

C++11 introduced a new keyword nullptr represents a 0-value pointer, which can only be assigned to a pointer type

In C + +, you can use the functions in the C. Runtime Library, but you need to add the extern "C" prefix

In C89, a local variable must be defined at the beginning of a function or statement block! Still using VC6.0 Please note that for your better code career, abandon it. C99 has supported a local definition, which is a big improvement in C. C + + can define local variables anywhere.

In C + +, the struct body can omit the struct keyword when defining the variable

In C + +, the struct body can contain functions, that is, the default properties are public classes

The above is a small series for everyone to talk about C and C + + some of the small differences in all the content, I hope that we support cloud Habitat Community ~

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.