Fundamental Types:bool, char, int, double,enum,void, int *, char[], double&, Datastructures and class.
The Boolean, character, and integer types are collectively called integral types. The integral and floating point types are collectively called arithmetic types. Enumerations and classes are called user defined types, In contrast, other types are called built-in types.
A Boolean, bool , can have one of the two values true or false.
A variable of type char can hold a character of the implementation’s character set.
C++ provides two types for which the answer is definite; signed char , which can hold at least the values -127 to 127 ,and unsigned char , which can hold at least the values 0 to 255 .
A type wchar_t is provided to hold characters of a larger character set such as Unicode.
each integer type comes in three forms: ‘plain’ int , signed int , and unsigned int . In
addition, integers come in three sizes: short int , ‘plain’ int , and long int . A long int can be referred to as plain long . Similarly, short is a synonym for short int , unsigned for unsigned int , and signed for signed int .
Unlike plain chars, plain int s are always signed. The signed int types are simply more explicit synonyms for their plain int counterparts.
Integer literals come in four guises: decimal, octal, hexadecimal, and character literals.
Like integers, floating point types come in three sizes: float (singleprecision), double (doubleprecision), and long double (extendedprecision).
1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
1 <= sizeof(bool) <= sizeof(long)
sizeof(char) <= sizeof(wchar_t) <= sizeof(long)
sizeof(float) <= sizeof(double) <= sizeof(long double)
sizeof(N) == sizeof(signed N) == sizeof(unsigned N)
In addition, it is guaranteed that a char has at least 8 bits, a short at least 16 bits, and a long at least 32 bits.
The type void is syntactically a fundamental type. It can, however, be used only as part of a more complicated type; there are no objects of type void . It is used either to specify that a function does not return a value or as the base type for pointers to objects of unknown type. void is used as a ‘pseudo return type’ to indicate that a function doesn’t return a value.
An enumeration is a type that can hold a set of values specified by the user. Once defined, an enumeration is used very much like an integer type.
Before a name (identifier) can be used in a C++ program, it must be declared.
A name (identifier) consists of a sequence of letters and digits. The first character must be a letter. The underscore character _ is considered a letter.
Advice:
[1] Keep scopes small;
[2] Don’t use the same name in both a scope and an enclosing scope;
[3] Declare one name (only) per declaration;
[4] Keep common and local names short, and keep uncommon and nonlocal names longer;
[5] Avoid similarlooking names;
[6] Maintain a consistent naming style;
[7] Choose names carefully to reflect meaning rather than implementation;
[8] Use a typedef to define a meaningful name for a builtin type in cases in which the builtin
type used to represent a value might change;
[9] Use typedefs to define synonyms for types; use enumerations and classes to define new types;
[10] Remember that every declaration must specify a type (there is no ‘implicit int ’);
[11] Avoid unnecessary assumptions about the numeric value of characters;
[12] Avoid unnecessary assumptions about the size of integers;
[13] Avoid unnecessary assumptions about the range of floatingpoint types;
[14] Prefer a plain int over a short int or a long int ;
[15] Prefer a double over a float or a long double ;
[16] Prefer plain char over signed char and unsigned char ;
[17] Avoid making unnecessary assumptions about the sizes of objects;
[18] Avoid unsigned arithmetic;
[19] View signed to unsigned and unsigned to signed conversions with suspicion;
[20] View floatingpoint to integer conversions with suspicion;
[21] View conversions to a smaller type, such as int to char , with suspicion;
阅读(587) | 评论(0) | 转发(0) |