The concept and difference between C + + literal constants and constant variables

Source: Internet
Author: User

The following code uses the platform Windows 64bits+vs2012.

The following concepts are often encountered when programming in C + +: constants, literal constants, symbolic constants, literal constants, constant variables, string constants, and character constants, and online blog materials are Chi laws, which are different, making everyone foggy. This article will try to clear the definition, relationship and difference of the above confusing concepts for everyone.

Constant refers to the amount of the value that cannot be changed. Constants are divided into two types in C/+ +: literal constants (Literal constant) and constant variables (constant variable).
The essential difference between literal and constant variables: literal constants are stored in the code area, are not addressable, and are stored in the data area, addressable.

1. Literal constants

literal constants , also known as literal constants, include numeric constants , character constants, and symbolic constants . This feature is written in the code area after compilation, is not addressable, cannot be changed, is part of the instruction.

int& r=5;//编译错误

This statement has a compilation error because literal constants are not addressable and cannot be used to establish references to literal constants.
The following statement is also valid:

constint& r=5;

The reason is that the compiler converts a literal constant into a constant variable. Open a nameless integer constant with a value of 5 in the data area, and then bind the reference r to the integer constant variable.

Numeric constants: include integer constants and real-type constants. Integer constants are referred to as constant integers, with decimal, octal, and hexadecimal representations of three. Real constants are represented in decimal decimal and exponential form, including single-precision floating-point numbers (float), double-precision floating-point numbers (doubles), and long double-precision floating-point numbers (long doubles).

int a=4;//4为文字数值常量中的整型常量float b=4.4//4.4为单精度实型常量double c=1.4e10//1.4e5表示的值为1.4×10^5,双精度实型常量

character constants: refers to a single ASCII character, with 256, such as ' A ' and ' B '.

Symbolic constants: use identifiers to represent a constant, which must be defined before use.

#define NUM 100//NUM为符号常量,100为整型常量enum Weekday{SUN, MON, TUES, WED, THU, FRI, SAT};//SUN,MON等均为符号常量
2. Constant variables

A constant variable refers to a variable that must be initialized at the time of definition and cannot be modified by value. Allocated space, like other variables, can be addressed. Note that a string constant is one of the constant variables, and the name is itself because it is stored in a constant area of the static store, addressable, and not modifiable.

cout<<&”hello world”<<endl;//打印输出字符串常量”hello world”存储地址

The constant variable is defined by the Const keyword in C + + and is divided into global constant variables and local constant variables.
The difference between the two is that the global constants are stored in the constant area of the static storage, which is read-only memory and cannot be modified. Local constant variables are stored in the stack area, in the high-level language semantic level by the editor to do grammar check to ensure that its value is not modifiable, because it is not placed in read-only memory, you can obtain the local constant variable address, indirect modification. See the four memory partitions in C/C + + for program memory partitioning
Refer to the following code:

#include <iostream>using namespace STD;Const intcon1=3;voidShowvalue (Const int& i) {cout<<i<<endl;}intMainintargcChar* argv[]) {Const intCon2=4;int* PTR=NULL; Ptr=const_cast<int*> (&con2); *ptr=5; Showvalue (Con2);//1    cout<<"Con2:"<<con2<<endl;//2Ptr=const_cast<int*> (&con1); *ptr=6;//3, run-time error, write conflict}

Program 1 output 5, indicating that the value of the local constant Con2 has been modified, 2 output as the result is still 4, does not mean that the constant variable Con2 value has not been modified, but because the compiler in the process of code optimization has replaced Con2 to literal constant 4. At program 3, a run-time error indicates that the global constant variable is stored in read-only memory and cannot be overridden indirectly.

This article is inevitably insufficient error, welcome comments from colleagues comment!

Reference documents

[2] Baidu encyclopedia. Symbolic constants.
[3] Baidu encyclopedia. Character constants.
[4] Baidu encyclopedia. Constant.
[5] Chen Gang. Advanced Step-by-step tutorials for C + + [M]. Wuhan: Wuhan University Press, 2008.

The concept and difference between C + + literal constants and constant variables

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: 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.