What is Operators in C Language?

The C language supports a wide set of built-in operators. An operator is a symbol that tells the compiler to perform a particular mathematical or logical operation. Operators are used programmatically to manipulate data and variables. Operators are symbols that help to perform operations of a mathematical and logical nature.

Types of Operators:

Programmer's Academy

1. Arithmetic Operators:

These operators are arithmetic or mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/), remainder of division (%), increment (++), decrement (-). ..

Types of Arithmetic Operators:

  • Unary Operators: This type of operator works with a single value (operand) such as ++ or -.
  • Binary Operators: this type of operator works with two operands, such as +, -, *, /.

This is a picture of the number of C arithmetic operators and the functions they perform.

Programmer's Academy
For example:
Programmer's Academy
Here code:
#include <stdio.h>
int main()
{
	int a = 12, b = 6, c;
	c = a + b;
	printf("a+b = %d \n", c);
	c = a - b;
	printf("a-b = %d \n", c);
	c = a *b;
	printf("a*b = %d \n", c);
	c = a / b;
	printf("a/b = %d \n", c);
	c = a % b;
	printf("Remainder when a divided by b = %d \n", c);
	return 0;
}

2. Relational Operators:

Relational operators are used when comparing the values ​​of two operands. Use the> = operator to ensure that one operand is greater than or equal to the other.

The following image is a list of C relational operators and their functions.

Programmer's Academy
Programmer's Academy
For example:
Programmer's Academy
Here Code:
#include <stdio.h>
int main()
{
int a = 7, b = 7, c = 10;
printf("%d == %d = %d \n", a, b, a == b); // true
printf("%d == %d = %d \n", a, c, a == c); // false
printf("%d > %d = %d \n", a, b, a > b); //false
printf("%d > %d = %d \n", a, c, a > c); //false
printf("%d < %d = %d \n", a, b, a < b); //false
printf("%d < %d = %d \n", a, c, a < c); //true
printf("%d != %d = %d \n", a, b, a != b); //false
printf("%d != %d = %d \n", a, c, a != c); //true
printf("%d >= %d = %d \n", a, b, a >= b); //true
printf("%d >= %d = %d \n", a, c, a >= c); //false
printf("%d <= %d = %d \n", a, b, a <= b); //true
printf("%d <= %d = %d \n", a, c, a <= c); //true
return 0;
}

3. Logical operators:

Logical operators are used for true or false results.

The following image shows the logical operators used in C.

Programmer's Academy
For example:
Programmer's Academy
Here Code:
#include <stdio.h>
int main()
{
int a = 8, b = 8, c = 12, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) equals to %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) equals to %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) equals to %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) equals to %d \n", result);
result = !(a != b);
printf("!(a == b) equals to %d \n", result);
result = !(a == b);
printf("!(a == b) equals to %d \n", result);
return 0;
}

4. Bitwise Operators:

These operators are used for bit-level operations on operands. The operator is first converted to the bit level and then the calculation is performed.

Programmer's Academy
For example:
Programmer's Academy
Here Code:
#include <stdio.h>
int main()
{
	int a = 10, b = 8;
	printf("Output = %d", a&b);
	return 0;
}

5. Assignment Operators:

These types of operators are used to assign values ​​to variables.

Programmer's Academy

6. Conditional Operators:

Also, the ternary operator or? :operator. These are used for decision making.

Syntax: Expression 1? Expression 2: Expression 3

7. Special Operators:

Some special operators used in C are:

Programmer's Academy
For example:
Programmer's Academy
Here Code:
#include <stdio.h>
int main()
{
	int *ptr, q;
	q = 40;
/* address of q is assigned to ptr */
	ptr = &q;
/* display q's value using ptr variable */
	printf("%d", *ptr);
	return 0;
}

Conclusion:

Operators are the basic foundation of the C / C ++ programming language. You can now use other conditions to perform any mathematical, logical, or relational operation.

References:

Leave a Reply

Your email address will not be published. Required fields are marked *

Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.