1.12. Identifiers, Types, and Literals

[ fromfile: cppintro.xml id: idliterals ]

Identifiers are names that are used in C++ programs for functions, parameters, variables, constants, classes, and types.

An identifier consists of a sequence of letters, digits, and underscores that does not begin with a digit. An identifier cannot be a reserved keyword. See Appendix A C++ Reserved Keywords for a list of them. The standard does not specify a limit to the length of an identifier, but certain implementations of C++ examine only the first 31 characters to distinguish between identifiers.

A literal is a constant value that appears somewhere in a program. Because every value has a type, every literal has a type also. It is possible to have literals of each of the native data types and also character string literals. Table 1.1 shows some examples of literals and their types.

Table 1.1.  Examples of Literals

Literal Meaning
5 int literal
5u u or U specifies unsigned int
5L l or L specifies long int after an integer
05 Octal int literal
0x5 Hexadecimal int literal
true bool literal
5.0F f or F specifies single precision floating point literal
5.0 double precision floating point literal
5.0L l or L specifies long double if it comes after a floating point
'5' char literal (ASCII 53)
"50" const char* containing the chars '5' '0' and '\0'
"any" "body" "anybody"
'\a' Alert
'\\' Backslash
'\b' Backspace
'\r' Return (or Enter)
'\'' Single quote
'\"' Double quote
'\f' Formfeed (newpage)
'\t' Tab
'\n' Newline char literal
"\n" Newline followed by null terminator (const char*)
'\0' Null character
'\v' Vertical tab
"a string with newline\n" Another const char*

String literals are special in C++, due to its historical roots in the C language. Example 1.17 shows how certain characters need to be escaped inside double-quoted string delimiters.

Example 1.17. src/early-examples/literals/qliterals.cpp

#include <QTextStream>
#include <QString>

int main() {
    const char* charstr = "this is one very long string "
                " so I will continue it on the next line";
    QTextStream cout(stdout);  
    QString str = charstr; 1
    cout << str << endl;
    cout << "\nA\tb\\c\'d\"" << endl;
    return 0;
}

1

C-style strings can be converted to QString.


Building and running this program produce the following output.

src/early-examples/literals> qmake -project
src/early-examples/literals> qmake
src/early-examples/literals> make
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB
  -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I.
  -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtCore
  -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtGui
  -I/usr/include/qt4 -I. -I. -I. -o qliterals.o qliterals.cpp
g++ -o literals qliterals.o  -L/usr/lib -lQtGui -lQtCore -lpthread
src/early-examples/literals> ./literals
  

The output should look something like this:

this is one very long string so I will continue it on the next line

A       b\c'd"

Notice that this program shows a way to control the lengths of lines when dealing with string literals. They can be broken at any white-space character and are concatenated automatically using this syntax.