Operators in C++

The operators are different special symbols used for indicating an operation to be performed.
The operator is the special symbols that tell the compiler to do task related to 1)Mathematical and 2)Logical Manipulation .C++ Operator can be classified into three sections such as:

Unary Operators
Binary Operators
Ternary Operators

It also includes formatted and unformatted functions such as:

Formatted IO FUNCTIONS
UNFormatted IO FUNCTIONS

Unary Operators:

 These are operators that require only one operand.

1. Unary minus(-): It gives the negative value of the variable to which it is preceded.
for e.g: x=3,y=6 then
y= -x; (semicolon(;) are the terminator required in every statement)
y=-3.
2. CASTING OPERATOR (()) or type Conversion: It is used to convert a data one type to another data type.
for e.g: int x=3;
float y=5.6;
x=(int)y;
x=5
3. Logical not operator(!): It performs the logical not of the result obtained from the expression on its right.
x=1,y=!x;
y having the value 0.
4. Address of operator(&): It returns the address of the variable
for e.g: y=&x;
5 . value operator(): return the data at a given address for e.g:z=y;
6 . scope resolution operator(::): It resolves the scope of a variable to be inside or outside the function.
for e.g:y=::x;
7. Decrement Operator(-): It returns the value of the variable subtracted with one and stores the result
for e.g:x=5
then x–;
will make the value x equal to 4.
8. Increment Operator(++): It returns the value of the variable added with one and stores the result in the variable itself.
for e.g:x=5
will make the value of x equal to 6.
9. Bitwise, not operator(~): this operator is used to perform bitwise, not operation
for e.g: x=3
then y=~x;
will result in y having a value of -4

Binary Operators:

 Operators that require two operands are called binary operators. these operators are further classified into various types namely the Arithmetic operators, Logical operators, Bitwise operators, and Relational operators.

1.Arithmetic operators :

This set includes the basic arithmetic operators to perform basic arithmetic operations like addition(+),subtraction(-),multiplication(*),division(/).

Note that any fractional part in integer division is discarded so no rounding occurs No parentheses are required. The multiplication is applied first because multiplication has a higher precedence than addition.

2. Bitwise operators:

These operators work bitwise on data. They perform different operations on bits of data like AND, OR, EXOR and NOT.

Bitwise Operator
Bitwise Operator

3.Logical operators:

logical operators follow the same truth table as for the bitwise operators they are used to check condition instead of operation on data.

 Logical operators:
Logical operators:

4.Relational operators:

The relational operators are used to test the relation between two variables or variables or a variable and constant.

 Relational operators:
Relational operators:

Confusing the equality operator == with the assignment operator = can result in logic errors.The equality operator should read “is equal to,” and the assignment operator should be read “gets” or “gets” or “gets the value of” or “is assigned the value of.” Some people prefer to read the equality operator as “double equal”.As we will soon see, confusing these operators may not necessarily cause an easy-to-recognize syntax error, but may cause extremely subtle logic errors.

Ternary/conditional operators:

An operator that requires three operands is called a ternary operator.there is only one ternary operator in C.This operator is used to check a condition and accordingly do one of the two things based on the condition being true or false.
the syntax of this operator is :

(condition)?<value if condition is true:<value if condition is false>;

Formatted IO FUNCTIONS:

Formatted IO FUNCTIONS in C++
Formatted IO FUNCTIONS IN C++

1.printf()

Syntax: printf(“Format string”, list of variables or expressions) The format string can contain the following:

a)The character set(A-Z,a-z,0-9, all special symbols on the keyboard)
b)Blank space
c)Escape sequence
d)Field width the width of value can be fixed by specifying the field width.
e)Format specifiers, It specifies the format of the variable data whose value is to be displayed

Examples:

1.printf(“The number of buildings is %d”,b);
This statement will print the output with the format specifier replaced with the value of b
2.printf(“The simple will print the output where the format specifier %f will be replaced with the float type value of the variable si“)
3.printf(“The simple interest is %5.2f”,si);
The format specifier in this statement is accompanied by a field width i.e. 5.2(%5.2f). which indicates five digits before the fraction point and 2 digits after the fraction point. This statement will display the output wherein the value of si will be displayed with five digits after the fraction point

UNFORMATTED IO FUNCTIONS :

UNFORMATTED IO FUNCTIONS
UNFORMATTED IO FUNCTIONS IN C++

1.getch(): This function is used to accept one character from the user
2.getche(): This function is used to accept one character from the user and echo it(display it on the screen)
3.getchar(): This function is used to accept one character from the user and echo it(display it on the screen) and also wait after that for the user to press the enter key
4.gets(): This function is used to accept a string from the user.
5.putch(): These functions are used to display a character on the monitor. or apply putchar()
6.puts(): This function is used to display a string on the monitor

Related Article

Recommended Post:


Translate »