C + + Constants (c + + numeric constants, string constants, symbolic constants) __c++

Source: Internet
Author: User
Tags arithmetic lowercase uppercase letter
Numeric Constants

A numeric constant is what is commonly called a constant. In C + +, numeric constants are type-sensitive, and their types are recognized in literal form.

Type of integer constant (integer)
As you know in the previous section: integer data can be divided into int, short int,long int and unsigned int, unsigned short, unsigned long, and so on. Integer constants are also grouped into the above categories. Why do you divide numeric constants into different categories? Data type matching is required because of the combination of an assignment or a function's parameter or actual value.

So how does an integer constant literally differentiate it from the above category? An integer that, if its value is in the range of -32768~+32767, is considered to be a short int, and it can be assigned to a short int 、int type and a long int type variable. An integer, if its value exceeds the range above, and within the -2147483648~+2147483647 range, it is considered a long int, and it can be assigned to an int or a long int variable. If the C + + version of a computer system (for example, Visual C + +) determines that int and long int data occupy the same length in memory, they can represent the same range of values. Therefore, a constant of type int is also a long int constant that can be assigned to an int or a long int variable. Constant has no unsigned type. However, a non-negative integer can be assigned to a unsigned integer variable, as long as its range does not exceed the range of variable values.

An integer constant can be represented in 3 different ways: a decimal integer. such as 1357,-432, 0, etc. After an integer constant with a letter L or L, it is considered a long int constant. such as 123L, 421L, 0L, and so on, which are often used in function calls. If the function's formal parameter is long int, then the argument is also a long int, at which point 123 is not an argument, and a 123L argument is required. Octal integer. Adding a number 0 to the beginning of a constant indicates that it is a constant in the form of a octal number. If 020 means that this is octal number 20, that is (20) 8, it is the equivalent of decimal 16. A hexadecimal integer. Adding a number 0 and an English letter x (or x) at the beginning of a constant indicates that it is a constant in hexadecimal notation. If 0x20 says this is hexadecimal number 20, that is (20) 16, it is equivalent to decimal 32.

the representation method of floating point numbers

A floating-point number can be represented in two different ways:
1 Decimal decimal form. such as 21.456,-7.98. It is generally composed of integral and fractional parts, and can omit one of them (such as 78. Or. 06,. 0), but neither is omitted. C + + compiler system to use this form of floating-point numbers are treated in double precision constants, in memory accounted for 8 bytes. If you add the letter F or F after the number of the real numbers, it means that the number is single-precision floating-point numbers, such as 1234F, -43f, 4 bytes. If you add the letter L or L, this number is a long double-precision number (long double), which occupies 12 bytes in gcc and 8 bytes in Visual C + + 6.0.

2) exponential form (i.e. floating-point form). A floating-point number can be written in exponential form, such as 3.14159 can be expressed as 0.314159x101, 3.14159x100, 31.4159x10-1, 314.159x10-2, and so on. In the program should be expressed as: 0.314159e1, 3.14159e0, 31.4159e-1, 314.159e-2, with the letter e to indicate that the following number is a power of 10, such as E12 1012. Its general form is:
Number character digit part exponent part

0.314159, 3.14159, 31.4159, and 314.159 of the above data are the digital parts. You can see that because of the existence of the exponent part, the same floating-point number can be expressed in different exponential form, and the position of the decimal point in the digit part is floating. For example:
A=0.314159e1;
A=3.14159E0;
a=31.4159e-1;
a=314.159e-2;
In the above 4 assignment statements, different forms of floating-point numbers are used, but their effects are the same.

In a program, whether floating-point numbers are decimal or exponential, they are stored in an exponential form in memory, that is, floating point form. For example, whether written in 314.159 or 314.159E0, 31.4159e1, 3.14159e2, 0.314159e3, in a program, is stored in a normalized exponential form in memory, as shown in Figure 2.3.
Figure 2.3

The number portion must be less than 1, and the first number after the decimal point must be a non-0 number, for example, not 0.0314159. So 314.159 and 314.159e0, 31.4159e1, 3.14159e2, 0.314159e3 are represented in memory as 0.314159x103. The storage unit is divided into two parts, one part for the digital part and one for the index part. For the sake of understanding, in Figure 2.3 is expressed in decimal, in fact, in the storage unit is a binary number to represent the fractional part, with 2 power to represent the exponent part.

Numeric constants, which are represented exponentially, are also treated as double constants. Word Constants Quantity

1 Ordinary Word Constants Quantity
One character enclosed by a single apostrophe is a character literal constant. such as ' A ', ' # ', '% ', ' D ' are all valid Word constants amounts, accounting for a byte in memory. Note: The word constants can only contain one character, such as ' AB ' is not legal. The constants of characters distinguishes between uppercase and lowercase letters, such as ' a ' and ' a ', which are two different words constants quantities. An apostrophe (') is a delimiter, not a part of the constants quantity of a word. such as cout<< ' a ', the output is a letter "a", not 3 characters "' A '".

2) escape Character Constants Quantity
In addition to the above form of Word constants, C + + also allows a special form of the word constants, that is, "\" The beginning of the sequence of characters. For example, ' \ n ' represents a "newline" character. "cout<< ' \ n"; "will output a newline, its effect and" cout<<endl; Same This "control character" is not displayed on the screen. It cannot be represented in a general form in a program, but only in a special form.

3 storage form of character data in memory and its using method
When you store a word constants in a memory cell, you don't actually put the character itself in the memory unit, but instead put the corresponding ASCII code in the storage unit. If the value of the character variable C1 is ' a ', the value of C2 is ' B ', the ASCII code 98 of ' a ' is stored in the variable, as shown in Figure 2.4 (a), and is actually stored in binary form in memory, as shown in Figure 2.4 (b).
Figure 2.4

Since character data is stored in ASCII code, it is stored in the same form as an integer. In this way, between the character data and the integer data in C + + can be common. A character data can be assigned to an integer variable, whereas an integer data can also be assigned to a character variable. You can also perform arithmetic operations on character data, which is equivalent to arithmetic operations on their ASCII code.

"Example 2.1" assignments the character to an integer variable.

Copy a plain text new window
#include <iostream> using namespace std; int main () {int i, J;//i and J are integral variables i= ' a ';//assign one word constants to integer variable i j= ' B '; assign a word constants amount to an integer variable J cout<<i<< ' &LT;&LT;J&LT;&L t; ' \ n '; Output integer variables I and j, ' \ n ' is a newline character return 0; }
#include <iostream>
using namespace std;
int main ()
{
  int  i, J;  I and J are integral variables
  i= ' a '; Assign a constants quantity to an integer variable i
  j= ' B '; assign a word constants amount to an integer variable J
  cout<<i<< ' <<j<< ' \ n ';  Output integer variables I and j, ' \ n ' is a newline character return
  0;
}

Output at execution time
65 66
I and J are specified as Integer variables. However, in lines 5th and 6th, the characters ' A ' and ' B ' are assigned to I and J respectively, which acts as the following two assignment statements:
i=65;j=66;
Because the ASCII code for ' A ' and ' B ' is 65 and 66. In the 5th and 6th lines of the program, the 65 and 66 are stored directly into the memory cells of I and J. So output 65 and 66.

You can see that, under certain conditions, character data and integer data are universal. However, note that character data is only one byte, and it can only hold integers within the 0~255 range.

The example 2.2 character data is mathematically calculated with an integer. The following program is to convert lowercase letters to capital letters.

Copy a plain text new window
#include <iostream> using namespace std; int main () {char c1,c2; c1= ' a '; c2= ' b '; c1=c1-32; c2=c2-32; cout<<c1<< ' <<c2<<endl; return 0;}
#include <iostream>
using namespace std;
int main ()
{
  char c1,c2;
  C1= ' a ';
  C2= ' B ';
  c1=c1-32;
  c2=c2-32;
  cout<<c1<< ' ' <<c2<<endl;
  return 0;
}

Run result is
A B
The ASCII code for ' A ' is 97, and ' A ' has an ASCII code of, ' B ' for, ' B ' is 66. You can see from the ASCII code table that each lowercase letter is 32 larger than the ASCII code of the uppercase letter corresponding to it. C + + character data and numerical arithmetic operations directly, ' a '-32 to get integer, ' B '-32 to get the integer 66. The 65 and 66 are stored in C1,C2, because C1,C2 is a character variable, so when you output C1,C2 with cout, the ASCII code of the characters A and B (A is 65,b is 66). string Constants

The part that encloses the double apostrophe is the string constant, such as "abc", "Hello!", "A+b", and "Li ping" are string constants. The string constant "abc" occupies 4 bytes (instead of 3 bytes) in memory, as shown in Figure 2.5.
Figure 2.5

The compilation system will automatically add a ' plus ' at the end of the string as the string end flag. But ' I ' is not part of the string, it's just the end of the string. Such as
cout<< "ABC" <<endl;
Outputs 3 Characters of ABC, not including '.

Note: "A" and "a" represent different meanings, "a" is a string constant, and ' a ' is the constants amount of the word. The former occupies two bytes, the latter accounting for 1 bytes. Please analyze the following program fragment:
char c; Define a character variable
C= ' a '; That's right
C= "a"; Error, C can only hold one character

Consider: the string constant "abc\n" contains several characters. is not 5 but 4 characters, where "\ n" is an escape character. However, it occupies 5 bytes (including a "\0″ character") in memory. When the compilation system encounters "\" It will think of it as the symbol of the escape character, along with its subsequent characters as an escape character.

If the character after "\" cannot constitute a valid escape character (such as "\c") with "\", an error message is displayed at compile time. If you want the "\" character to also be a character in the string, it should be written as "abc\\n", where the character includes 5 characters, that is, a,b,c,\,n. If you have the following output statement:
cout<< "abc\\\n" <<endl;
The output will be:
Abc\ (and then line wrap)
Same execution
cout<< "I say \" Thank you!\ "\ n";
The output is:
I say "Thank you!"

If the last character in a string is "\", it is a continuation character, the next line of characters is part of the string, and there are no spaces between the two lines of string. Such as
cout<< "We must study c\//Line after the last" \ "The space and the newline do not work
+ + hard! "; The character of the line is attached to the last "\" character in the previous row
The output:
We must study C + + hard! Symbolic Constants

For programming and reading convenience, in C + + programming, a symbol name is commonly used to represent a constant, called a symbolic constant, that is, a constant in the form of an identifier.

Use of the "example 2.3" symbol constant.

Copy a plain text new window
#include <iostream> using namespace std; #define PRICE 30//Note that this is not a statement, the end does not add a semicolon int main () {int num,total; num=10 total=num * price; cout<< "Total=" <<to tal<<endl; return 0; }
#include <iostream>
using namespace std;
#define PRICE  //Note this is not a statement, the end does not add a semicolon
int main ()
{
  int num,total;
  num=10;
  Total=num * PRICE;
  cout<< "total=" <<total<<endl;
  return 0;
}

The program uses the preprocessing command #define to specify price in this program unit to represent constant 30, thereafter, where the price in this program unit represents 30, can be the same as the constant operation, the program runs the result of
total=300
Note that the symbolic constant, although it has a name, is not a variable. Its value cannot be changed within its scope (in this case the primary function), nor can it be assigned a value. such as using the assignment statement "PRICE=40;" Assigning a value to Price is an error. The advantage of using symbolic constants is that the meaning is clear and you can "change everything" when you need to alter a constant. Such as:
#define PRICE 35

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.