C Programming Tutorial
Computer Tutorial

Operators in C



Operators

C is very rich in built-in operators. In fact, it places more significance on operators than do most other computer languages. There are four main classes of operators: arithmetic, relational , logical, and bitwise. In addition, there are some special operators, such as the assignment operator, for particular tasks.

Operators are the following types :

The Assignment Operator

C uses a single equal sign to indicate assignment (:= ). The target, or left part, of the assignment must be an object, such as a variable, that can receive a value.
The general form of the assignment operator is :
variable_name = expression
Ex- x = x+10;( Compound Assignments)
x = y = z = 0;( Multiple Assignments)

Arithmetic Operators

Arithmetic operator are those operator which use to compute arithmetical or mathematic operation like addition, subtraction, multiplication and division.

Operator Action
- Subtraction, also unary minus
+ Addition
* Multiplication
/ Division
% Modulus
- - Decrement
+ + Increment

The precedence of the arithmetic operators :

++, – – (Highest)
–(unary minus)
*, /, %
+, – (Lowest)
» Operators on the same level of precedence are evaluated by the compiler from left to right.

The Increment and Decrement Operators

The Increment operator (++) adds 1 to its operand and the Decrement operator (– –) subtracts 1 from its operand.
x = x + 1 is the same as ++ x;
x = x – 1; is the same as x ––;

Relational Operators

Relational Operators refers to the relationships that values can have with one another i.e. to compare the values of two variables.

Relational Operator Action
> Greater than
> = Greater than or equal
< Less than
< = Less than or equal
= = Equal
!= Not equal

Void data type

The void type specifies an empty set of values. The type void either explicitly declares a function as returning no value or creates generic pointers.

Modifying the Basic Types

Except type void, the basic data types may have various modifiers preceding them. A type modifier alters the meaning of the base type to more precisely fit a specific need.

Type Typical Size in Bits Minimal Range
char 8 -127 to 127
unsigned char 8 0 to 255
int 16 or 32 –32,767 to 32,767
unsigned int 16 or 32 0 to 65,535
short int 16 –32 767 to 32,767
long int 32 –2,147,483,647 to 2,147,483,647
float 32 1E–37 to 1E+37 with six digits of precision
double 64 1E–37 to 1E+37 with ten digits of precision
long double 80 1E–37 to 1E+37 with ten digits of precision

Logical Operators

Logical operators are used to perform logical operations on the given conditions or expressions. There are three logic operator – AND, OR, NOT.
AND ( && ) Operator :It returns true when both conditions are true.
OR ( || ) Operator :It returns true when at-least one of the condition is true.
NOT ( ! ) Operator :It reverses the state of the operand.

Bitwise Operators

Bitwise operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits. It may refers to testing, setting, or shifting the actual bits in a byte or word, which correspond to the standard char and int data types and variants.

Operator Action
& AND
| OR
^ Exclusive OR (XOR)
~ One's complement (NOT)
>> Shift right
<< Shift left