Professional Documents
Culture Documents
Presentation New
Presentation New
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;
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
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.