Keywords constants Operators
Keywords constants Operators
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.
Defining Constants:
In C/C++ program we can define constants in two ways as shown below:
#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;
}
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.
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.
>>( 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.
+= 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: