Sixth chapter: Types and declarations

Source: Internet
Author: User

One: C + + variable basic type

C + + variable types can be divided into built-in types (built-in types) and user-defined types (user-defined type). Built-in types can be classified into basic types (such as Bool,int, Char, double,void, etc.) and types constructed with the declaration operator (such as int*, char[], double&, double&&). User-defined types refer to data structures, classes, and enumeration types. Here are some introduction to the basic types:

Basic type:

1:bool Type:
(1): BOOL type has two values, true and false, which are used to express the result of a logical expression, such as bool result=(a==b) if the variable A and B have the same value, the value of the bool variable result is true, otherwise false;
(2): the bool type is often used as the return type of a function, such as bool is_open(File*) ;
(3): when BOOL is converted to an integer, True converts to 1,false to 0. When an integer is converted to type bool, a non-0 integer is converted to true and the integer 0 is converted to false;
(4): The pointer can also be implicitly converted to bool, non-0 pointers converted to true,0 pointers to false.

2: String Type:
(1): The most common type of string is denoted by char. On most machines, it is 1 bytes in size, so it can store 256 different values. But as we know, 8 of bits can be expressed as 0~255 or -128~127. On different machines, it's not the same. Such as

char c=255;int i=c;

On some machines the output value of I is 255, on some machines is-1. But fortunately, most of the characters we see are the size of the 0~127 within the range.
(2): In order to solve the problem that char does not represent the same range on different machines, C + + introduces the signed char and unsigned char types, and the range signed char represents is -127~127,unsigned The range represented by Char is 0~255. However, because of the difference in the range of representations, the conversion between the three types is prone to problems. So in order to avoid potential errors, we should use char and avoid using negative string values.
(3): Character literals are single characters enclosed in single quotation marks, such as ' a '. This is distinguished from the string literal, where the string literal is 0 or more characters enclosed in double quotation marks, such as "a". We can use octal to represent an escape character in the form of \ooo, where Ooo represents three octal digits (up to 3 octal digits). Similarly, we can also use hexadecimal to represent the escape character in the form \xoo ..., which consists of a backslash, an X and one or more hexadecimal digits.
3: Integral type:
(1): In the integer, we use the most is int, but unlike Char, we think that int is signed, that is, it can represent negative numbers, and char is dependent on the machine. In addition to int, there are unsigned int, long unsigned int, long int, and so on.
(2): integer literals can be expressed in decimal digits, octal digits, and hexadecimal digits. An integer constant of literal value beginning with 0 represents octal, which begins with 0x or 0X for hexadecimal, so that 0 is the literal integer constant that represents octal. In general, literal integer constants are by default an int or long type, whose precision depends on the literal value. But we can convert literal integer constants to long,unsigned, or unsigned long types by adding suffixes (l,l,u,u).
4: Floating point type:
(1): Indicates that there are three types of floating-point type, Float,double,long double. Because precision is important in numerical calculations, choosing the right type to represent a floating-point number is critical, and a double is generally selected.
(2): You can usually use decimal or scientific notation to represent the value of a floating-point literal. When using scientific notation, the exponent is denoted by E or E. The default floating-point value literal is a double type, followed by a value of F or F for single precision. Also add L or L for extended accuracy. such as 1e-3f,1.2345e1l.
5:void type
The void type has two uses, one of which is to indicate that the function does not return any values, such as void F (). The other is to use the pointer type on the void*, this pointer type can hold the address of any type of object. such as Double obj=3.14,void* pv=&obj. However, void* only supports several limited operations: comparing with another pointer, passing a void* pointer to a function, or returning a void* pointer from a function, and assigning a value to another void* pointer. However, the void* pointer is not allowed to manipulate the object it points to. To get the value of the void* pointer, you can use the following method

double obj=3.14;void* pv=&obj;double* dp=static_cast<double*>(pv);
The length of the base type:

The function of the 1:sizeof operator is to return the length of an object or type name in bytes and the result of a sizeof expression being a compile-time constant. such as sizeof (int), sizeof (int*), sizeof (char), and so on. It is important to note, however, that the sizeof operation on the pointer returns the amount of memory needed to hold the pointer, not the size of the object pointed to by the pointer.
2: For the basic type more properties we can find in this header file, such as the Std::numeric_limits::max can return the maximum value stored by the float type, can be used Std::numeric_limits::is_ Signed can determine whether Char is signed or unsigned.

Identifier

In C + +, identifier consists of letters and numbers, the first character must be a letter, we think the ' _ ' character belongs to the letter. C + + does not specify the number of characters in identifier. Of course, user-defined identifier cannot conflict with keywords already defined in C + +, such as new and delete. And be sure to note the case-sensitive in C + +, which is different from the Hello variable.

Sixth chapter: Types and declarations

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.