![]() |
Essentials of C++: Section I |
Simple Types, Variables, Constants and Comments C++ is a typed language, meaning that all variables and
constants must be declared using some description of the type of
information they will hold. A. Simple Types
*As found in Borland C++ 5.0 There is one other important type that more recent implementations of C++ include. This is the string. Although technically not a scalar type, it is included here because it is so much a part of many programs. See Section XIII and the document "The String Type in Borland C++" for more information. B. Declaring Variables C++ is a case sensitive language meaning that the identifiers 'abc', ABC', and 'aBc' are all different. Whitespace (spaces, tabs, newline characters and comments)
indicates the end of an identifier and therefore cannot be included in
identifiers. Thus 'mySpecialIdentifier' is a valid identifier but 'my
Special Identifier' is not. (The special capitalization here is just for
easy reading - read the "C++ Language Guidelines"
for more information.) A variable declaration consists of a type name, an identifier name, and
possibly an equal sign (=) followed by a value - to initialize the
variable. Valid declarations include:
Note the semicolon at the end of each declaration. Although a series of
declarations can be grouped together with commas separating them and a
semicolon only at the end of the series, it is more common to place them
on separate lines ending in semicolons. Other than being used as a
separator whitespace is not significant. Thus the second
declaration above could have been written as: In essence, whitespace is ignored except where it acts as a separator.
C. Declaring Constants
'Z', '2', "a", "Z", "Hello there" Note that 'a' is a character constant while "a" is a string constant. A
variable of type char could hold 'a' but not "a". Also, '2' is a character
while the digit 2 (not surrounded by quote marks) is an integer. A space
surrounded by single quotes (' ') is character constant while " " is a
string constant representing one space. A string with nothing in it, the
null string, is represented by two adjacent double quotes: "". While it is not possible, by the very definition of a character, to have a character constant consisting of multiple blanks (or ANY sequence of letters or digits), strings can consist of any number of blanks or combinations of digits and letters. String constants cannot be broken up across multiple lines without the use of the backslash (\) character acting as a continuation character. Example: Check a manual for more information on the use of special characters
such as tabs inside strings. Often, constants are declared in programs as named constants.
The best way to make such a declaration is by using the word const
followed by the type of the constant, the name (identifier) of the
constant, an equal sign, and its value: If a type name is not supplied, 'int' is assumed as a default. It is
considered good practice to add the type 'int' even though it is not
necessary. Also for style and readability, named constants are best coded
in upper case and with an underscore between words in a multiword constant
identifier. (See the "C++
Language Guidelines".) D. Lvalues The word lvalue comes from 'left value' because such values originally
referred to identifiers or expressions that could legally stand only on
the left side of assignment statements. What were originally lvalues we
now call modifiable lvalues to distinguish them from constants - which, of
course, cannot be on the left side of assignment statements. In the statement: if the identifier 'variable1' has been declared as a variable, it is a valid modifiable lvalue since it names a memory location in which the results of the expression on the right side (the rvalue) can be stored. However, the statement would not be valid since "variable2 + variable3 * variable4" is
not an lvalue. It does not name a specific memory location in which to put
the value on the right side of the expression. E. Declarations vs. Definitions A declaration by itself does not include the setting aside or allocation of memory for the identifier being declared. It is only a statement describing what may later come into existence in the computer's memory. A statement that sets aside memory is called a definition. Thus, the variable and constant declarations described here are really also definitions since memory is allocated. It is possible (using the 'extern' statement described in Section IV) to declare a variable without setting aside space for it. Function declarations are, however, often true declarations in that the definition is separate. Also, type declarations (Section IX and Section XI) do not set aside memory. F. Keywords
G. Comments the compiler will ignore "to hold the diameter of a jelly bean" and
"int value3;" but will compile the rest of the text. (Note that the first
comment might make sense as a description of the purpose of 'value1', but
the second comment makes no sense.) The second format is used for multi-line comments. Such comments begin with a slash-asterisk pair (/*) and conclude with an asterisk-slash pair (*/). Thus, one might see the following: /* Links to 'The Story of C++" and other documents
|