0% found this document useful (0 votes)
21 views2 pages

Constants

Constants are expressions with fixed values and include literals, which are values directly expressed in source code. There are several types of literals including integers, floats, characters, strings, Booleans, and pointers. Integer literals represent whole number values and can be expressed in decimal, octal (prefixed with 0), or hexadecimal (prefixed with 0x). Literal constants have inherent types like int, long, and unsigned that can be specified with suffixes added to the end of the literal.

Uploaded by

icul1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Constants

Constants are expressions with fixed values and include literals, which are values directly expressed in source code. There are several types of literals including integers, floats, characters, strings, Booleans, and pointers. Integer literals represent whole number values and can be expressed in decimal, octal (prefixed with 0), or hexadecimal (prefixed with 0x). Literal constants have inherent types like int, long, and unsigned that can be specified with suffixes added to the end of the literal.

Uploaded by

icul1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Constants

Constants are expressions with a fixed value.




Literals
Literals are the most obvious kind of constants. They are used to express particular values within
the source code of a program. We have already used some in previous chapters to give specific
values to variables or to express messages we wanted our programs to print out, for example,
when we wrote:
a = 5;


The 5 in this piece of code was a literal constant.

Literal constants can be classified into: integer, floating-point, characters, strings, Boolean,
pointers, and user-defined literals.


Integer Numerals

1
2
3
1776
707
-273


These are numerical constants that identify integer values. Notice that they are not enclosed in
quotes or any other special character; they are a simple succession of digits representing a whole
number in decimal base; for example, 1776 always represents the value one thousand seven
hundred seventy-six.

In addition to decimal numbers (those that most of us use every day), C++ allows the use of octal
numbers (base 8) and hexadecimal numbers (base 16) as literal constants. For octal literals, the
digits are preceded with a 0(zero) character. And for hexadecimal, they are preceded by the
characters 0x (zero, x). For example, the following literal constants are all equivalent to each
other:
1
2
3
75 // decimal
0113 // octal
0x4b // hexadecimal


All of these represent the same number: 75 (seventy-five) expressed as a base-10 numeral, octal
numeral and hexadecimal numeral, respectively.

These literal constants have a type, just like variables. By default, integer literals are of type int.
However, certain suffixes may be appended to an integer literal to specify a different integer
type:
Suffix Type modifier
u or U unsigned
l or L long
ll or LL long long

Unsigned may be combined with any of the other two in any order to form unsigned
long or unsigned long long.

For example:
1
2
3
4
5
75 // int
75u // unsigned int
75l // long
75ul // unsigned long
75lu // unsigned long


In all the cases above, the suffix can be specified using either upper or lowercase letters.

You might also like