OPERATORS IN C/C++
C/C++ programs are made with the chief intention of solving a
certain task. These tasks can be from simple day to day calculations to complex
algorithms. But in all these tasks one thing that is common is that we need to
compute certain expressions. We need to take inputs from user, then calculate
the desired result and output the result on the screen. All these actions are
completed using special tokens called the Operators.
OPERATORS IN C/C++
The operators in C++ are classified into many categories. In
this article we will learn about them in very short.
In a program, we perform various operations on data. Operators
let us perform operations on your data.
C++’s rich set of operators comprise of I/O, arithmetic,
relational, logical and certain other types of operators.
I/O Operators :
Input coming from the user’s terminal, referred to as standard
input is tied to the predefined iostream cin and output directed to
the user’s terminal, referred to as standard output, is tied to the
predefined iostream cout.
Output operator (“<<”) : The output operator (“put to”)
is used to direct a value to standard output. For instance,
cout<< “The sum of 6 & 2
is ”;
cout<< 6+2;
Output :
The sum of 6 & 2 is 8
Input Operator (“>>”) : The input operator (“get from”)
is used to read a value from standard input.
int n;
cin>> n;
This takes input from the user till he hits enter.
Also, many of these operators can be used together to take
many inputs or to give many outputs. (It’s called Cascading)
cout<< “Learn ” << “to ”
<< “code ”;
Output :
Learn to code
Arithmetic Operators :
To do arithmetic, C++ uses operators. It provides operators
for five basic arithmetic calculations : addition, subtraction, multiplication,
division and modulus which are +, -, *, /, % respectively.
Each of these operators is a binary operator i.e. it requires
two values (operands) to calculate a final answer. Apart from these binary
operators, C++ provides two unary arithmetic operators : unary plus and unary
minus.
The arithmetic operators in C++ have the same function as they
have in mathematics. The modulus operator (%) produces the remainder of
dividing the first operand by second. But both the operands must be integer
types here. Rest all other operators can work with integer and floating-point
values.
Let us see the workings briefly :
cout<< 8+6<< “ ”;
cout<< 8-6<< “ ”;
cout<< 8*6<< “ ”;
cout<< 8/6<< “ ”;
cout<< 8%6;
Output : 14 2 48 1 2
Note
that 8/6 yields 1 and not 1.33. This is due to the fact that both operands 8
& 6 are integers and so the entire expression works as integers. We can
understand it as 8 = 6 * (1) + 2; So, the integer division yields 1 and the
modulus yields the remainder 2.
Increment/Decrement Operators :
The increment operator,
++, adds 1 to its operand and the operator – subtracts 1 from its operands.
That is, a = a+1 is equal to a++ (or ++a) and a = a-1 is equal to a—(or --a).
The two forms are
called postfix and prefix.
Postfix : a++ or a—
Prefix : ++a
or
--a
The postfix form of ++,
-- operators follow the rule use-then change. For example,
int
sum = 6;
int
c = 2;
sum
= sum + (c++);
cout<<
“Sum = ” <<sum<<endl;
cout<<
“ c = ” <<c;
Output : Sum = 8
c = 3
The prefix form of
++,-- operators follow the rule change-then-use. For example,
int
sum = 6;
int
c = 2;
sum
= sum + (++c);
cout<<
“Sum = ” <<sum<<endl;
cout<<
“ c = ” <<c;
Output : Sum = 9
c = 3
Note
:
Increment/Decrement
operators have a higher precedence than the arithmetic operators.
Post
fix form is at higher precedence than prefix form.
Relational Operators :
Relational operators
determine the relation among different operands. C++ provides six relational
operators for comparing numbers and characters. But they do not work on arrays.
Each of these operators
is binary and returns a Boolean value (i.e. either true or false). If an expression
is true, it returns true (or 1) and if the expression is false, it returns
false (or 0).
The six relational
operators are :
< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to)
== (equal to)
!= (not equal to)
Logical
Operators :
C++ provides for 3
logical operators to combine existing expressions. These are || (logical OR),
&& (logical AND) and ! (logical NOT). Let us examine them :
The logical OR operator (||)
: It evaluates true if any of the
operands evaluate true, else false.
The logical AND operator(&&)
: It combines two expressions together
just like OR. The resulting expression has the value 1 (i.e. true) only if both
of the original expressions (its operands) are true.
The logical NOT operator (!)
: This operator, unlike the other two,
works on a single expression (operand),i.e. it is a unary operator. It negates
or reverses the truth value of an expression, i.e. if the expression is true,
then ! (expression) is false and vice versa.
Conditional Operator :
C++ offers one
conditional operator, which is also popularly called as ternary operator as it
needs 3 operands to work.
General syntax :
If expression 1 is
true, then the value of the whole expression is the value of expression 2,
otherwise, the value of the whole expression is the value of expression 3.
Example :
int
marks = 58; char grade;
grade
= marks>=50 ? ‘P’ : ‘F’ ;
cout<<
“The grade is ”<<grade;
Output : The grade is P
int
marks = 48; char grade;
grade
= marks>=50 ? ‘P’ : ‘F’ ;
cout<<
“The grade is ”<<grade;
Output : The grade is F
Note
: The value holding variable (here grade) must be of the correct data type.
Some other Operators :
The Compile-Time Operator
sizeof() : It is a unary
compile-time operator that returns the length (in bytes) of the variable or any
type-specifier which is put within the parenthesis.
It can be used 2 forms
:
The Comma Operator : A comma operator is used to string
together several expressions. The group of expressions separated by commas (,)
is evaluated left-to-right in sequence and the result of the right-most
expression becomes the value of the total comma-separated expression.
For example,
int
a=3,b;
b
= (a+1,a+3);
cout<<b;
Output : 6
Evaluating left to
right, a+1 = 4; then a+3 = 6. As a+3 is the rightmost expression, it is stored
in b.
Which topics you want an article on? Comment down on this
post.
Do not stop here. Read all articles related to C++ here.
If you want to contribute any article for the website, feel
free to share it on coding.nkcoder@gmail.com. We will
publish it on the website with your name.
Download the pdf form of this article here.
Post a Comment