Operators in C Language - Any Notes BCA

Operators in c programming language notes


An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations (calculations). C is extremely rich in built-in operators. Operators are used in program to manipulate data and variables. Some operators require two operands, while others act upon only one operand.

C operators are classified as follows: 

1) Arithmetic Operators 
2) Relational Operators 
3) Logical Operators 
4) Assignment Operator 
5) Increment & Decrement Operator 
6) Conditional Operator 
7) Bit wise Operators 
8) Special Operators

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulo (remainder). They work on numeric data types and are essential for arithmetic calculations.

Operator Meaning Example
+ Addition sum = a + b;
- Subtraction difference = a - b;
* Multiplication product = a * b;
/ Division quotient = a / b;
% Modulo (remainder) remainder = a % b;

Integer division truncates any fractional part. Modulo division produces the remainder of integer division. E.g. sum = a+b; product = a*b; difference = a-b; quotient = a/b; remainder = a%b; In the example, a & b are variables ad are known as operand

Note: The modulo operator (%) cannot be used with float or double data types.

2. Relational Operators

Relational operators compare two values or expressions and return a boolean result (true or false). They are often used in decision-making constructs like if and while statements.

Operator Meaning Example
> Greater than a > b
>= Greater than or equal to a >= b
< Less than a < b
<= Less than or equal to a <= b
== Equal to a == b
!= Not equal to a != b

A simple relational expression contains only one relational operator and takes the following form: ܽ݁2 ݎ݋ݐܽݎ݁݌݋ ݈݊ܽ݋݅ݐ݈݁ܽݎ ܽ݁1 Here ae1 and ae2 are arithmetic expressions, which may be simple constants, variables or combination of them. The value of a relational expression is either one or zero. It is one if the specified relation is true and zero if the relation is false. E.g. 10 < 20 is true, 20 < 10 is false. These expressions are used in decision statements such as if and while to decide the course of action of a running program. 

3. Logical Operators

Logical operators are used to combine multiple conditions or expressions. They return true or false based on the evaluation of the conditions.

Operator Meaning Example
&& Logical AND (a > b) && (x == 10)
|| Logical OR (a > b) || (x == 10)
! Logical NOT !(a > b)

An expression of this kind will combine two or more relational expressions is termed as logical expression or compound relational expression. The logical expression given below is true if a>b is true and x == 10 is true. If either or both of them are false, the expression is false.

4. Assignment Operators

Assignment operators are used to assign values to variables. C also provides shorthand assignment operators for conciseness and efficiency.

Operator Meaning Example
= Assign x = y
+= Add and assign x += y (x = x + y)
-= Subtract and assign x -= y (x = x - y)
*= Multiply and assign x *= y (x = x * y)
/= Divide and assign x /= y (x = x / y)
%= Modulo and assign x %= y (x = x % y)

The use of shorthand assignment operators has 3 advantages: 
  • 1. What appears on the left-hand side need not be repeated and therefore it becomes easier to write. 
  •  2. The statement is more concise and easier to read. 
  •  3. The statement is more efficient. 

5. Increment & Decrement Operators

These operators increment or decrement a variable by 1. They can be used in prefix or postfix forms:

  • Prefix: ++x or --x (operation happens before assignment).
  • Postfix: x++ or x-- (operation happens after assignment).

Example:

m = 5;

y = ++m; // y = 6, m = 6 (prefix increment)

y = m++; // y = 5, m = 6 (postfix increment)

6. Conditional Operator

The conditional operator (?:) is a ternary operator used for concise decision-making.

Syntax:

condition ? expression1 : expression2;

Example:

a = 10;

b = 15;

x = (a > b) ? a : b; // x = 15

7. Bitwise Operators

Bitwise operators allow manipulation of data at the bit level. These are primarily used for low-level programming tasks like working with hardware registers.

Operator Meaning Example
& Bitwise AND x & y
| Bitwise OR x | y
^ Bitwise XOR x ^ y
~ Bitwise NOT ~x
<< Left Shift x << 2
>> Right Shift x >> 2

8. Special Operators

C also supports several special operators such as the comma operator, sizeof, pointer operators (&, *), and member selection operators (. and ->).

Example of the sizeof operator:

int size = sizeof(int); // Returns the size of an integer in bytes

Conclusion

Understanding operators is fundamental to mastering C programming. Operators provide the tools to manipulate data and implement logical control within programs. By learning their usage and application, programmers can write efficient, readable, and robust code.

Practice Questions

  1. Write a program to demonstrate the use of arithmetic operators in C.
  2. Implement a program that uses relational and logical operators to compare three numbers.
  3. Create a program that shows the difference between pre-increment and post-increment operators.
  4. Write a program to demonstrate the use of conditional operators to find the maximum of two numbers.
  5. Implement bitwise operators to toggle the bits of a number and display the result.

Post a Comment

Previous Post Next Post

Contact Form