Introduction to operators

C supports a rich set of built-in operators, we have already used several of them like =, &, etc. An operator is a symbol that tells the computer to performs certain mathematical or logical manipulation. operators are used in the program to manipulate data and variables. They usually form mathematical or logical expressions.

C operators can be classified into a number of categories. They include:

  1. Arithmetic operators
  2. Compound assignment
  3. Comma Operator
  4. Logical operators
  5. Assignment operator
  6. Increment and decrement operators
  7. Conditional ternary operator
  8. Explicit type casting operator
  9. sizeof()
  10. Special Operators

Arithmetic operators

C provides all the basic arithmetic operators. They all work the same as they do in other languages. These can operate on any built-in data type allowed in C. They are:-

  1. Addition (+)
  2. Subtraction (-)
  3. Multiplication (*)
  4. Divide (/)
  5. Modulo (%)

C Code Example

#include <stdio.h>


void main()
  {
		int a,b=2,c=3,d;
		a=b+c;    d=b*c;

		printf("value of a and d : %d , %d",a,d);
	}

Compound assignment

Compound assignment operators modify the current value by performing an operation.

  1. + =
  2. – =
  3. * =
  4. / =

C Code Example

#include <stdio.h>


void main()
  {
		 int a,b;
		 a=2;b=3;
		 a+=b //a=a+b
		 printf("value of  a : %d,a);
  }

Comma Operator

The comma operator is used to separate expressions. Where first First expression is evaluated, then the second expression is evaluated

int a,b;

* Compiler first allocates space to First and then the second variable.

Logical operators

C provides three logical operators when we test more than one condition and make decisions.

These are:

  1. && (meaning logical AND)
  2. || (meaning logical OR)
  3. ! (meaning logical NOT)

C Code Example

#include <stdio.h>


void main()
 {
		int a,b;
		a=2;b=3;
		a=b;
		printf("value of  a : %d,a);
 }

Assignment operator (=)

The assignment operator assigns the right side variable value to the left variable.

a=7;

This statement assigns the integer value 7 to the variable a. The assignment operation always takes place from right to left

This statement assigns to variable the value contained in variable b. The value of an at the moment this statement is executed is lost and replaced by the value of b.

Increment and decrement operators (++, –)

C allows two very useful operators not generally found in other languages. These are the increment and decrement operators.

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

 ++i or i++		 and			 --i or i--

We use the increment and decrement statement in the loop extensively.

While ++i and i++ mean the same thing they are form statements independently, they behave differently;y when they used in the expression on the right-hand side of an assignment statement. Consider the following:-

i=1;

j=++i;

In this case, the value of j and I would be 2.Suppose if we rewrite the above statement as:-

i=1;

j=i++;

then, the value of j would be 1 and i would be 2. A prefix operator first adds 1 to the operand and then the result assign to the variable on left. on the other hand, a postfix operator first assigns and then increments the operands.

Conditional ternary operator

A ternary operator pair ” ? :” is available in C to construct conditional expression form:-

condition ? result_1 : result_2

If the condition is true, the entire expression evaluates to result_1, and otherwise to result_2.

C Code Example

#include <stdio.h>
#define C 99

void main()
  {
    int a=2,b=3;

 a<b ? printf("a is less") : printf("a is greater");
  }

Explicit type casting operator

Typecasting operators allow converting a value of a given type to another type

(new_datatype)variable_name;

 

C Code Example

#include <stdio.h>


void main()
 {
	 int a;

	 scanf("%f", &a);
	 printf("value of a = %f",(float)a);
 }

After that expression executed it start work as a float type

sizeof() operator

TheĀ Sizeof is a compile-time operator and when used with an operand, it returns the number of bytes the operation occupies. The operation may be a variable, constants, or a data type qualifier. For example:-

sizeof(int)

It returns the size of int

Special Operators

  1. & Returns the address of a memory location.
  2. * Pointer to a variable.

We learn about that operator later