C- Programming : Operators and Expressions

Redmen Ishab
4 min readApr 17, 2022

Operators:

An operator is a symbol that operates on a certain data type or data item.The data items on which operator is used is called operands. For eg: 8+9 ; Here ‘+’ is the operator and 8 and 9 are the operands.

  1. Unary Operator: Operator that requires only one operand. eg: ++ , — , += and so on.
  2. Binary Operator: Operator that requires two operands. eg: + ,- , /, * , > and so on.
  3. Ternary Operator: Operator that requires three operands. eg: “?:”.

Arithmetic operators: Operators that is used for arithmetical calculations.

  • + : Adds two operands.
  • - : Subtracts second operands from the first.
  • * : Multiplies both operands
  • / : Divides numerator by denominator
  • % : Modulo finds remainder dividing numerator by denominator
  • ++ : Increment operator. Increases value by 1.
  • — : Decrement operator. Decreases value by 1.

Relational Operators: Operator used to comparison between two operands.

  • == : Checks the value are equal . a==2
  • != : Checks for value are not equal. a!=2
  • > : Checks if the value is greater than.
  • < : Checks if the value is less than.
  • ≥ : Checks if the value is greater than or equal to.
  • ≤ : Checks if the value is less than or equal to.

Logical Operators:Operator used to compare or evaluate logical and relational expression.

  • && (Logical AND) : Checks if both operands are true . A && B -> true/false
  • || (Logical OR) : Checks if any one of the value is true.
  • ! (Logical Not) : Negates or reverse the logical value.

Assignment Operators: Operator used to assign the result of an expression to a variable.

  • = : eg. a=1.
  • += : eg. a+=1; equivalent to a=a+1.
  • -= : eg. a-=1; equivalent to a=a-1.
  • *= : eg. a*=2; equivalent to a=a*2.
  • /= : eg. a/=2; equivalent to a=a/2.
  • %= : eg. a%=2; equivalent to a=a%2.

Increment And Decrement Operator: Operator used to increase or decrease the value of operand by 1. It is an unary operator.
In prefix the value of a is updated before read by the compiler.
In postfix the value of a is read by compiler first and updated.

  • ++: eg: a=1;a++ ; equivalent to a =a+1;
  • — : eg: a=1;a — ; equivalent to a=a-1;

Conditional Operators: “ ? : ” is called conditional operator. It is a ternary operator.

Syntax:
conditional_statement ? expression1 : expression2 ;

eg : a>b ? printf(“a is greater”): printf(“b is greater”).
a%2 == 0 ? printf(“a is even”) : printf(“a is odd”).

Bitwise Operators: Operator used to manipulate data at bit level.

  • & ( Bitwise AND) : Performs logical AND between two bit operands.
  • | ( Bitwise OR): Performs logical OR between two bit operands.
  • ^ ( Bitwise exclusive Xor ) : The result of exclusive OR operation is 1 only if exactly one of the operand is 1.
  • ~ ( One’s compliment ) : The unary operator which inverts the bits represented by its operand.
  • << ( Binary Left Shift Operator ) : Operand shifts to the left by some bit positions.
  • >> ( Binary Right Shift Operator ): Operand shifts to the right by some bit positions.

Special Operator: some of the operator with special usage called special operators are comma operator (,) , sizeof operator( sizeof), pointer (&) operator and member selector operator or spread operator (. , ->) .

Type Conversion :

The conversion of data types from one to another type is called type conversion. Eg: Conversion of int to float datatype is a type conversion. For further detail let’s look into its’ types:

1. Explicit type conversion:

The process of converting a datatype explicity by user themselves is called explicit type conversion.

Syntax :

type_name expression
Eg1 :
int a;
a = (int) 9.3
Now a= 9;
Eg2:
quotent = (float)4/3;
quotent2 = 4/3;
Here: quotent = 1.333333; quotent2 = 1;

2. Implicit type conversion :

The automatic conversion by complier such that all the data types are same while executing the calculations is called implicit type conversion. The result of implicit type conversion are as follows:

  1. float to int causes truncation of fractional part.
  2. double to float causes rounding of digits.
  3. long int to int causes dropping the excess of higher order bits.
Eg
short int s;
int i;
float f;
double d;
long int l;
s = i + l/ d * ( f - i );
Here:
i converted to float.
(f-i) is converted to double.
l is converted to double.
i is converted to double.
right expression is converted to short int from double.

Operator Precedence and Associativity:

operator precedence and associativity

Now perform some exercise and write the o/p:

Problem 1:

void main () {
int a = 5, b=2, c=3, d=4;
float e=1.2, f=2.5;
a = (a/b*c)%d + (a+b-c);
b+=a*b;
b++;
e=f/e;
e--;
printf("a = %d , b= %d, e= %d ", a, b, e );

Problem 2:

void main () {
int i = 5;
i = i + (++i) - (i++ )+ (--i) + i + (i--) + i;
printf ("i = %d", i ) ;
}

--

--

Redmen Ishab

Software Engineer. Failed startup “Software Factory”. Working experience as CTO, SSE, Full Stack Dev, Mobile App Engineer, and Lecturer.