1. Differences between C and C ++, and const analysis, 1. cconst

Source: Internet
Author: User

1. Differences between C and C ++, and const analysis, 1. cconst

Start from this chapter and learn C ++ from 0. The main content of this chapter is as follows:

  • 1)Basic differences between C and C ++
  • 2)Const differences between C and C ++

 

 

1. Differences between C ++ and C

1.1C ++ emphasizes the practicality of the language. All variables can be defined as needed.

For example:

for(int i=0;i<100;i++);

1.2C ++ does not allow multiple global variables with the same name to be defined, but C does allow repeated definitions.

1.3The C ++ register is only compatible.

1.4All C ++ identifiers must have declaration types

For example, in C:

F (): indicates that the default return value is int. It can accept functions of any int type parameter.

In C ++:

Int f () int f (void) indicates that all functions are the same. If no parameter exists, the return value is an int-type function.

Int f (I): An error is reported because I does not declare the type.

1.5Structure Upgrade

For example, define a struct in C:

Typedef student_type student; // Declaration
Struct student_type {char * name; int age ;}; struct student student_info = {"Li", 20};/* or */student student2 = {"Zhang", 20 };

In C ++, you only need to write as follows:

Struct student_type {char * name; int age ;}; student_type student2 = {"Zhang", 20}; // you do not need to declare strcut again.

 

Const differences between C and C ++

2. const in C

2.1 Introduction

The const in C language only changes the variable into a read-only attribute. In essence, it is a variable, not a constant in the true sense (only the enum enumeration defines the constant ).

Note:The const variable cannot be directly assigned a value, but you can use a pointer to modify the const variable.

Because the const local variables will exist in the stack, and the const global variables will exist in the read-only storage Zone

Therefore, we can use a pointer to modify the local variable of the const, but modifying the global variable of the const will cause the program to crash.

2.2 modify a const instance

1) instance1-Use a pointer to modify the local variable of const.

The Code is as follows:

# Include <stdio. h> int main () {const int c = 0; // const local variable int * p = (int *) & c; * p = 5; // use the pointer to modify the const variable printf ("c = % d \ n", c); return 0 ;}

Output result:

 

2) Instances2-Use a pointer to modify the const global variable.

The Code is as follows:

# Include <stdio. h> const int c = 0; // const global variable int main () {int * p = (int *) & c; * p = 5; // modify the const variable printf ("c = % d \ n", c); return 0 ;}

Output result:

 

The program crashes because the pointer modifies the data in the read-only storage area.

 

3. const in C ++

3.1 Introduction

In C ++, the const variable is a real constant and will be placed in the symbol table during definition.

Therefore, when the const variable is used during compilation, the constant is taken directly from the symbol table.

A bucket is allocated only when the const variable is global (declared using extern) or the & operator is used.

Next, we will use an example to analyze the bucket

The Code is as follows:

# Include <stdio. h> int main () {const int c = 0; // const local variable int * p = (int *) & c; // use the & operator, space will be allocated * p = 5; printf ("c = % d, * p = % d \ n", c, * p); return 0 ;}

Output result:

Why are two different output values?

This is because when & c is used, the value of c is retrieved from the symbol table and 0 is stored in a new allocated space address, so * p only modifies the allocated space address content, while c is a constant.

 

3.2 difference between const and define

Is it true that the const and define macro definitions in C ++ are the same? Actually different!

  • Const constant:Processed by the compiler. It checks the const constant type and scope.
  • Define macro definition:Processed by a pre-processor, directly replacing the text without performing various checks

(The pre-processor is a program run before the compiler, used to delete comments, macro variable conversion, etc)

Next, we will use an example to analyze const and define

The Code is as follows:

# Include <stdio. h> void f () {# define a 3 // define macro const int B = 4; // define local variable} int main () {f (); printf ("a = % d", a); // printf ("B = % d", B); return 0 ;}

Output result:

 

This is because when the pre-processor is executed, all a that is encountered will be changed to 3, so what the compiler sees is printf ("a = % d", 3 );

But cancel // printf ("B = % d", B); after blocking, the program will report an error because the scope of B is only valid in the f () function.

3.3 pointer const

Pointer const can be divided into two types: underlying const and top-level const

(The const (or reference) of a common variable is always the top-level const, that is, the const int and int const are essentially the same)

1)Underlying const(On the left side)

A constant pointer indicates that the object to which the pointer is directed is a constant. You cannot modify its content. You can only change the address pointed to by the pointer,

However, you can modify the content in other ways, such:

Int a = 1, B = 3; const int * p = & a; // underlying const // * p = 2; // error, * content a = 2 in a pointed to by p cannot be modified; // correct. use other methods to modify * p content printf ("% d \ n", * p ); p = & B; // correct. You can change the address of the pointer to printf ("% d \ n", * p );

Output result:

23

 

2)Top-level const(On the Right)

Pointer constant, indicating that the address pointed to by the pointer cannot be changed, but its content can only be modified (it must be initialized during definition)

Similar to references, for example:

Int a = 1; int B = 3; // int * const p; // error, not initialized int * const p = &; // top-level const // p = & B; // error. The pointer address cannot be changed * p = 2; // correct. The value of a is equal to 2.

 

 

This chapter ends with the following chapters:Bool type in C ++, three-object operator, reference

 

 

 

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.