CHAPTER 5-1 cs
CHAPTER 5-1 cs
C++ (pronounced "C plus plus") is a powerful, popular object oriented programming (OOP)
language. The C++language can be used to practice various programming concepts such as
sequence, selection and iteration
Just like any other language, the learning of C++ language begins with the familiarisation of its
basic symbols called characters.
Character set
The study of any language, such as English, Malayalam or Hindi begins with the alphabet.
Similarly, the C++ language also has its own alphabet. With regard to a programming language
the alphabet is known as character set. It is a set of valid symbols, called characters that a language
can recognize.
The set of valid characters in a language which is the fundamental units of that language is
collectively known as character set. The character set of C++ is categorized as follows:
Tokens
Tokens are the fundamental building blocks of the program. They are also known as lexical units.
C++ has five types of tokens as listed below
1. Keywords
2. Identifiers
3. Literals
4. Punctuators
5. Operators
1. Keywords
The words (tokens) that convey a specific meaning to the language compiler are called keywords.
These are also known as reserved words as they are reserved by the language for special purposes
and cannot be redefined for any other purposes.
2. Identifiers
Identifiers are the user-defined words that are used to name different program elements such as
memory locations, statements, functions, objects, classes etc. The identifiers of memory locations
are called variables. The identifiers assigned to statements are called labels. The identifiers used
to refer a set of statements are called function names.
While constructing identifiers certain rules are to be strictly followed for their validity in the
program. The rules are as follows:
Examples for some valid identifiers are Count, Sumof2numbers, Average_Height, _1stRank,
Main, FOR.
The following are some invalid identifiers due to the specified reasons:
3. Literals
In C++, we use the type of tokens called literals to represent data items that never change their
value during the program run. They are often referred to as constants. Literals can be divided into
four types follows:
1. Integer metals
2. Floating point literals
3. Character literals
4. String literals
Integer literals
Consider the numbers 1776, 707, 273. They are integer constants that identify integer decimal
values. The tokens constituted only by digits are called integer literals and they are whole numbers
without fractional pact. [1:25 PM, 9/22/2022] Preethi
Eg: numbers like 3.14159, 3.0 × 108, 1.6 × 10-19 and 3.0.
Floating point literals, also known as real constants are numbers having fractional parts. These can
be written in one of the two forms called fractional form or exponential form.
A real constant in exponential form consists of two parts: mantissa and exponent. For instance, 5.8
can be written as 0.58 × 101 = 0.58E1 where mantissa part is 0.58(the part appearing before E) and
exponential part is 1 (the part appearing after E). The number El represents 101.
Character literals
A single character enclosed in single quotes that never changes its value during the program run,
we call it a character literal or character constant.
Note that x without single quote is an identifier whereas ‘x’ is a character constant. C++ language
has certain non-graphic character constants, which cannot be typed directly from the keyboard.
For example, there is no way to express the Carriage Return or Enter key, Tab key and Backspace
key. These non graphic symbols can be represented by using escape sequences, which consists of
a backslash (\) followed by one or more characters.
Examples of some valid character constant are: ′s ′ , ′S′, ′$′, ′𝑛 \′, ′ + ′, ′9′
String literals
A sequence of one or more characters enclosed within a pair of double quotes is called string
constant. For instance, "Hello friends", "123", "C++", "Baby\'s Day Out", etc. ate valid string
constants.
Punctuators
In languages like English, Malayalam, etc. punctuation marks are used for grammatical perfection
of sentences. ? is the punctuation mark that tells that the statement is a question. In the same way
C++ also has some special symbols that have syntactic or semantic meaning to the compiler. These
are called punctuators. Examples are: #, ; ‘ “ ( ) [ ] { }.
Operators
An operator is a symbol that tells the compiler about a specific operation. They are the tokens that
trigger some kind of operation. The operator is applied on a set of data called operands. C++
provides different types of operators like arithmetic, relational, logical, assignment, conditional,
etc.
The compilers such as GCC (GNU Compiler Collection). Turbo C++, Borland C++, and many
other similar compilers provide an Integrated Development Environment (IDE) for developing
C++ programs. Many of these IDEs provide facilities for typing, editing, searching, compiling,
linking and executing a C++program. We use Geany IDE (IT@School Ubuntu Linux 12.04) for
the purpose of illustrating the procedure for coding, compiling and executing C++ programs.
GCC compiler is a free software available with Linux operating system. GCC stands for GNU
Compiler Collection and is one of the popular C++ compilers which works with ISO C++ standard.
Geany is a cross-platform IDE for writing, compiling and executing C++ programs.
The elit window of Geany IDE can be opened from the Applications menu of Ubuntu Linux by
processing as follows
Once a file is opened, enter the C++ program and save it with a suitable file name with extension
.cpp. GCC being a collection of compilers, the extension decides which compiler is to be selected
for the compilation of the code. Therefore we have to specify the extension without fail.
Let us write a simple program given as Program 5.1 and save with the name welcome.cpp.
The IDE window after entering Program 5.1 is shown in the Figure. Observe the diffrence in
colours used for the tokens.
To save the program, choose File menu and select Save option or use the keyboard shortcut
Ctrl+S. Alternatively the file can be saved by clicking the Save button in the toolbar.
C. Compiling and linking the program
The next step is to compile the program and modify it, if errors are detected. For this, choose Build
menu and select Compile option. Alternatively we can also use the Compile button. If there are
some errors, those errors will be displayed in the compiler status window at the bottom, otherwise
the message Compilation finished successfully will be displayed.
After successful compilation, choose Build menu and select Build option for linking or click the
Build button in the toolbar. Now the program is ready for execution.
Running the program is the process by which a computer carries out the instructions of a computer
program. To run the program, choose Build menu and select Execute option. The program can
also be executed by clicking the Execute button in the toolbar. The output will be displayed in a
new window as shown in the Figure.
Once we have executed the program and desired output is obtained, it can be closed by selecting
Close option from File menu or by clicking the Close button X in the active tab or in the title bar
For writing another program, a new file can be opened by the New option from the File menu or
by clicking the New button tool bar. The key combination Ctrl+N can also be used for the same.