|
Data Types, Variables, and Constants |
|
|
|
|
Written by Hemanshu Patel
|
|
Friday, 19 October 2007 |
|
Page 8 of 8 Arrays You may declare arrays of any data type, including classes. The general form of a singly dimensioned array is type var-name[size]; where type specifies the data type of each element in the array and size specifies the number of elements in the array. For example, to declare an integer array x of 100 elements, you would write int x[100]; This will create an array that is 100 elements long with the first element being 0 and the last being 99. For example, the following loop will load the numbers 0 through 99 into array x: for(t=0; t<100; t++) x[t] = t; Multidimensional arrays are declared by placing the additional dimensions inside additional brackets. For example, to declare a 10 × 20 integer array, you would write int x[10][20]; Arrays can be initialized by using a bracketed list of initializers. For example, int count[5] = { 1, 2, 3, 4, 5 };> In C89 and C++, array dimensions must be specified by constant values. Thus, in C89 and C++, all array dimensions are fixed at compile time and cannot change over the lifetime of a program. However, in C99, the dimensions of a local array can be specified by any valid integer expression, including those whose values are known only at compile time. This is called a variable- length array. Thus, the dimensions of a variable-length array can differ each time its declaration statement is encountered. Defining New Type Names Using typedef You can create a new name for an existing type using typedef. Its general form is typedef type newname; For example, the following tells the compiler that feet is another name for int: typedef int feet; Now, the following declaration is perfectly legal and creates an integer variable called distance: feet distance; Constants Constants, also called literals, refer to fixed values that cannot be altered by the program. Constants can be of any of the basic data types. The way each constant is represented depends upon its type. Character constants are enclosed between single quotes. For example 'a' and '+' are both character constants. Integer constants are specified as numbers without fractional components. For example, 10 and –100 are integer constants. Floating-point constants require the use of the decimal point followed by the number’s fractional component. For example, 11.123 is a floating-point constant. You may also use scientific notation for floating-point numbers. There are two floating-point types: float and double. Also, there are several flavors of the basic types that are generated using the type modifiers. By default, the compiler fits a numeric constant into the smallest compatible data type that will hold it. The only exceptions to the smallest-type rule are floating-point constants, which are assumed to be of type double. For many programs, the compiler defaults are perfectly adequate. However, it is possible to specify precisely the type of constant you want.> To specify the exact type of numeric constant, use a suffix. For floating-point types, if you follow the number with an F, the number is treated as a float. If you follow it with an L, the number becomes a long double. For integer types, the U suffix stands for unsigned and the L for long. Some examples are shown next: C99 also allows you to specify a long long integer constant by specifying the suffix LL (or ll). Hexadecimal and Octal Constants It is sometimes easier to use a number system based on 8 or 16 instead of 10. The number system based on 8 is called octal and uses the digits 0 through 7. In octal, the number 10 is the same as 8 in decimal. The base 16 number system is called hexadecimal and uses the digits 0 through 9 plus the letters A through F, which stand for 10, 11, 12, 13, 14, and 15. For example, the hexadecimal number 10 is 16 in decimal. Because of the frequency with which these two number systems are used, C/C++ allows you to specify integer constants in hexadecimal or octal instead of decimal if you prefer. A hexadecimal constant must begin with a 0x (a zero followed by an x) or 0X, followed by the constant in hexadecimal form. An octal constant begins with a zero. Here are two examples: int hex = 0x80; // 128 in decimal int oct = 012; // 10 in decimal String Constants C/C++ supports one other type of constant in addition to those of the predefined data types: a string. A string is a set of characters enclosed by double quotes. For example, "this is a test" is a string. You must not confuse strings with characters. A single-character constant is enclosed by single quotes, such as 'a'. However, "a" is a string containing only one letter. String constants are automatically null terminated by the compiler. C++ also supports a string class, which is described later in this book.> Boolean Constants C++ specifies two Boolean constants: true and false. C99, which adds the _Bool type to C, does not specify any built-in Boolean constants. However, if your program includes the header <stdbool.h>, then the macros true and false are defined. Also, including <stdbool.h> causes the macro bool to be defined as another name for _Bool. Thus, it is possible to create code that is compatible with both C99 and C++. Remember, however, that C89 does not define a Boolean type. >>Complex Constants In C99, if you include the header <complex.h>, then the following complex and imaginary constants are defined. Here, i represents the imaginary value, which is the square root of –1. Backslash Character Constants Enclosing character constants in single quotes works for most printing characters, but a few, such as the carriage return, are impossible to enter into your program’s source code from the keyboard. For this reason, CC++ recognizes several backslash character constants, also called escape sequences. These constants are listed here: The backslash constants can be used anywhere a character can. For example, the following statement outputs a newline and a tab and then prints the string “This is a test”. cout << "\n\tThis is a test";
|
|
| |
|
|