Identifiers, Types, and Literals

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 for a list of them. The standard does not specify a limit to the length of an identifier, but certain implementations of C++ only examine the first 31 characters to distinguish between identifiers.

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

Table 1.1. Examples of Literals

Literal

Meaning

5

an int literal

5u

u or U specifies unsigned int

5L

l or L specifies long int after an integer

05

an octal int literal

0x5

a hexadecimal int literal

true

a bool literal

5.0F

f or F specifies single precision floating point literal

5.0

a double precision floating point literal

5.0L

l or L specifies long double if it comes after a floating point

'5'

a char literal (ASCII 53)

"50"

a const char* containing the chars '5', '0', and ''

"any" "body"

"anybody"

'a'

alert

'\'

backslash

''

backspace

' '

carriage return

'"

single quote

'"'

double quote

'f'

formfeed (newpage)

' '

tab

' '

newline char literal

" "

newline followed by null terminator (const char*)

''

null character

'v'

vertical tab

"a string with newline "

another const char*

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

Example 1.6. src/early-examples/literals.cpp

#include 
#include 

int main() {
 using namespace std;
 const char* charstr = "this is one very long string "
 "so I will continue it on the next line";
 string str = charstr; <-- 1
 cout << str << endl;
 cout << "
A	b\c'd"" << endl;
 return 0;
}
 

(1)STL strings can hold onto C-style char* strings.

We compile and run the way we described earlier:

src/early-examples> g++ -ansi -pedantic -Wall literals.cpp
src/early-examples> ./a.out

The output should look something like this:

this is one very long string so I will continue it on the next line
A bc'd"

Notice that this program shows a way to avoid very long lines when dealing with string literals. They can be broken at any white space character and are concatenated automatically using this syntax.

Exercises: Identifiers, Types, and Literals

Modify Example 1.6 so that, with a single output statement, the output becomes:

1.

GNU stands for "GNU's Not Unix".

2.
Title 1 "Cat Clothing"
Title 2 "Dog Dancing"
 


Part I: Introduction to C++ and Qt 4

C++ Introduction

Classes

Introduction to Qt

Lists

Functions

Inheritance and Polymorphism

Part II: Higher-Level Programming

Libraries

Introduction to Design Patterns

QObject

Generics and Containers

Qt GUI Widgets

Concurrency

Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments

Bibliography

MP3 Jukebox Assignments



An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net