# Begin C Programming with C Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. They are used in programs to manipulate data and variables. The C language has built-in operators and can be classified into a number of categories.

Learn more about the C language at Udemy.com

**Arithmetic Operators**

C provides all the basic arithmetic operators.

- + Addition or unary plus
- – Subtraction or unary minus
- * Multiplication
- / Division
- % Modulo Division

An Example of Arithmetic Operators

void main() { int number1, number2,number3; int sum1,multi,divi,difference; printf(“Enter two numbers \n”); scanf(“%d”,&number1); scanf(“%d”,&number2); //number1=10,number2=5,number3=20 scanf(“%d”,&number3); sum1=number1+number2+number3; difference=number1-number2; multi=number1*number2*number3; divi=number1/number2; printf(“Addition is %d”sum1); printf(“Subtraction is %d”,difference); printf(“Multiplication is %d”,multi); printf(“Division is %d”,div); }

**Output:**

Addition is 35

Subtraction is 5

Multiplication is 1000

Division is 2

**Relational Operator**

These operators are used in making a comparison between two quantities or take certain decisions. For instance, a relational operator compares the age of two people or the price of two items.

a<b or a<30 is called relational expression and the value is either one or zero or true or false, respectively.

For example:

10 < 40 is true. While 20>240 is false.

Different operators are:

- < is less than
- <= is less than or equal to
- >I s greater than
- >= is greater than or equal to
- == is equal to
- != not equal to

An example:

4<=10 True 4<=-10 False 10==10 True

a+b=d+g only if the sum of values of a and b is equal to the sum of g and d.

An arithmetic expression can be used on either side of the relational operator. In this case, an arithmetic expression is evaluated first on either side and then the result is evaluated.

Master C programming with a class at Udemy.com

**Logical Operators**

Logical operators are used to evaluate complex regular expressions and are used in combinations with relational operators to test one or more conditions and make decisions. C has three logical operators:

- &&: It is logical AND operator. If both the operands are non-zero, then condition becomes true
- ||: It is logical OR operator. If any of the two operands is non-zero, then condition becomes true.
- !: It is Logical NOT and is used to reverse the logical state of the operand. If the condition is true the NOT operator will make it false and vice versa.

An Example:

a> b&& x==20

The above expression is a combination of two-relational expressions and is termed as logical expression. A logical expression also yields a value of one or zero. For instance, the above expression is true when both the condition a>b and x==20 is True.

Some Examples of logical expressions:

- If(num<5 || num>10)
- If(num>5 && num2<5)

**Assignment Operators**

These operators are used to assign the result of an expression to a variable. C also has some shorthand assignment operators.

For Example: x=x+y+1 can be written in shorthand form x+=y+1. The shorthand operator += means “add y+1 to x’ or ‘increment x by y+1’.

If y=2 and x=1, then x after execution would become 5.

The advantages of using shorthand are:

- No repetition of left-hand side and thus the readability is good
- The statement is more concise.
- The statement is more efficient

Some Examples:

- a+=1
- a-=1
- a*=n+1 is same as a=a*(n+1)
- a/=n+1 is same as a=a/(n+1)
- a%=b is same as a a= a%b

**Increment Decrement Operators**

C allows very useful operators that aren’t found generally in other languages. These are the increment(++) and decrement(–) operators.

The increment operator adds 1 to the operand, while decrement operator subtracts 1 and both these are unary operators.

These types are further divided into two sub divisions based on their functionality and they are:

- Postfix
- Prefix

In Postfix the value is first assigned to the variable on the left and it then increments the operand.

Example:

n=2 ; y=n++;

The ‘y’ will hold 2 and the postfix first assigns the value and then increments it by one.

In Prefix, the value is incremented and then assigned.

Example:

n=2 y=++n

The ‘y’ will hold 3 now.

Increment and decrement operators are unary operators and they require variables as their operands. The precedence and associativity of ++ and – operators are the same as that of unary + and unary –

**Conditional Operator:**

Ternary operator is a conditional operator available in C to construct conditional expressions of the form exp1? exp2:exp3

Here exp1, exp2,exp3 are the expressions.

The operator?: works as follows :

Exp1 is evaluated first. If true then exp2 is evaluated and becomes the value of the expression.If exp1 is false exp3 is evaluated and it is the value of the expression. Note that only one of the expressions after ‘?’ will be executed.

For Example:

n=9; m=0; x=(n>m) ? n:m;

Ternary operator works just like the If else statement.

**Bitwise Operators:**

C also provides the facility of changing information at bit levels. These operators are used to test the bits or shift them right or left. Bitwise operators cannot be applied to float and double i.e. decimal values.

Some of the operators are:

- &bitwise AND
- | bitwise OR
- ^ bitwise exclusive OR
- << shift left
- >>shift right

** **

**Special Operators**

The C language supports special operators like comma and sizeof operators.

**Comma Operator**

Comma operator is used in linking all related expressions in a way where the comma links the list of the expressions evaluated from left to right and the rightmost expression shows the combined expression’s value.

Example:

In for Loop, for (n=1,m=10,n<=m; n++,m++).

val=(x=11,z=10,x+z);

In this example, it first assigns the value 11 to x and then assigns 10 to z and later assigns 21 to variable val. Parentheses are necessary since Comma has the least precedence as compared to all the other operators.

**The sizeof operator:**

The sizeof operator is compile time operator and it gives the size of the operands in the bytes it occupies. The operand can be a variable or a constant or a data type.

Example:

M=sizeof(int) N=sizeof(long )

The size of operator helps to determine structure size when the programmer does not know the size. It also helps in dynamic memory allocation.

Learn more about the C language with a course at Udemy.com

### Top courses in C#

### C# students also learn

## Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.