C programming operator
There are many types of operators in C programming for completing arithmetic, conditional and bitwise operations. In this chapter, you will learn about different types of C operators and their use.Operator is a type of symbol that can operate the value or variable. For example: + (plus sign) is an operator used for adding.
There are many types of operators in C programming for performing different types of operations. Operators can be divided into the following ways
C programming operator
Arithmetic Operator - Arithmetic Operator
Increment and Decrement Operator
Assignment Operator - Assignment Operator
Relational Operator - Relational Operator
Logical Operator - Logical Operator
Conditional Operator - Conditional Operator
Bitwise Operator - Bitwise Operator
Special Operator - Special Operator
C arithmetic operator
The arithmetic operator performs mathematical calculations such as addition, subtraction, multiplication and division.
Examples of operator operator mean (int a = 11, b = 5)
+ Yoga or unary add a + b 16
- minus or unary minus a - b 6
* Gon a * b 55
/ Share a / b 2
After the share of the result the result (modulo operator) a% b 1
Example: Arithmetic Operator
This example shows how the arithmetic operator works in // C program.
#include <stdio.h>
int main ()
{
int a = 11, b = 5, c;
c = a + b;
printf ("a + b =% d \ n", c);
c = a-b;
printf ("a-b =% d \ n", c);
c = a * b;
printf ("a * b =% d \ n", c);
c = a / b;
printf ("a / b =% d \ n", c);
c = a% b;
printf ("Remainder when a divided by b =% d \ n", c);
return 0;
}
Output
a + b = 16
a-b = 6
a * b = 55
a / b = 2
Remainder when a divided by b = 1
+, - and * operator correctly add, subtract and multiply respectively.
Normally, if you divide 11 by 5 (11/5), then get a share of 2.2. But the output of our program was found 2.
Because the type integers of both variables. So the output has been an integer. The compiler has cut the decimal number after the decimal number, so the result is 2 instead of 2.2.
The contents are available through the modulo operator%. In the above example, a = 11 is divided by b = 5, the result is 1. The% operator is used only for the integer.
Suppose a = 10.0, b = 4.0, c = 10 and d = 4, then C programming
a / b = 2.5 // due to both the operand floating point variables.
a / d = 2.5 // because an operand floating point variable.
c / b = 2.5 // because an operand floating point variables
c / d = 2 // Because both operated integer variables
Increment and decrement operator
In order to increase or decrease the constellation or variable of the variable 1, there are two operators in C programming, respectively increment ++ and the other is decrement.
Increment increment values of increment operator simultaneously, decreasing the decrement operator value simultaneously. These two operators are called unary operators because they can operate single operands.
Examples: Increment and decrement operator
This example shows how the increment and decryment operator works in // C program.
#include <stdio.h>
int main ()
{
int a = 15, b = 150;
float c = 50.5, d = 80.5;
printf ("++ a =% d \ n", ++ a);
printf ("- b =% d \ n", --b);
printf ("++ c =% f \ n", ++ c);
printf ("- d =% f \ n", --d);
return 0;
}
Output
++ a = 16
--b = 149
++ c = 51.500000
--d = 79.500000
Here the operator is used as ++ and - prefix. This operator can also be used as two postfix. For example - a ++ and a--. To know more about increment and decryption operators, please visit the C and C ++ Decrease operator section.
C assignment operator
Assignment operators are used to assign or store value to variables. The most used assignment operator is =
Operator Example (int a = 11, b = 5) Similar results
= a = b a = b 5
+ = a + = b a = a + b 16
- = a - = b a = a-b 6
* = a * = b a = a * b 55
/ = a / = b a = a / b 2
% = a% = b a = a% b 1
Example: Assignment operator
This example shows how the assignment operator works in // C program.
#include <stdio.h>
int main ()
{
int a = 10, c;
c = a;
printf ("c =% d \ n", c);
c + = a; // c = c + a
printf ("c =% d \ n", c);
c - = a; // c = c-a
printf ("c =% d \ n", c);
c * = a; // c = c * a
printf ("c =% d \ n", c);
c / = a; // c = c / a
printf ("c =% d \ n", c);
c% = a; // c = c% a
printf ("c =% d \ n", c);
return 0;
}
Output
c = 10
c = 20
c = 10
c = 100
c = 10
c = 0
C relational operators
The relational operator in C programming evaluates the relation between two operands. If the relation is true then returns 1; Returns 0 if the relation is false.
Relational operators are used in decision making and loop.
Operator operator means examples (int a = 11, b = 5)
== Equal to a == b False
> Greater than a> b True
<Less than a <b False
! = Not equal to a! = B True
> = Greater than or equal to a> = b True
<= Less than or equal to a <= b False
Example: Relational operators
This example shows how the Relational Operator works in // C program.
#include <stdio.h>
int main ()
{
int a = 5, b = 5, c = 10;
printf ("% d ==% d =% d \ n", a, b, a == b); // true
printf ("% d ==% d =% d \ n", a, c, a == c); // false
printf ("% d>% d =% d \ n", a, b, a> b); // false
printf ("% d>% d =% d \ n", a, c, a> c); // false
printf ("% d <% d =% d \ n", a, b, a <b); // false
printf ("% d <% d =% d \ n", a, c, a <c); // true
printf ("% d! =% d =% d \ n", a, b, a! = b); // false
printf ("% d! =% d =% d \ n", a, c, a! = c); // true
printf ("% d> =% d =% d \ n", a, b, a> = b); // true
printf ("% d> =% d =% d \ n", a, c, a> = c); // false
printf ("% d <=% d =% d \ n", a, b, a <= b); // true
printf ("% d <=% d =% d \ n", a, c, a <= c); // true
return 0;
}
Output
5 == 5 = 1
5 == 10 = 0
5> 5 = 0
5> 10 = 0
5 <5 = 0
5 <10 = 1
5! = 5 = 0
5! = 10 = 1
5> = 5 = 1
5> = 10 = 0
5 <= 5 = 1
5 <= 10 = 1
C logical operator
C programming A &&, || And! Operators are called logical operators. In the decision-making process, the use of logical operators in C programming is used.
Operator operator means examples (int a = 11, b = 5, c = 15)
&& Logial AND - True if both operands are true (a == b) && (b> c) True
|| Logical OR - True if any one operand is true (b == c) || (a> c) False
! Logical NOT - True if false is abbreviated! (B == c) True
Example: Logical operator
This example shows how logical operators work in the // C program.
#include <stdio.h>
int main ()
{
int a = 5, b = 5, c = 10, result;
result = (a = b) && (c> b);
printf ("(a = b) && (c> b) equals to% d \ n", result);
result = (a = b) && (c <b);
printf ("(a = b) && (c <b) equals to% d \ n", result);
result = (a = b) || (c <b);
printf ("(a = b) || (c <b) equals to% d \ n", result);
result = (a! = b) || (c <b);
printf ("(a! = b) || (c <b) equals to% d \ n", result);
result =! (a! = b);
printf ("! (a == b) equals to% d \ n", result);
result =! (a == b);
printf ("! (a == b) equals to% d \ n", result);
return 0;
}
Output
(a = b) && (c> b) equals to 1
(a = b) && (c <b) equals to 0
(a = b) || (c <b) equals to 1
(a! = b) || (c <b) equals to 0
! (a! = b) equals to 1
! (a == b) equals to 0
The above example explains
(a = b) and (c> b) because both operands are true (a = b) && (c> 5) the result is 1.
Because the value of the operand (c <b) is false, (a = b) && (c <b) the result is 0.
Because the value of the operand (a = b) is true (a = b) || (c <b) the result is 1.
(a! = b) and (c <b) because both operands are false (a! = b) || (c <b) has resulted in 0.
Because the value of the operand (a! = B) is false! (A! = B) the result is 1. Reason! (A! = B) the value is true.
Because the value of the operand (a == b) is true! (A == b) the result is 0. Reason! (A == b) the value of false.
Bitwise operators
During accounting, mathematical operations such as yoga, subtraction, multiplication and division are converted into bit-level, fast processing and energy is stored.
Bitwise operators are used to complete bit-level operations in C programming.
Think, x = 10 in the table below (0000 1010 in the binary) and y = 4 (0000 0100 in the binary)
Operator operator meaning (x = 10 and y = 4) results
& Bitwise AND x & y = 0 (0000 0000) 0
| Bitwise OR x | y = 14 (0000 1110) 14
^ Bitwise exclusive OR x ^ y = 14 (0000 1110) 14
~ Bitwise complement ~ x = -11 (1111 0101) -11
<< Shift left x << 2 = 42 (0010 1000) 42
>> Shift right x >> 2 = 2 (0000 0010) 2
Visit our CWitView Operator section to know more about bitwise operators
Other operators
Comma operator
Comma operator is used to declare related expressions simultaneously. For example:
int a, b = 10, c = 5, d;
sizeof operator
Sizeof operator is a unary operator that returns the size of data such as constant, variables, arrays, structures etc.
Syntax
sizeof (variable)
Example: sizeof operator
#include <stdio.h>
#include <conio.h>
void main ()
{
int a;
float b;
double c;
char d;
printf ("Size of Integer:% d bytes \ n", sizeof (a));
printf ("Size of float:% d bytes \ n", sizeof (b));
printf ("Size of double:% d bytes \ n", sizeof (c));
printf ("Size of character:% d byte \ n", sizeof (d));
getch ();
}
Output
Size of Integer: 2 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of character: 1 byte
C-Operational Operator (?)
When an operator works with three operands (oparend), he is called a ternary operator. Do you see the turnarie operator? Ternary operators are also called conditional operators.
Conditional Operator Syntax

The steps of the conditional operator are described below:
Boolean expressions in the above syntax are Conditions and true part and false part are either values or variables or statements or any mathematical expressions. True condition is edited if the condition is true otherwise false is p

0 Comments