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:

Data Type

Constant Examples


1 123 21000 –234

long int

35000L –34L

unsigned int

10000U 987U


123.23F 4.34e–3F


123.23 12312333 –0.9876324

long double


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.


(const float _Complex) i


(const float _Imaginary) i


_Imaginary_I (or _Complex_I if imaginary types are not supported)

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, C/C++ recognizes several backslash character constants, also called escape sequences. These constants are listed here:






Form feed




Carriage return


Horizontal tab


Double quote


Single quote




Vertical tab




Octal constant (where N is an octal constant)


Hexadecimal constant (where N is a hexadecimal constant)


Question mark

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";

C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539 © 2008-2017.
If you may any questions please contact us: