Programmer Coding

Operators in C Language?

What is Operators in C?

Operators in C are symbols that operate on operators. They are used to manipulate data and perform many functions in C functions.

There Are Many Types Of Operators in C

  • Arithmetic Operators:

Arithmetic operators are used to perform mathematical operations consisting of addition, subtraction, multiplication, division, and modulus.

Examples

#include <stdio.h>

int main() {

int a = 10, b = 5;

int sum = a + b; // 15

int difference = a – b; // 5

int product = a * b; // 50

int quotient = a / b; // 2

int remainder = a % b; // 0

printf(“Sum: %d\n”, sum);

printf(“Difference: %d\n”, difference);

printf(“Product: %d\n”, product);

printf(“Quotient: %d\n”, quotient);

printf(“Remainder: %d\n”, remainder);

return 0;

}

Output

Sum: 15

Difference: 5

Product: 50

Quotient: 2

Remainder: 0

  • Relational Operators:

Relational operators are used to compare the relationship between two operands. They return either true (1) or false (0).

Examples (1)

== (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to)

Example (2)

#include <stdio.h>

int main() {

int x = 10, y = 5;

if (x > y) {

printf(“x is greater than y\n”); // x is greater than y

}

if (x == y) {

printf(“x is equal to y\n”);

}

return 0;

}

Output

x is greater than y

  • Logical Operators:

Logical operators are used to perform logical operations on boolean operands. They are typically used in conditional statements and loops.

Examples (1)

&& (logical AND), || (logical OR), ! (logical NOT)

Example (2)

#include <stdio.h>

int main() {

int p = 1, q = 0;

if (p && q) {

printf(“Both p and q are true\n”);

}

if (p || q) {

printf(“At least one of p or q is true\n”); // At least one of p or q is true

}

return 0;

}

Output

At least one of p or q is true

  • Assignment Operators:

The Assignment operator is used to assign values ​​to variables. They work while providing benefits.

Examples (1)

= (simple assignment), += (addition assignment), -= (subtraction assignment), *= (multiplication assignment), /= (division assignment), %= (modulus assignment)

Example (2)

#include <stdio.h>

int main() {

int x = 10;

x += 5; // Equivalent to x = x + 5

printf(“x: %d\n”, x); // x: 15

return 0;

}

Output

x: 15

  • Increment and Decrement Operators:

The increment (++) and decrement (–) operators increase or decrease the value of the variable by 1.

Examples (1)

++ (increment), — (decrement)

Example (2)

#include <stdio.h>

int main() {

int count = 0;

count++;

printf(“Count: %d\n”, count); // Count: 1

return 0;

}

Output

Count: 1

  • Bitwise Operators:

Bitwise operators perform operations on individual bits of operands.

Examples (1)

& (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), >> (right shift)

Example (2)

#include <stdio.h>

int main() {

int a = 5, b = 3;

int result_and = a & b; // Bitwise AND

int result_or = a | b; // Bitwise OR

int result_xor = a ^ b; // Bitwise XOR

printf(“Bitwise AND Result: %d\n”, result_and); // Bitwise AND Result: 1

printf(“Bitwise OR Result: %d\n”, result_or); // Bitwise OR Result: 7

printf(“Bitwise XOR Result: %d\n”, result_xor); // Bitwise XOR Result: 6

return 0;

}

Output

Bitwise AND Result: 1

Bitwise OR Result: 7

Bitwise XOR Result: 6

  • Conditional Operator (Ternary Operator):

The conditional operator (?:) is used to evaluate a condition and return one of two values ​​depending on whether the condition is true or false.

Examples (1)

(condition) ? value1 : value2

Example (2)

#include <stdio.h>

int main() {

int x = 10, y = 5;

int max = (x > y) ? x : y; // max will be assigned the value of x if x > y, otherwise y

printf(“Maximum: %d\n”, max); // Maximum: 10

return 0;

}

Output

Maximum: 10

  • sizeof Operator:

The sizeof operator returns the size of a data type or variable in bytes.

Examples (1)

sizeof(int)

Example (2)

#include <stdio.h>

int main() {

int size = sizeof(int);

printf(“Size of int: %d\n”, size); // Size of int: 4 (assuming int is 4 bytes)

return 0;

}

Output

Size of int: 4

  • Comma Operator:

The comma operator (,) is used to split expressions and compare them from left to right.

Examples (1)

expression1, expression2

Example (2)

#include <stdio.h>

int main() {

int a = 1, b = 2, c = 3;

int value = (a++, b++, c++); // value will be assigned the value of c (3)

printf(“Value after comma operator: %d\n”, value); // Value after comma operator: 3

return 0;

}

Output

Value after comma operator: 3

Precedence of operators in C

Leave a Comment

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

Scroll to Top