Hang (Constant / Literal) in C / C ++

Constant involves fixed values ​​that the program cannot change and they are called literals.

Constant involves fixed values ​​that the program cannot change and they are called literals .

Constant is an alternative to Literal data type, while Literal represents itself. In the example: const PI = 3.14 then Constant is PI, Literal is 3.14.

Constant can be any basic data type in C / C ++, and can be divided into integer constant values, real constants, character constants, string and Boolean literal constants (roughly translated: constant logic) .

In addition, constant is treated like regular variables, except that their values ​​cannot be changed after definition.

Raw constants in C / C ++

The integer constant can be decimal (base 10), octal (base 8) or hexadecimal (base number 16). The prefix value (prefix) is 0 for octal, 0x or 0X for hexadecimal and nothing for decimal.

An integer constant also has suffixes (suffix) U or L representing unsigned or long. The suffix may be uppercase or lowercase and may be in any order.

Examples of integer constants:

 212 // Hop le 215u // Hop le 0xFeeL // Hop le 048 // Khong hop le: 8 khong phai la mot ky so trong he octal 032UU // Khong hop le: hau to (suffix) khong the bi lap 

Here are the various types of integer constants:

 85 // Hang so dang decimal 0213 // Hang so dang octal 0x4b // Hang so dang hexadecimal 30 // Hang so dang so nguyen (int) 30u // Hang so dang so nguyen khong dau (unsigned int) 30l // Hang so dang long 30ul // Hang so dang unsigned long 

Real constants in C / C ++

A real constant consists of integer part, decimal point, fraction part and exponent part. We can represent constants in decimal or exponential form.

When expressed in the decimal form, include the decimal point, the exponent, or both. When expressed in the form of a hat, the whole part, the odd part or both. Hats form with the letter E or e.

Consider the following examples:

 3.14159 // Hop le 314159E-5L // Hop le 510E // Khong hop le: pham mu (exponent) con thieu 210f // Khong hop le: khong co phan thap pha decimal hoac phan mu exponent .e55 // Khong hop le: thieu phan nguyen integer hoac phan fraction 

Boolean literal in C / C ++

There are two types of Boolean literal and they are part of C / C ++ keywords:

True value

And false

You should not agree true value with 1 and false value with 0.

Hang characters in C / C ++

Character constants in C / C ++ start and end with single quotes. If the character constant starts with L (eg L'x ') then it is the type of wchar_t. Otherwise, it is a char constant, such as 'x'.

The character sequence can be a character (like 'X'), an escape sequence (like 't') or an extended character (like 'u02c0 ′).

Some characters in C / C ++ when preceded by a sign will have a special meaning like starting a new line 'n' or creating a tab 't'. They are known as escape sequences. The following table shows some common escape sequence codes:

Escape sequence Meaning Character 'Character' "Character"? Characters ? a Alert or bell Backspace f Form feed n Newline r Carriage return tab horizontal v tab vertical ooo Number of base numbers 8 of one to 3 digits xhh. . . Number of radix systems of one or more digits

Here is an example to illustrate some escape sequence characters:

 #include using namespace std; int main() { cout << "HellotWorldnn"; return 0; } 

When the above code is compiled and executed, it gives the following result:

 Hello World 

Constants string in C / C ++

String concatenation contained in quotation marks, for example "abc". A string will contain characters similar to the character constants, including pure characters, escape sequences, and extended characters.

You can break a long line into multiple lines by using constant strings and distinguishing them by using whitespace.

Consider, for example, a string constant in C / C ++ expressed in 3 different ways:

 "hoc, lap trinh" "hoc, lap trinh" "hoc, " "lap" "trinh" 

Constants definition in C / C ++

There are two ways to define constants in C / C ++:

Use #define preprocessor.

Using const keyword .

Use #define preprocessor in C / C ++

Here is the syntax to use #define preprocessor to define a constant in C / C ++:

 #define identifier value 

The following example explains the above syntax:

 #include using namespace std; #define CHIEUDAI 10 #define CHIEURONG 5 #define NEWLINE 'n' int main() { int dientich; dientich = CHIEUDAI * CHIEURONG; cout << dientich; cout << NEWLINE; return 0; } 

When the above code is compiled and executed, it gives the following result:

 50 

Use const keyword in C / C ++

You can use the const prefix to define constants in C / C ++ with a specific type, as follows:

 const kieu_gia_tri bien = giaTri; 

The following example explains the syntax in detail:

 #include using namespace std; int main() { const int CHIEUDAI = 10; const int CHIEURONG = 5; const char NEWLINE = 'n'; int area; dientich = CHIEUDAI * CHIEURONG; cout << dientich; cout << NEWLINE; return 0; } 

When the above code is compiled and executed, it gives the following result:

 50 

Note : It is a good practice for you to define constants in upper case in C / C ++.

According to Tutorialspoint

Previous article: Variable scope in C ++

Next lesson: Modifier in C / C ++

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile