Lesson3 - C - Tokens
Lesson3 - C - Tokens
UNIVERSITY
Cabambangan, Villa de Bacolor 2001, Pampanga,
C++ Tokens
Descriptio
n
A token is the smallest element of a program that is meaningful to the
compiler. A variable provides named storage that programs can manipulate.
Each variable in C++ has a specific type, which determines the size and
layout of the variable's memory; the range of values that can be stored
within that memory; and the set of operations that can be applied to the
variable. C++ keywords, identifiers, constants and data types are used to
give meaning to C++ variables.
Objectives
After completing the module, the students are expected to:
Understand the input/output functions of C++ programming language
Familiarize with the terms used in C++ programming language
Able to construct the basic structure of C++
Able to write codes using C++ programming language in
Dev C++ IDE Instructions
Variables
The usefulness of the "Hello World" programs is rather questionable. We had to write
several lines of code, compile them, and then execute the resulting program, just to
obtain the result of a simple sentence written on the screen. It certainly would have been
much faster to type the output sentence ourselves.
However, programming is not limited only to printing simple texts on the screen. In order
to go a little further on and to become able to write programs that perform useful tasks
that really save us work, we need to introduce the concept of variables.
Let's imagine that I ask you to remember the number 5, and then I ask you to also
memorize the number 2 at the same time. You have just stored two different values in
your memory (5 and 2). Now, if I ask you to add 1 to the first number I said, you should be
retaining the numbers 6 (that is 5+1) and 2 in your memory. Then we could, for example,
subtract these values and obtain 4 as result.
The whole process described above is a simile of what a computer can do with two
variables. The same process can be expressed in C++ with the following set of
statements:
DON HONORIO VENTURA STATE
UNIVERSITY
Cabambangan, Villa de Bacolor 2001, Pampanga,
Obviously, this is a very simple example, since we have only used two small integer
values, but consider that your computer can store millions of numbers like these at the
same time and conduct sophisticated mathematical operations with them.
Identifiers
A C++ identifier is a name used to identify a variable, function, class, module, or
any other user- defined item. An identifier starts with a letter A to Z or a to z or an
underscore (_) followed by zero or more letters, underscores, and digits (0 to 9).
C++ does not allow punctuation characters such as @, $, and % within identifiers. C++ is
a case-sensitive programming language. Thus, Manpower and manpower are two different
identifiers in C++.
Keywords
Keywords are the reserved words in C++ libraries used to perform an internal
operation.
Data Types
In C++, data types are declarations for variables. This determines the type and size
of data associated with variables.
Sample Code:
DON HONORIO VENTURA STATE
UNIVERSITY
Cabambangan, Villa de Bacolor 2001, Pampanga,
Constants
Constants are expressions with a fixed value.
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. Literal constants can be classified into: integer,
floating-point, characters, strings, Boolean, pointers and user-defined literals.
Example: a = 5;
Integer Numerals – these are numerical constants that identify integer values.
They don’t
need to be enclosed in quotes or other special character.
Example:
Decimal Numerals – 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)
Example:
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:
DON HONORIO VENTURA STATE
UNIVERSITY
Cabambangan, Villa de Bacolor 2001, Pampanga,
Unsigned may be combined with any of the other two in any order to form
unsigned long or unsigned long long.
Example:
- String literals
Character and string literals can also represent special characters that are
difficult or impossible to express otherwise in the source code of a program, like
newline (\n) or tab (\t). These special characters are all of them preceded by a
backslash character (\). These are the following list of the single character escape
codes:
DON HONORIO VENTURA STATE
UNIVERSITY
Cabambangan, Villa de Bacolor 2001, Pampanga,
References:
Goodrich, Michael et.al (2011). Data Structures and Algorithms in C++. John Wiley & Sons Inc,
United States of America
Educational Technology Journals. http://www.educational-software-directory.net/publications/journals
CPlusPlusNotesForProfessionals. http://www.GoalKicker.com
C++ Tutorials. http://www.programiz.com
C++ Tutorials. http://www.w3schools.in