C ++ types and declarations

Source: Internet
Author: User

Enumeration and classes are called user-defined types. They must be defined by the user and cannot be directly used without prior declaration. For example, cmyclass type must be defined before cmyclass A is used.
The internal type can be used directly, for example, int.

By definition, true has a value of 1, while false has a value of 0. Correspondingly, integers can be implicitly converted to bool values: integers other than 0 are converted to true, while integers 0 are converted to false.
For example, int A = 10; bool B = A; int c = B; Result A is 10, B is true, and C is 1.
Pointers can also be implicitly converted to bool, non-0 pointers can be converted to true, and pointers with 0 values can be converted to false.

The number of characters starting with 0 and followed by X (0x) is a hexadecimal number (base on 16 ), the number of characters starting with 0 followed by X is the number of octal (based on 8 ).
For example:
Decimal: 0 2 63 83
Ocatl: 0 02 077 0123
Hexadecimal: 0x0x2 0x3f 0x53

The suffix u can be used to show the unsigned text volume, and the suffix l can be used to show the long text volume. For example, 3 is an int type, 3u is an unsigned int type, and 3l is a long int type.

The floating point type indicates the floating point number. The floating point number has three sizes: Float (single precision), double (Double Precision), and long double (extended precision ). According to the default rule, the floating point text volume is of the double type, and there cannot be spaces in the middle of the floating point text volume (123.4 56 will cause a syntax error ). If you want to write a float floating point text volume, you can use the suffix f or F, for example, 1.23f. If you want to write a long Double Floating Point text volume, you can define them by the suffix L or l, for example, 3.12l.

The following are the properties that can be guaranteed by the size of the basic type:
1 = sizeof (char) <= sizeof (short) <= sizeof (INT) <= sizeof (long)
1 <= sizeof (bool) <= sizeof (long)
Sizeof (float) <= sizeof (double) <= sizeof (Long Double)
Sizeof (n) = sizeof (signed n) = sizeof (unsigned N)
Where N can be char, short int, Int or long Int. In addition, it is ensured that Char has at least 8 digits, short has at least 16 bits, and long has at least 32 bits.

Type void is a basic syntax type. It can be an integral part of a more complex type, but there is no void object. Void is used to portray a function that does not return values. It is also used as the basic type of pointer to an unknown object. For example:
Void X; // error, no void object
Void F (); // function F does not return values
Void * PV; // pointer to an object of unknown type

By default, the enumerated values increase progressively from 0. The Enumerator can also be initialized with an integer. If the values of all enumerators in an enumeration are not negative, the enumerated value range is [0: 2 ^ k-1], 2 ^ K indicates the power of 2 that can make all enumerative operators within this range. If there is a negative enumerated character value, the enumerated value range is [-2 ^ K: 2 ^ k-1]. For example:
Enum E1 {dark, light}; // value range: 0: 1
Enum E2 {A = 3, B = 9}; // value range: 0: 15
Enum E3 {min =-10, max = 1000000}; // range:-1048576: 1048575
An integer value can be displayed and converted to an enumerated value. It is not defined unless the result of this conversion is within the enumerated range. For example:
Enum flag {x = 1, y = 2, Z = 4, E = 8 };
Flag F1 = 5; // The type is incorrect. 5 is not of the Flag type.
Flag F2 = flag (5); // yes. Flag (5) is of the Flag type and is within the flag range.
Flag F3 = flag (z | E); // yes. Flag (12) is of the Flag type and is within the flag range.
Flag F4 = flag (99); // unspecified, 99 is not within the flag range
The sizeof of an enumeration is a sizeof of an integer that can accommodate its range. It is not larger than sizeof (INT) unless the value of an enumeration character cannot be expressed by an unsigned Int. For example, on a machine with sizeof (INT) = 4, sizeof (EL) can be 1 or 4, but it will never be 8.

The operator only acts on a single name, not all names subsequently written in the same declaration. For example:
Int * P, Y; // int * P; int y; not int * Y;
Int X, * q; // int X; int * q;
Int V [10], * PV; // int V [10]; int * PV;
This structure is not conducive to reading the program, so we should avoid

A name (identifier) consists of letters and numbers. The first character must be a letter. The underline character is also considered as a letter.

The name declared in a block can mask the name declared in the block outside it or the global name. For example
Int x // global
Void F ()
{
Int X; // The local X masks the global x
X = 1; // assign a value to the local X
{
Int X; // mask the first local X
X = 2; // assign a value to X in the second part.
}
X = 3; // assign a value to X in the first part.
}
Int * P = & X; // obtain the address of global x
The masked global name can be referenced by the scope resolution OPERATOR:. For example:
Int X;
Void F2 ()
{
Int x = 1; // mask the global x
: X = 2; // assign a value to global x
X = 2; // assign a value to the local X
}
Int x = x; // This is not illegal, but absurd.

The function parameter is treated as the name declared in the outermost block of the function.
Void F5 (int x)
{
Int X; // Error
}
Yes, because X is defined twice in the same scope.

If you provide an initial formula for an object, the initial formula determines the initial value of the object. If the initial formula is not provided, global, namespace and local static objects will be automatically initialized to the appropriate type of 0, local objects and objects created in the free storage area will not be initialized. Array and structure members are also determined based on whether the array or structure is static by default.

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.