0% found this document useful (0 votes)
3 views

Keywords constants Operators

The document covers fundamental programming concepts including identifiers, keywords, constants, and operators in C++. Identifiers are names for variables and functions, while keywords are reserved words with predefined meanings. Constants are fixed values that cannot be altered, and operators perform operations on variables, categorized into arithmetic, logical, relational, bitwise, and assignment types.

Uploaded by

bfgiartsnaac
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Keywords constants Operators

The document covers fundamental programming concepts including identifiers, keywords, constants, and operators in C++. Identifiers are names for variables and functions, while keywords are reserved words with predefined meanings. Constants are fixed values that cannot be altered, and operators perform operations on variables, categorized into arithmetic, logical, relational, bitwise, and assignment types.

Uploaded by

bfgiartsnaac
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Topics to be

covered

 Identifiers
 Keywords
 Constants
 Operators
Identifiers
Identifiers refer to the name of variables, functions, arrays, classes, etc.
created by the programmer. They are the fundamental requirement of
any language.

Rules for naming identifiers:


 Identifier name can not start with a digit or any special character.
 A keyword cannot be used as an identifier name.
 Only alphabetic characters, digits, and underscores are permitted.
 The upper case and lower case letters are distinct. i.e., A and a are
different in C++.
 The valid identifiers are Sum, sum, Sum_of_Int, fun1, fun2,.
Keywords
Keywords are also known as reserved words.
Keywords are those words whose meaning is already
defined by Compiler and are always written or typed in
short(lower) cases. Keywords are words that the
language uses for a special purpose, such
as void, int, public, etc. It can’t be used for a variable
name or function name.
Keywords in C++ Language which are also available in C
language are given below:
auto break case char const contin default do
ue
double else enum extern float for goto if
int long registe return short signed sizeof static
r
struct switch typede union unsign void volatile while
f ed
A list of 30 Keywords in C++ Language which are not available in
C languageasmare givendynamic_cast
below. namespace reinterpret_c bool
ast
explicit new static_cast false catch
operator template friend private class
this inline public throw const_cast
delete mutable protected true try
typeid typename using virtual wchar_t
Constants
Constants refer to as fixed values that the program may not alter
during its execution. Constant must have to be initialized at the time of
creating it, and new values cannot be assigned later to it. These fixed
values are also called literals. Constants are treated just like regular
variables except that their values cannot be modified after their
definition. Constants can be of any of the basic data types and can be
divided into Integer Numerals, Floating-Point Numerals, Characters,
Strings and Boolean Values.

Defining Constants:
In C/C++ program we can define constants in two ways as shown below:

1. Using #define preprocessor directive


2. Using a const keyword
1. Using #define preprocessor directive: This directive is used to
declare an alias name for existing variable or any value. We can use this to
declare a constant as shown below:

#define identifierName value

• identifierName: It is the name given to constant.


• value: This refers to any value assigned to identifier Name.

#include <iostream>
using namespace std;
#define VAL1 20 OUTPUT:
#define VAL2 6 Sum of 20 and 6
int main() is: 26
{
int total;
total = VAL1 + VAL2;
cout << “Sum of”<<VAL1<<“and”<<VAL2<<“is:”<<total;
}
2. Using a const keyword: Using const keyword to define
constants is as simple as defining variables, the difference is
you will have to precede the definition with a const keyword.

void main()
{
//const int SIDE; -> error
const int SIDE = 50;
// SIDE=30; -> error OUTPUT:-
The area of the square with side 50
int area;
is 2500
area = SIDE*SIDE;
cout<<"The area of the square with side: " << SIDE <<"
is: " << area;
}

It is also possible to put const either before or after the type.


int const SIDE = 50;
OR
const int SIDE = 50;
Constant/Literal
 Integer LiteralsTypes
An integer literal can be a decimal, octal, or hexadecimal constant. A
prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal,
and nothing for decimal.
For e.g.-
212 ,215u, 0xFC // Legal
078 // Illegal: 8 is not an octal digit

 Floating-point Literals
A floating-point literal has an integer part, a decimal point, a fractional
part, and an exponent part. You can represent floating point literals either
in decimal form or in exponential form.
For e.g.-
3.14159, 314159E-5L, // Legal
510E // Illegal: incomplete exponent
210f // Illegal: no decimal or exponent
.e55 // Illegal: missing integer or fraction
 Boolean literals

There are two Boolean literals and they are part of standard C++ keywords −
A value of true representing true.
A value of false representing false.
You should not consider the value of true equal to 1 and value of false equal to 0.

 Character Literals

Character literals are enclosed in single quotes. If the literal begins with L (uppercase
only), it is a wide character literal (e.g., L'x') and should be stored in wchar_t type of
variable . Otherwise, it is a narrow character literal (e.g., 'x') and can be stored in a
simple variable of char type.
A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'),
or a universal character (e.g., '\u02C0’).

 String Literals
String literals are enclosed in double quotes. A string contains characters that are
similar to character literals: plain characters, escape sequences, and universal
characters.
You can break a long line into multiple lines using string literals and separate them
OPERATORS

Operators are symbols which act on variables or other entities that are called operands
and perform mathematical or logical operations to modify their values and produce
results.
For example, consider the below statement:

c = a + b;
Here, ‘+’ is the operator known as addition operator and ‘a’ and ‘b’ are operands. The
addition operator tells the compiler to add both of the operands ‘a’ and ‘b’.
C/C++ has many built-in operator types and they are classified as follows:
1. Arithmetic Operators: These are the operators used to perform
arithmetic/mathematical
operations on operands. Examples: (+, -, *, /, %,++,–). Arithmetic operator are of
two types:
Unary Operators: Operators that operates or works with a single operand are unary
operators. For example: (++ , –)
Binary Operators: Operators that operates or works with two operands are binary
operators. For example: (+ , – , * , /)
Now Consider the following program:

void main()
{
int op1=3,op2=4;
float op3=10.1,op4=5.4;
int x=4,y;
y = ++x;
cout<<"PreIncrement: Value of x = "<<x;
cout<<"Operands are op1 = "<<op1<<" op2 = "<<op2;
cout<<" op3 = "<<op3<<" op4 = "<<op4;
cout<<“\nop1 + op2 = "<<op1+op2;
cout<<“\nop1 - op2 = "<<op1-op2;
cout<<“\nop3 * op4 = "<<op3*op4;
2. Logical Operators: Logical Operators are used to combine two or more conditions
or to
complement the evaluation of the original condition in consideration. The result of the
operation
Operato
Description
of ar logical operator is a boolean value either true or false. For example consider the
following table:
&& Logical AND: returns true if both conditions are true otherwise returns false.

|| Logical OR: returns true if one of the conditions is true. Returns false when both conditions are
false.

!void main()Logical NOT: negates the condition.


{
int a=10, b=8,c=12,d=14;
if(!(a==0))
cout<<"a is not zero";
else
cout<<“\n a is zero";
if((a>b)&&(c<d))
cout<<“\nLogical AND is true";
else
cout<<“\nLogical AND is false";
if((a<c)||(b<d))
cout<<"Logical OR is true";
}
3. Relational Operators: These are used for comparison of
the values of two operands. For example, checking if one
operand is equal to the other operand or not, an operand is
greater than the other operand or not etc. Some of the
relational operators are (==, >= , <= ).

Following are the Relational operators supported by C++:

Operator Description
‘==' Evaluates whether two operands are equal. Returns true if equal else returns
false.
!=(not equal to) Complements ‘equal to’ operator. Returns true if operands are not equal. False
otherwise.
<(less than) Returns true if the first operand is less than second. False otherwise.
<=(less than equal to) Returns true if the first operand is less than or equal to the second operand.
False otherwise.
>(greater than) Returns true if the first operand is greater than second. False otherwise.
>=(greater than equal to) Returns true if the first operand is greater than equal to the second. False
otherwise.
4. Bitwise Operators: The Bitwise operators is used to perform bit-
level operations on the operands. The operators are first converted to bit-
level and then the calculation is performed on the operands. The
mathematical operations such as addition, subtraction, multiplication etc.
can be performed at bit-level for faster processing.

Following are the bitwise operators supported by C++:


Operators Description
&( Binary AND) Performs AND operation on bits of operand 1 and operand 2.

|( Binary OR) Performs OR operation on bits of operand 1 and operand 2.

^( Binary XOR) Performs XOR operation on bits of operand 1 and operand 2.

~( Binary one's Takes one operand and inverts its bits.


complement)
<<( Binary left shift Shifts bits of the first operand to the left to a number of bits specified by the
operator) second operand.

>>( Binary right shift Shifts bits of the first operand to the right to a number of places specified by the
operator) second operand.
5. Assignment Operators: Assignment operators are used to
assign value to a variable. The left side operand of the
assignment operator is a variable and right side operand of
the assignment operator is a value. The value on the right side
must be of the same data-type of variable on the left side
otherwise the compiler will raise an error.

The below table gives us a description of these


assignment operators.
Operator Description
= Assigns the value of RHS operand to LHS operand

+= Adds RHS operand to LHS operand and assigns the result in LHS operand.

-= Subtracts RHS operand to LHS operand and assigns the result to LHS
operand
*= multiplies RHS operand to LHS operand and assigns the result to LHS
operand
/= divides RHS operand to LHS operand and assigns the result to LHS operand
Other Operators: Apart from the above operators there are some other
operators available in C or C++ used to perform some specific task.
Some of them are discussed here:

1. sizeof operator: It is a compile time unary operator which can be


used to compute the size of its operand/variable.

2. Comma Operator: The comma operator (represented by the token ,)


is a binary operator that evaluates its first operand and discards the
result, it then evaluates the second operand and returns this value (and
type). The comma operator has the lowest precedence of any C operator.
Comma acts as both operator and separator.

3. Conditional Operator/Ternary Operator: Conditional operator is of


the form:
Expression1 ? Expression2 :
Expression3 .
Here, Expression1 is the condition to be evaluated. If the
Thanks

You might also like