Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Standard output

By default, the term standard output refers to the output displayed on


monitor. C++ uses the cout stream object to display standard output.
The word cout stands for console output. The cout object is used with
insertion operator(<<).The message or the value of the variable
followed by the insertion operator is displayed on the screen.
Syntax
Cout<<variable/constant/expression;
Example:
Cout<<“welcome”;
Standard Input
By default, the term standard output refers to the input given by
keyboard.C++ uses cin stream object to get standard input. The word
‘cin’ stands for console input.C++ handles standard input by applying
the extraction operator(>>). The operator must be followed by a
variable. The variable used to store the input data.
Syntax
Cin>>var;
Example:
Cin>>x;
Cin>>a>>b>>c;
Identifier
A variable name eg. Number1 is any valid identifier that is not a
keyword.An identifier is a series of characters consisting of letters,
digits and underscores(_) that does not begins with a digit.C++ is case
sensitive– uppercase and lowercase letters are different ,so a1 and A1
are different identifiers.
Some key points for identifier name are as follows:
• Choosing meaningful identifier name makes a program self
documenting.
• Avoid using abbreviations In identifiers. This improves program
readability.
Continue
• Do not use identifiers that begin with underscores and double
underscores.
Types of identifiers
There are two types of identifiers in C++ language:
• Standard Identifiers
• User-defined Identifiers
Standard Identifiers:
A type of identifiers that has special meaning in C++ is known as
standard identifier.
Continue
Example:
Cout and cin are examples of standard identifiers.
User-defined Identifiers:
The type of identifier that is defined by the programmer to access
memory location is known as user-defined identifier.
Example:
Marks and age are examples of user defined identifiers.
Constants
Constant is a quantity that cannot be changed during program
execution.Following are two types of constants in C++:
• Literal constant
• Symbolic constant
Literal constant
A literal constant is a value that is typed directly in a program.
Example:
The following statement contains string literal constant hello world:
Cout<<“hello world”;
Int age = 19;
Types Of literal Constants
Different types of literal constants are as follows:
➢ Integer Constant
➢ Floating Point Constant
➢ Character Constant
➢ String Constant

1. Integer Constant
Integer Constant are numeric values without fraction or decimal point.
Both positive and negative integer constant are used.
Example: Some examples are as follows
• 87
• -5
2. Floating Point Constant
Floating point constant Are numeric values with fraction or decimal point. Both
positive and negative floating point constant are used.
Example
15.32

3. Character Constant
Any characteristic written in single quotes is known as character constant.
Example:
Some examples are as follows:
‘A’ ‘=‘ ‘$’
String Constants
A collection of characters written in double quotations is called string
of string constant.It may consist of any alphabetic characters ,
digits and special symbols.
Examples:
Some examples of string constants are as follows:
“Faisalabad” “456” “22, gulshan colony”
4. Symbolic Constant
A symbolic constant is a name given to values that cannot be changed. A
constant must be initialised. After a constant is initialised, its value cannot be
changed. Symbolic Constant are used to represent a value that is frequently
used in a program.
Symbolic Constant can be declared in two ways.
• Const Qualifier
• Define Directive

Const Qualifier
Const Qualifier is used to define a constant. The constant is declared by
specifying its name and data type.
Syntax
The syntax of declaring constant with const Qualifier is as follows:
const data_type identifier = value;

Const. It is a keyword used to define a constant


Data_type. It indicates the data type of constant
Identifier. It represents the name of the constant
Value. It represents the value with which the constant

Example
Following is an example of constant declaration
Cont int N= 100;
• Define Directive
Define Directive is also used to define a constant. The difference between the
const qualifier and define directive does not specify the data type of the
constant. Define directive starts with a symbol #.

Syntax
The syntax of define directive is as follows:
#define. Identifier value
# It indicates the start of preprocessor directive
Define It is used to define a constant
Identifier It is the name of the constant
Value It represents the value associated with the identifier

Example:
#define PI 3.141593
• Program 3.2
Write a program that inputs the radius of a circle and displays the circumference by using formula
2pieR. Store the value of pie in a constant by using DEFINE directive

#include <iostream.h>
#include <conio.h>
#define PI 3.141
Void main()

Float r, area
Clrscr();
cout<<“Enter radius: “:
cin>>r;
area= 2.0 * PI * r;
cout<<“Area = “<<area;
getch();
Type Casting
The process of converting the data type of a value during execution is known
as type casting. Type casting can be performed in two ways:
• Implicit type casting
• Explicit type casting

Implicit type casting


Implicit type casting is performed automatically by the C++ compiler. The
operands in arithmetic operation must be of similar types. An expression,in
which operands are of different is called mixed-type expression. In this case,
the results of an expression is evaluated to larger data type in the expression.
Different types of variables are as follows:

1. Long double
2. Double
3. Float
4. Long
5. int
6. Char
Explicit Casting
Explicit casting is performed by programmer. It is performed by using
cast operator. The cast operator tells the computer to convert the data
type of value.
Syntax
The syntax of using cast operator is as follows:
(type)expression;

Example:
(int) x % (int) y
Program
Write a program that divides two float variables
and finds the remainder by using explicit casting.
#include <iostream.h>
#include <conio.h>
Void main()
{
clrscr();
float a, b;
int c;
a = 10.3
b = 5.2;
c = (int)a % (int)b;
cout<<“Result is<<c;
getch();
}
Comments
Comments are the lines of the program that are not executed. The
compiler ignores comments and does not include them in an executable
program. That is why the comments do not affect the size of the
executable program
Comments can be added anywhere in two ways:
1. Single line comments : Comments on single line are added by using
double dash slash “ //“.
2. Multi line comments: Multi line comments are inserted to the code
by placing /* at the beginning of the comments. The character */ is
used to end multi-line comments.
Scope
The area where a variable can be accessed is known as scope of
variable.

You might also like