Engineer Simple https://www.engineersimple.com/2022/07/c-program-learning-part-3.html

# OPERATORS AND EXPRESSIONS

An operator tells the computer to perform certain mathematical or logical manipulation.
C operators can be classified below:
• Arithmetic Operator (+,-,*,/,%)
• Relational Operator (<, <=, >, >=, ==, !=)
• Logical Operator (&&, ||, !)
• Assignment Operator (=)
• Increment and Decrement Operator (++, --)
• Conditional Operator (? :)
• Bitwise Operator
• Special Operator

ARITHMETIC OPERATOR

#include<stdio.h>
main()
{
int months, days;
printf(“Enter days\n”);
scanf(“%d”,&days);
months = days / 30;
days = days % 30 ;
printf(“Months= %d , Days = %d”, months,days);
}

### RELATIONAL OPERATOR

#include

#define N 100

#define A 2

main()

{

int a;

a=A;

while(a<N)

{

printf(“%d \n”,a);

a= a*a;

}

#### }Increment (++) and Decrement (--) operator

The operator ++ adds 1 to the operand, while -- subtracts 1. Both are unary operators and takes the following form

++m; or m++;

--m; or m--;

++m; is equivalent to m = m + 1 ;

--m; is equivalent to m = m – 1;

*When the postfix ++ (or --) is used with a variable in an expression, the expression is evaluated first using the original value of the variable and then the variable is incremented (or decremented) by one.

*When the prefix ++ (or --) is used with a variable in an expression, the variable is incremented (or decremented) by one and then the expression is evaluated using the new value of the variable.

Conditional Operator

A ternary operator pair “? :” is available in C. The form is

 Exp1 ? Exp2 : Exp3

The operator ? : works as follows: exp1 is evaluated first. If exp1 is true then exp2 will be evaluated and exp2 becomes the value of the expression. If exp1 is false then exp3 will be evaluated and exp3 becomes the value of the expression.

For example, consider following statement:

a= 10;

b= 15;

x= (a > b) ? a : b;

#include<stdio.h>

main()

{

int a, b, c, d;

a = 15; b = 10;

c = ++a – b;

printf(“a = %d b = %d c = %d \n”, a, b, c); d = b++ +a;

printf(“a = %d b = %d d = %d \n”, a, b, d”);

printf(“a/b = %d \n”,a/b); printf(“a%b = %d \n”,a%b);

printf(“a * = b = %d \n”,a* = b);

printf(“%d \n”, (c>d) ? 1 : 0);

printf(“%d \n”, (c>d) ? 1 : 0);

}

Output:

a = 16 b = 10

c = 6 a = 16

b = 11 d = 26

a/b = 1

a%b = 5

a*=b = 176

0

1

PRECEDENCE OF ARITHMETIC OPERATORS

An arithmetic expression without parentheses will be evaluated first from left to right using the rules of precedence of operator. There are two distinct priority levels of arithmetic operators in C:

High Priority * / %

Low Priority + -

OPERATOR PRECEDENCE AND ASSOCIATIVITY

Operator in C has a precedence associated with it. This precedence is used to determine how an expression involved more than one operator is involved. There are distinct level of precedence and one operator belong one of these levels. The operators at the highest level of precedence are evaluated first. The operators of the same precedence are evaluated either from left to right or from right to left, depending on the level. This is known as the associativity property of an operator.

Consider the following conditional statement

if (x == 10 + 15 && y < 10)

The precedence rules say that addition operator has a higher priority than the logical operator (&&) and the relational operator (== and<). Therefore the additional of 10 and 15 is executed first. This is equivalent to :

if (x == 25 && y < 10)