C++ Character Set and Program Structure

What is C++?

C++ is an object-oriented programming (OOP) language. It was developed by Bjarne Stroustrup in 1983 at the AT & T Bell Laboratories, New Jersey, USA. C++ is basically a superset of C, which provided low-level features, and Simula 67, which provided the class concept.

C++ has the following features:

  • Reduces complexity while solving problems.
  • Correctness of results is ensured.
  • Affordable in terms of hardware and other resources.
  • Easier and cheaper to integrate existing software facilities and libraries.
  • Portable i.e. can be used on different types of computers with little or no change in the programs.

C++ was originally named ‘C with classes’. The name is derived from the ‘increment’ operator in C, which is ‘++’. So C++ is an incremented version of C.

C++ can be used for writing procedural code like C, but its real strength lies in writing object-oriented programs.

An object-oriented program is a collection of discrete objects, which are self-contained collections of both data structures and functions that interact with other objects.

C++ adds classes, inheritance, function overloading, and operator overloading. With the help of these one can create abstract data types, inherit properties from existing data types, and use polymorphism. Thus we can say that C++ is an advancement of C, providing an additional set of object-oriented facilities.

Note: With the help of C++, one can develop editors, compilers, communication systems, databases, and any other real-life application system.

C++ Character Set:

A character set is a set of valid characters that a language can recognize. A character represents any letter, digit, or any other sign. The C++ has the following character set.

  • Source Characters.
  • Escape Sequence/ Execution Characters.

Source Characters:

The source text is created with the help of source characters. Following are the source characters:

Alphabets: A to Z, a to z.

Digits: 0 to 9.

Special Characters/Symbols:

C Special Symbols - C++ Character Set and Program Structure

Escape Sequence/ Execution Characters:

These are interpreted at execution time. The values of these characters are implementation-defined.

C++ uses some characters such as line feed, formfeed, tab, etc. through execution characters i.e. which cannot be printed or displayed directly.

Each of these characters has a unique implementation-defined value that can be assigned to a single character. Every escape sequence character starts with a backslash ( \ ) followed by a character but both are considered as a single character.

The following table shows some of the escape sequence characters:

EscapeMeaningExecution Time Result
\0End of stringNull
\nEnd of lineTakes the control to the next line.
\rCarriage returnTakes the control to the next paragraph.
\fForm feedTakes the control to the next logical page.
\tHorizontal tabTakes the control to the next horizontal tabulation position.
\vVertical tabTakes the control to the next vertical tabulation position.
\bBackspaceTakes the control to the previous position in the current line.
\\BackslashPresents with a backslash \.
\aAlertProvides an audible alert.

Also if a character is put after the backslash ( \ ) in the output statement, we will have the same character appearing in the output.

For example- cout <<‘\J’; will result in the display of J.

Here the identifier cout is a predefined object, that is used for the standard output stream (screen) in C++. The operator << called insertion or put to operator sends (or inserts) the contents on its right to the object on its left.

Structure of a C++ Program:

Programs in C++ are generally written in terms of classes and functions. Every program consists of the function main ( ). Comments can be included in a program. Indentation should be maintained throughout the program.

// Program 1
// Finding the sum of three integers

#include <iostream.h>
void main ( )
int a, b, c, sum;
cout << “Enter three integers: “;
cin >> a >> b >> c;
sum = a + b + c;
cout << “\n sum of three integers is” << sum;

When this program is executed, the following message is displayed.

Enter three integers:
If we type three integers 4 5 8 leaving a blank space between integers, it displays the output.
Sum of three integers is 17
provided we input 4, 5, and 8.

Let us discuss the statements in the program. The first statement begins with // which is an indication to the C++ compiler that this statement is for documentation. Whatever is written in a line after // is not executed at all. We generally include comments in programs by writing //, because they enhance the readability of programs.

Include Files:

The third statement #include <iostream.h> is not part of main ( ) function. It does not end with a semicolon as program statements do. It begins with the number sign # which is known as a preprocessor directive. We know that program statements are instructions to the computer. But a preprocessor directive is an instruction to the compiler. The file containing the program is called the source file. The preprocessor directive #include instructing the compiler to insert another file into the source file. Thus, the file iostream.h is added to the source file before compiling. The file iostream.h is an example of a header file.

Declaration of Objects: cout and cin

Why should iostream.h be added to the source file? The iostream.h file contains declarations that are needed by the objects cout (pronounced si out) and cin (pronounced si in), the insertion operator << (two less than symbols) and extraction operator >> (two greater than symbols). The compiler does not recognize cout, cin, << and >> without these declarations. Hence the preprocessor directive on the third line is necessary.

Main Function:

This program consists of only one function, called the main ( ). The word main is the name of the function. The parentheses ( ) after main are the distinct feature of a function. The word main becomes an identifier without these parentheses. The empty parentheses ( ) show that the function has no arguments. The keyword void before the function name indicates that the function main ( ) does not return a value.

The statement(s) written in a function are called function body. The function body is enclosed in braces (or curly brackets). Maintaining the opening brace and closing brace on the same column is a good programming practice. Both braces are placed in the fourth column in this program. It is very important that every opening brace should have a corresponding closing brace. The opening brace in the seventh line says that the function definition begins. The closing brace in the thirteenth line says that the function definition ends.

We notice that the opening brace begins after three spaces. Leaving such margins is known as indentation. It improves the clarity of a program.

A C++ program may have several functions. Of course, this program has only one function. When a C++ program is executed, the first function to be executed is the main ( ). If the main ( ) function does not appear in the C++ program, a linker error occurs.

The first statement
int a, b, c, sum;
defines four integer variables a, b, c and sum.

The keyword int indicates that the variables are integer type. The list of variables follows the keyword int. Commas separate the variables. A semicolon terminates the definition. A semicolon also terminates the statements in a program. The variables must be defined before using them. Otherwise, a syntax error occurs.

Use of Operators << and >>

The second statement
cout << “Enter three integers: “;

displays the string Enter three integers: on the screen. The insertion operator << directs the contents of the variable or constant on its left to the object cout. The object cout sends the contents to the screen. In this case, the string constant is sent to the screen for display.

The statement
cin >> a;

causes program execution to wait until the user types the value for a. The object cin represents the value coming from the keyboard. The extraction operator >> takes the value represented by the object on its left (that is, cin) and assigns it to the variable on the right (that is, a).

Cascading >>

The third statement
cin >> a >> b >> c;

also causes the program execution to wait for the user to type three values separated by blank spaces. The use of >> more than once in a statement is known as cascading >>. The extraction operator >> is used three times. The values typed from the keyboard are assigned to the variables a, b and c in the same order. In other words, the first value is assigned to a, the second to b, and the third to c. Thus, 4, 5, and 8 are assigned to a, b, and c respectively in the sample execution.


The fourth statement
sum = a + b + c;

consists of the variable sum, the assignment operator =, and the expression a + b + c. An arrangement of variables and operators that requires a computation is called an expression. Thus, the expression contains three variables a, b, and c, and the addition operator +. The values in the variables a, b, and c are added together and assigned to the variable sum. Thus, 4, 5, and 8 are added, that is, 17 which is assigned to the variable sum in the sample execution. We must note that symbol = is the assignment operator, not the equality symbol. The equal symbol in C++ is ==.

Cascading <<

The fifth statement
cout << “\n sum of three integers is” << sum;

consists of the insertion operator at two places. The use of << more than once in cout statement is known as cascading <<. The object cout first sends the string constant sum of three integers is to the screen, and then the value (assigned to sum) to the screen.

We observed that the string constant begins with \n, which is known as an escape sequence. The backslash symbol \ interprets the following character n as a new line character, not as n. Thus, the insertion operator << displays the string sum of three integers is on a new line. Thus, we see the output

Sum of three integers is 17
on the screen in the sample execution.

Important Note:

Present-day C++ programs write in the given below format.

Program to display ASCII code of a character - C++ Character Set and Program Structure

Database Management System (DBMS)
Computer Output Devices
Computer Memory
Secondary Storage Devices
cout in C++ Language

Comments (No)

Leave a Reply