Operators in C

Page Contents:

What are Operators in C Language ? with Examples of bitwise operators
Unary Operators :
Binary Operators :
Ternary/conditional operators:
Formatted IO FUNCTIONS:
UNFORMATTED IO FUNCTIONS :

What are Operators in C Language ? with Examples of bitwise operators

  • Especially the data on which the operation is performed is called as operands
  • If an operator requires one operand, it is called a Unary operator
  • Similarly, an operator requires two operands is called Binary operators and Also if it requires three operands is called Ternary operators. Hence Types of operation in Binary operators are :

Examples bitwise operator:

Click here

The operators are one of the tokens of C.There are different types of operators classified based on their functions Namely the following below.

Unary Operators :

These Are Operators That Require Only One Operand.

Types of Operator require One Operand are:

1.Unary minus(-): Especially It gives the negative value of the variable to which it preceded.
For example : x=3,y=6
y= -x; (semicolon(;) are the terminator required in every statement)
y=-3


2. CASTING OPERATOR (()) or type Conversion: Casting is used to convert a data one type to another data type.
For example : int x=3;
float y=5.6;
x=(int)y;
x=5

3. Logical not operator(!): Especially 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(&): Especially It returns the address of the variable
For example
y=&x;


5 . value operator(): Especially it Returns the data at a given address for e.g:z=y;


6 . scope resolution operator(::): Especially It resolves the scope of a variable to be inside or outside the function.
For example y=::x;


7. Decrement Operator(-) :
Especially It returns the value of the variable subtracted with one and stores the result
For example x=5
So then x–;
will make the value x equal to 4.


8. Increment Operator(++): Especially It returns the value of the variable added with one and stores the result in the variable itself.
For example x=5
will make the value of x equal to 6.


9. Bitwise not operator(~): This operator used to perform bitwise not operation
For example x=3
So then=~x;
will result in y having a value of -4

Binary Operators :

  • Accordingly, Operators that require two operands 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 : Arithmetic operator set includes the basic arithmetic operators to perform basic arithmetic operations like addition(+),subtraction(-),multiplication(*),division(/),(%)modulus.

What arithmetic operation do?
What arithmetic operation do?

For example:

1.2*2=4;

2. For int type of data, 5/3=1;

For float type of data, 5/3=1.67

3.5%3=2;

4.2+2=4;

5.3-2=1;

2. Bitwise operators :

  • These operators work bitwise on a data
  • Bitwise operators work bitwise on data. Also, they perform the different operation on bits of data like AND, OR, EXOR and NOT
  • These operators are used to perform bitwise binary operations on the data
  • As we have addition, subtraction operations in decimal data for performing arithmetic operations; similarly AND, OR, NOT are basics bitwise operations on the binary data.
bitwise in C
bitwise in C

Examples of bitwise operator

For an example of & operation

1.5 & 3 = 1

(5)decimal = (0 1 0 1)binary

(3)decimal = (0 0 1 1)binary


(0 0 0 1)binary = (1)decimal

For an example of | operation

2. 12|9 =13

(12)decimal = ( 1 1 0 0 )binary

(9)decimal = ( 1 0 0 1 )binary


( 1 1 0 1 )binary = (13)decimal

For example of ^ operation

4. 8 ^10 = 2

(8)decimal = ( 1 0 0 0 )binary

(10)decimal = ( 1 0 1 0 )binary


( 0 0 1 0 )binary = (2)decimal


3. Logical operators :

logical operator in C
the logical operator in C
  • Logical operators follow the same truth table as for the bitwise operators Also they are used to check the condition
  • After all the logical operators are AND and OR.the symbol used for these operators in C are && and | | respectively
  • For example a statement y > 5 && y < 10; will result in “true” i.e. 1 if the value of y is greater than 5 AND less than 10, else it will result in false i.e 0
  • Another example a statement y > 5 | | y==2; will result in “true” i.e. 1 if the value of y is greater than 5 OR equal to 2, else it will result in false i.e. 0
  • Logical operators will be understood in more detail with the expressions and program examples followed by this section.


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

The relational operator in C
The relational operator in C

Ternary/conditional operators:

An operator that requires three operands is called a ternary operator. Therefore 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 function
formatted function in C

There are two formatted IO functions in C namely printf() to display data on monitor and scanf() to accept data from the keyboard

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

2.scanf()

Syntax: scanf(“format string”,address of variables);
Here, format string can contain format specifier, field width and assignment suppression character. The assignment suppression character(*) is used to discard one of the user-entered values.
The address of the variable is obtained with the help of the address-of operator(&).

UNFORMATTED IO FUNCTIONS :

unformatted functions
unformatted functions in C

The unformatted IO functions do not have any format specifier. They are mostly used to accept and display only one character or a string (String is a set of characters to make a word or sentence).

The different unformatted IO functions are listed below;

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

Translate »