Operators in C

Operators in C: A Comprehensive Guide

In C programming, operators are symbols that perform operations on variables and values. They are the building blocks of logic in C, enabling programmers to manipulate data and control the flow of programs. In this blog, we'll explore the different types of operators in C, how they work, and where to use them.



1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus (remainder).


Operator

Symbol

Description

Example

Addition

+

Adds two operands

a + b

Subtraction

-

Subtracts second operand from first

a - b

Multiplication

*

Multiplies two operands

a * b

Division

/

Divides numerator by denominator

a / b

Modulus

%

Returns the remainder of division

a % b



Example: 


int a = 10, b = 3;

printf("%d\n", a + b); // Output: 13

printf("%d\n", a % b); // Output: 1


                                    2. Relational Operators

Relational operators are used to compare two values. They return a boolean value (1 for true, 0 for false).

Operator

Symbol

Description

Example

Equal to

==

Checks if two values are equal

a == b

Not equal to

!=

Checks if two values are not equal

a != b

Greater than

> 

Checks if the left operand is greater than the right

a > b

Less than

< 

Checks if the left operand is less than the right

a < b

Greater than or equal to

>=

Checks if the left operand is greater than or equal to the right

a >= b

Less than or equal to

<=

Checks if the left operand is less than or equal to the right

a <= b

 

Example:

int a = 10, b = 20;

printf("%d\n", a == b);  // Output: 0 (false)

printf("%d\n", a < b);   // Output: 1 (true)


                                    3. Logical Operators

Logical operators are used to combine two or more conditions. They evaluate to 1 or 0 based on whether the condition(s) are true or false.

Operator

Symbol

Description

Example

Logical AND

&&

Returns true if both conditions are true

a && b

Logical OR

`

`

Logical NOT

!

Returns true if the condition is false

!a

 

Example:

int a = 1, b = 0;

printf("%d\n", a && b);  // Output: 0 (false)

printf("%d\n", a || b);  // Output: 1 (true)

printf("%d\n", !a);      // Output: 0 (false)


                                4. Assignment Operators

Assignment operators assign values to variables. The most basic one is the = operator, but there are compound assignment operators that combine an operation with assignment.


Operator

Symbol

Description

Example

Assignment

=

Assigns right-hand value to left-hand variable

a = b

Add and assign

+=

Adds right-hand value to left-hand variable and assigns

a += b

Subtract and assign

-=

Subtracts right-hand value from left-hand variable and assigns

a -= b

Multiply and assign

*=

Multiplies left-hand value by right-hand value and assigns

a *= b

Divide and assign

/=

Divides left-hand value by right-hand value and assigns

a /= b

Modulus and assign

%=

Finds remainder and assigns

a %= b

 

Example:

int a = 10;

a += 5;  // Equivalent to a = a + 5

printf("%d\n", a);  // Output: 15


5. Increment and Decrement Operators

These operators are used to increase or decrease a variable's value by 1.

Operator

Symbol

Description

Example

Increment

++

Increases value by 1 (prefix or postfix)

++a or a++

Decrement

--

Decreases value by 1 (prefix or postfix)

--a or a--

 

Example:

int a = 5;

printf("%d\n", ++a);  // Output: 6 (pre-increment)

printf("%d\n", a--);  // Output: 6 (post-decrement)


6. Bitwise Operators

Bitwise operators operate on binary digits of integers. They are mainly used in low-level programming.


Operator

Symbol

Description

Example

AND

&

Performs bitwise AND

a & b

OR

`

`

Performs bitwise OR

XOR

^

Performs bitwise XOR

a ^ b

NOT

~

Performs bitwise NOT

~a

Left Shift

<< 

Shifts bits to the left

a << 2

Right Shift

>> 

Shifts bits to the right

a >> 2

 

Example:

int a = 5, b = 3;

printf("%d\n", a & b);  // Output: 1 (bitwise AND)

printf("%d\n", a << 1); // Output: 10 (left shift)


7. Conditional (Ternary) Operator

The ternary operator is a shorthand for if-else statements. It takes three operands and is used to evaluate conditions concisely.

Operator

Symbol

Description

Example

Ternary

? :

Returns a value based on a condition

a ? b : c


Example:

int a = 10, b = 20;

int max = (a > b) ? a : b;  // If a is greater than b, return a, otherwise return b

printf("Max: %d\n", max);   // Output: 20


8. Comma Operator

The comma operator allows multiple expressions to be evaluated in a single statement. It returns the result of the last expression.

Example:

int a = 1, b = 2, c;

c = (a = 10, b = 20, a + b);  // First a is assigned 10, then b is assigned 20, then a + b is calculated

printf("%d\n", c);  // Output: 30


9. Sizeof Operator

The sizeof operator is used to find the size, in bytes, of a variable or data type.

Example:

int a = 10;

printf("%lu\n", sizeof(a));  // Output: 4 (size of int in bytes)


10. Type Casting Operators

Type casting operators convert one data type into another. This is done explicitly using the syntax (type).

Example:

int a = 10;

double b = (double) a;  // Cast integer a to double

printf("%f\n", b);      // Output: 10.000000


Conclusion

Understanding the operators in C is essential for writing efficient and concise programs. From arithmetic and logical operations to bit manipulation and type casting, C provides a wide range of operators to meet the diverse needs of programming. Mastering these operators will allow you to handle data effectively and perform complex operations with ease.


Happy coding with C!





Post a Comment

0 Comments