C + + (constant)

Source: Internet
Author: User
Tags integer numbers

Constants are fixed values that do not change during program execution. These fixed values are also called literal quantities .

Constants can be any basic data type and can be divided into integer numbers, floating-point numbers, characters, strings, and Boolean values.

Constants are like regular variables, but the values of constants cannot be modified after they are defined.

Various constants:

         0213//decimal  0x4b30u    30l30ul//unsigned long integer

Integer constants

An integer constant can be a decimal, octal, or hexadecimal constant. The prefix specifies the cardinality: 0x or 0X for hexadecimal, 0 for octal, and the default for decimal without a prefix.

An integer constant can also have a suffix, which is a combination of u and L, u represents an unsigned integer (unsigned), and L represents a long integer. The suffix can be uppercase, or it can be in the order of lowercase, U, and L.

Examples of several integer constants are listed below:

212         //Legal 215u//Legal 0xFeeL//Legal   078//illegal: 8 not octal digit 032UU// Illegal: cannot repeat suffix        

The following are examples of the various types of integer constants:

         0213//decimal 0x4b30u30l30ul//unsigned long integer    
Floating-point constants

A floating-point constant consists of an integral part, a decimal point, a fractional part, and an exponential part. You can use decimal or exponential form to represent floating-point constants.

When used in decimal notation, you must include the integer part, the fractional part, or both. When represented in exponential form, it must contain a decimal point, an exponent, or both. The signed exponent is introduced by E or E.

An example of several floating-point constants is listed below:

3.14159       314159e-5l510E//illegal: Incomplete index 210f//illegal: No decimal or exponent .  Illegal: missing integers or fractions      
Boolean Constants

There are two Boolean constants, all of which are standard C + + keywords:

    • A true value represents true.
    • A value of false represents false.

We should not treat the value of true as 1, and the value of false as 0.

String constants

String literals or constants are enclosed in double quotation marks "". A string contains characters similar to character constants: Ordinary characters, escape sequences, and common characters.

You can use a space to make a delimiter, and a long string constant to branch.

The following example shows some string constants. The strings shown in the following three forms are the same.

"Hello, dear""Hello, dear""Hello," "D""ear"   
Defining constants

In C + +, there are two simple ways to define constants:

    • Use #define preprocessor.
    • Use the const keyword.
#define Preprocessor

The following is the form of defining constants using the #define preprocessor:

#define Identifier Value

For details, see the following example:

#include <iostream>Using NamespaceStd;#defineLENGTH10 #defineWIDTH5 #define  NEWLINE  ' \ n ' < Span class= "KWD" >int Main () {  int Area; area = LENGTH * Width;<< Area; cout << Newline;return 0;                /span>                

When the above code is compiled and executed, it produces the following results:

50
const keyword

You can declare constants of the specified type by using the const prefix, as follows:

Const= value;  

For details, see the following example:

#include <iostream>Using NamespaceStd;IntMain(){ Const IntLENGTH= 10; Const IntWIDTH= 5 const char NEWLINE Span class= "pun" >=  ' \ n ' ;int Area; area = LENGTH * Width; cout << Area; cout <<< Span class= "PLN" > Newline;return 0;                /span>                

When the above code is compiled and executed, it produces the following results:

50





C + + (constant)

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.