Data types and Constants in C + + programming Learning Tutorials _c language

Source: Internet
Author: User
Tags arithmetic data structures numeric lowercase uppercase letter

C + + data types
the object that the computer handles is the data, and the data is in a certain form (for example, integers, floating-point numbers, characters, etc.). There are often some links between different data (for example, a number of integers to form an array of integers). Data structure refers to the organization of the information. For example, an array is a data structure. Different computer languages allow the use of different data structures. Dealing with the same kind of problem, if the data structure is different, the algorithm will also be different. For example, the algorithm for sorting 10 integers and sorting an integer array that contains 10 elements is different.

C + + data includes constants and variables, and constants and variables have types. These data types can also form more complex data structures. For example, the use of pointers and structural body types can form complex data structures such as tables, trees, stacks, and so on.

C + + does not uniformly specify the accuracy of various types of data, numerical range and the number of bytes in memory, the C + + compiler system according to their own circumstances to make arrangements. Table 2.1 lists the Visual C + + numeric and character data types.

A few notes:
1 integer data is divided into long int, general integer (int) and short int. precede int with long and short, respectively.

2 The integer data storage mode is stored in binary form, for example, decimal integer 85 is in binary form 1010101, then the storage in memory is as shown in the following figure.

3 the integer symbol int and the character symbol char are preceded by a modifier signed (representing "signed") or unsigned (denotes "unsigned"). If specified as signed, the value is stored in the complement form, and the highest bit (bit) in the storage cell is used to represent the symbol of the value. If specified as unsigned, the value has no symbol, and all bits are used to represent the value itself. For example, the short integer data occupies two bytes,

When signed, the maximum value that can be stored is 215-1, or 32767, and the minimum value is-32768. When unsigned, the maximum value that can be stored is 216-1, or 65535, and the minimum value is 0. Some data has no negative value, and you can use unsigned, which stores a positive range one times larger than the signed.

4 floating-point (also called solid) data is divided into single precision (float), double precision (double) and long double 3, in Visual C + + 6.0, provides 6-digit valid digits for float, and a 15-digit valid number for double, And the range of values for float and double is different. Assigns 4 bytes to float and 8 bytes to double and long double.

5 in a column of type identifiers in a table, the square brackets [] contain portions that can be written, such as short and short int equivalent, unsigned int and unsigned equivalent.

The value of a constant cannot be changed, and it can generally be judged by its literal form as a constant. Constants include two broad classes, numeric constants (that is, constants), and character-type constants. For example, 12, 0,-3 is an integer constant, 4.6,-1.23 is a literal constant, and the character included between the two apostrophes is the constants quantity, such as ′a′,′x′. This literal recognition of constants is called "literal constants" or "direct constants."


C + + constants (c + + numeric constants, string constants, symbolic constants)
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, it can be assigned to a short 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 in the program written in 314.159 or 314.159E0, 31.4159e1, 3.14159e2, 0.314159e3, in the form of memory is in the normalized exponential form of storage,

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. Attention:
Word constants can only contain one character, 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 ', C2 's value is ' B ', the ASCII code of ' A ' is stored in the variable, and the ASCII code 98 of ' B ', as shown in Figure 2.4 (a), is actually stored in binary form in memory,

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" assignments the character to an integer variable.

#include <iostream>
using namespace std;
int main ()
{
 int i, J;//i and J are integral variables
 i= ' a ';//assign a Word constants amount to 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.

"Example" character data and integers are arithmetic operations. The following program is to convert lowercase letters to capital letters.

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

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 ';//correct
 c= "a";//error, C can only hold one character

Consider: string constant "abc\n" contains several characters? is not 5 but 4 characters, where "\ n" is an escape character. However, it occupies 5 bytes in memory (including a "per" character). 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\//bank's last" \ "After the space and the line break does 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" symbol constant.

#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=" <<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

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.