Keywords Identifiers Constant in C++
Keywords:
The Keywords
implement specific C++ language features. They are explicitly reserved
identifiers and cannot be used as names for the program variables or other
user-defined program elements.
Many of
Keywords are common to both C and C+.
Example:
break |
auto |
case |
float |
If |
public |
short |
while |
Int |
Identifiers:
Identifiers
refer to the names of variables, functions, arrays, classes , etc. created by
the programmer. They are the fundamental requirement of any language. Each
language has its own rules for naming these identifiers. The following rules
are common to both C and C++.
·
Only
alphabetic characters, digits and underscore are permitted
·
The
name cannot start with a digit.
·
Uppercase
and lowercase letters are distinct.
·
A
declared keyword cannot be used as a variable name
Constant:
Constant
refer to fixed values that do not changes during the execution of a program.
Like C, C++
supports several kinds of literal constants. They include integers, characters,
floating point numbers and strings. Literal constant do not have memory
location.
Example:
How constants
defined in C++?
There are two ways of
creating Constants in C++:
- Using the qualifer const
- · Defining a set of integer constants using enum keyword
In C
and C++ both , any value declared as const cannot be modified by program in any
way.
Example:
Using
const
Const int num=10;
Second
method of naming integer constants is by
enumeration as under;
enum
{ X,Y,Z}
This
defines X,Y and Z as integer constants with values o,1 and 2 respectively.
This
equivalent to:
const
x=0;
const
y=1;
const
z=2;
We
can also assign values to X,Y and Z explicitly.
Example:
Enum{X=100.
Y=500,Z=200};
program: Write a program to swap two number.
using namespace std;
int main()
{
int num1,num2,temp;
cout<<"Enter the two number ";
cin>>num1;
cin>>num2;
cout<<"The number before swaping is num1="<<num1;
cout<<" and second num2="<<num2;
temp=num1;
num1=num2;
num2=temp;
cout<<"\n The number after swaping is num1="<<num1;
cout<<" and second num2="<<num2;
}
OUTPUT:
Enter the two number 12
26
The number before swaping is num1=12 and second num2=26
The number after swaping is num1=26 and second num2=12
----------------------------------------------------------------------
Comments
Post a Comment