CHAPTER 2 BASIC ELEMENTS OF C In this

CHAPTER 2 BASIC ELEMENTS OF C++

In this chapter, you will: · Become familiar with the basic components of a C++ program, including functions, special symbols, and identifiers · Explore simple data types and examine the string data type · Discover how to use arithmetic operators · Examine how a program evaluates arithmetic expressions · Learn what an assignment statement is and what it does · Discover how to input data into memory using input statements · Become familiar with the use of increment and decrement operators · Examine ways to output results using output statements · Learn how to use preprocessor directives and why they are necessary · Explore how to properly structure a program, including using comments to document a program · Learn how to write a C++ program

· A computer program or a program is a sequence of statements whose objective is to accomplish some task. · Programming is a process of planning and creating a program. · Cooking Recipe · It is usually easier to follow a recipe than to create one. · There are good recipes and there are bad recipes. · There are recipes which are easy to follow and there are those which are not. · There are recipes whose end results are reliable and there are those that are not. · One must have some knowledge of how to use cooking tools to follow a recipe to completion. · The same is true about programming

The Basics of a C++ Program · A C++ program is a collection of one or more subprograms, called functions. · Roughly speaking, a subprogram or a function (like a program) is a collection of statements and when it is activated (that is, executed) it accomplishes something. · Every C++ program has a function called main.

Example 2 -1 #include <iostream> using namespace std; int main() { cout<<"Welcome to C++ Programming"<<endl; return 0; } Output: Welcome to C++ Programming

· To write meaningful programs, we learn the special symbols, words, and the syntax rules of the programming language. · The syntax rules tell us which statements (instructions) are legal, that is, accepted by the programming language and which are not. · Programming Language: A set of rules, symbols, and special words. · Semantic- The semantic rules determine the meaning of the instructions. · Metalanguage- A language used to write the syntax rules.

Syntax template of the function main is: int main() { statement 1. . . statementn return 0; } • In these slides, reddish color indicate a part of the syntax that may or may not appear.

· The smallest individual unit of a program written in any language is called a token. Special-Symbols · The following are some of the special symbols +. <= ; != * ? == / , >= Word-symbols int, float, double, char, void, return · These are called reserved words, or keywords.

Identifier: A C++ identifier consists of letters, digits, and the under score character (_), and must begin with a letter or underscore. · C++ is case sensitive—uppercase and lower case letters are different. · Some of the predefined identifiers are cout and cin. · Unlike reserved words, pre-defined identifiers may be redefined, but it would not be wise to do so.

Example 2 -2 The following are legal identifiers in C++. first conversion pay. Rate counter 1

Data Types and Arithmetic Operators Data Type: A set of values together with a set of operations is called a data type. C++ data types fall into three categories • Simple Data Type. • Structured Data Type. • Pointers.

Simple Data Type C++ simple data can be classified into three categories 1. Integral, which is a data type that deals with integers, or numbers without a decimal part. 2. Floating-point, which is a data type that deals with decimal numbers. 3. Enumeration type, which is a user-defined data type.

Integral data types


int Data Type -6728, -67, 0, 78, 36782, +763, . . . • Positive integers do not have to have a + sign in front of them. • No commas are used within an integer. • In C++ commas are reserved for separating items in a list. So 36, 782 would be interpreted as two integers 36 and 782.

The bool Data Type • The data type bool has two values, true and false. The central purpose of this data type is to manipulate logical (Boolean) expressions. We call true and false the logical (Boolean) values. • In C++, bool, true, and false are reserved words.

char Data Type · char is the smallest integral data type. · char data type is used to represent characters, that is letters, digits and special symbols. · Each character is enclosed within single quote marks. Some of the values belonging to char data type are: 'A', 'a', '0', '*', '+', '$', '&' · Blank space is a character and is written ' ', with a space left between the single quotes.

The Most Common Character Sets: · ASCII (American Standard Code for Information Interchange) and EBCIDIC. · The ASCII character set has 128 values. · EBCIDIC has 256 values and is used by IBM.

ASCII Character Set · Each of the 128 values of the ASCII character set represents a different character. · The value 65 represents 'A', and the value 43 represents '+'. · Each character has a pre-defined ordering, which is called a collating sequence, in the set. · The collating sequence is used when you compare characters. · The value representing 'B' is 66, so 'A' is smaller than 'B'. · '+' is smaller than 'A' since 43 is smaller than 65. · · The first 32 characters in the ASCII character set are nonprintable. The 14 th character in the set is the new line character. In C++, the new line character is represented as 'n'. The horizontal tab character is represented in C++ as 't'. · The null character is represented as '