3B. Values

A starting point for any programming language is data: what are the data values that a program can work with?


Numeric values

C++ supports integers and real numbers, and it distinguishes between the two.

Integer constants are a way to refer to particular integer values in a program. They have forms such as 45 and -30.

Real constants have forms such as 29.05. In cases where real numbers are very large or very small, you can write constants such as 71.0E25 (71.0×1025) and 1.0E-25 (1.0×10−25).

A numeric constant that has a decimal point or an E is a real number. Other numeric constants are integers.


Boolean values

Like most languages, C++ supports values true and false, which are used as the results of yes/no questions.


Characters

A character is a member of an alphabet. Characters include letters, digits, spaces, end-of-line markers, special symbols (like left parenthesis) and punctuation marks. C++ uses the ASCII alphabet, which has 128 characters. Extensions to the ASCII alphabet have 256 characters.

A character constant is a way to refer to a particular character value in a program. Use single-quotes in a character constant, as in 'a'. (The single-quote character is on the same key as the double-quote character. It is also called an apostrophe. Don't try to be fancy and write `a'. A character constant is written between two apostrophes.)

Character constant '(' is the left parenthesis character, and ' ' is the space character. (There is a space between the two quote marks.) Character constant '\n' indicates an end-of-line marker and '\t' is a tab character.


Strings

A string is a sequence of zero or more characters. You can refer to a fixed string in a program using a string constant. Write the characters of the fixed string in double quotes, as in "the frog".

String constant "" indicates an empty string, a string with no characters in it. String constant "first\tsecond\n" has length 13; \t and \n are just one character each.


Gluing String Constants Together

C++ has an unusual feature for writing string constants. If you write two string constants in a row, they are glued together into one string. For example, "abc" "def" has the same meaning as "abcdef". Don't over-generalize this. It only works for two consecutive string constants.