Operators in C Language
The C language is one of the most popular and widely used programming languages. It is used by many software developers to create programs and applications. It is a powerful language and has many features that make it a great choice for coding. One of these features is the use of operators.
Operators are symbols that allow the programmer to manipulate variables and data. They are used to perform operations such as addition, subtraction, multiplication, division, and many more. Operators are very important in the C language and are used in almost every program.
The C language provides a wide variety of operators, each with its own specific purpose. Arithmetic operators are used to perform various arithmetic operations such as addition, subtraction, multiplication, and division. There are also logical operators, which are used to compare two values and determine if they are equal, greater than, or less than each other. Other operators include bit-wise operators, which are used to manipulate the bits of a number, and assignment operators, which are used to assign values to variables.
The C language also supports a wide range of operator precedence. This means that the order in which operators are evaluated is determined by the precedence of each operator. For example, the addition operator (+) has a higher precedence than the subtraction operator (-). As a result, the addition operator will be evaluated before the subtraction operator.
Operators are an important part of the C language and are used in almost every program. Understanding how to use operators correctly will help you create more efficient and effective programs.
1. Arithmetic Operators
All basic mathematical operations are done using these operators. They are as follows:
Operator | Meaning of Operator |
---|---|
+ | Addition or unary plus |
- | Subtraction or unary minus |
* | Multiplication |
/ | Division |
% | Remainder after division (modulo division) |
Example Program:
// Example program for arithmetic operations
#include <stdio.h>
int main() {
int a = 9, b = 4, c;
c = a + b;
printf("The sum is = %d \n", c);
printf("The subtraction is = %d \n", a - b);
printf("The multiplication = %d \n", a * b);
c = a / b;
printf("The division is = %d \n", c);
c = a % b;
printf("Remainder when a divided by b = %d\n", c);
return 0;
}
Assignment operators must require two arguments. The left side must be a variable, but the right-side argument can be a variable, an expression, or a constant.
Examples of invalid and valid assignments:
- 10 = 10; // It gives L value required error.
- = 10; // It gives L value required error.
- A = ; // It gives error
- A = 2 + 3; // It is allowed in programming
- A = 2 * 3 + 4; // It is allowed in programming
- 2 + 3 = A; // It gives error
- 10 = 2.5; // It gives error
Interview Questions on the Assignment Operator
- int a = b = c = 10; is this a valid statement?
Ans: It is not a valid statement because here memory allocation is left to right, whereas the order of precedence for assignment is right to left. There is no memory allocated for b and c. Without memory allocation, we are trying to assign values to c.
int a, b, c; a = b = c = 10; // It is valid in C programming
int a = 10, c, d = b = c = a; // It is valid in C programming
int (a, b) = (10, 20); // It is not valid. Multiple values cannot be assigned at a time.
NOTE: Whenever we have the same operators or multiple operators in a single statement at a time, we need to follow the order of precedence for those operators.
Operator Precedence:
- (+,-) Addition and Subtraction Operators
- (+) and (-) have higher precedence than the assignment operator (=).
- (+) and (-) have the same precedence.
- If they have the same precedence, we have to follow the associativity (left-to-right).
- (*) Multiplication Operator
- It has higher priority than (+,-).
- Associativity (left-to-right).
- (/) Division Operator
- Division operator always gives the quotient value.
- The result depends on the operand types: int / int = int, float / int = float, etc.
- Example: A = 16 / (4 + 4); // ?
- (%) Modulus Operator
- It always gives the remainder value.
- Modulus does not apply to float values in C (Java and Python allow it).
- Example: a = 5 * 4 - 20 / 3 * 6 - 10 / 4 * 2 - 30 + 5 / 2 // Let me know the answer in the comments below.
How to Add Two Numbers Without Using the (+) Operator?
Ans: By using the unary minus (-) the associativity is left-to-right, 5 - (-6) = 11
int a = 10, b = 9, c = 8, d = 7, e = 4, f;
f = a += b -= c *= d /= e %= 4;
2. Relational Operators
The result of relational operators is '0' or '1'. Zero means False and one means True.
Operator | Meaning of Operator | Example |
---|---|---|
== | Equal to | 5 == 3 result is 0 |
> | Greater than | 5 > 3 result is 1 |
< | Less than | 5 < 3 result is 0 |
>= | Greater than or equal to | 5 >= 3 result is 1 |
<= | Less than or equal to | 5 <= 3 result is 0 |
!= | Not equal to | 5 != 3 result is 1 |
Example Program:
// Example program for relational operators
#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("a == b = %d\n", a == b);
printf("a != b = %d\n", a != b);
printf("a > b = %d\n", a > b);
printf("a < b = %d\n", a < b);
printf("a >= b = %d\n", a >= b);
printf("a <= b = %d\n", a >= b);
return 0;
}
3. Logical Operators
Logical operators are used to evaluate logical expressions. They are:
Operator | Meaning of Operator | Example |
---|---|---|
&& | Logical AND | 0 && 1 = 0 |
|| | Logical OR | 0 || 1 = 1 |
! | Logical NOT | !1 = 0 |
Example Program:
// Example program for logical operators
#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("a > b &qmp;& a < 10 = %d\n", (a > b) && (a < 10));
printf("a > b || a > 10 = %d\n", (a > b) || (a > 10));
printf("!(a < b) = %d\n", !(a < b));
return 0;
}
4. Increment and Decrement Operators
Increment and Decrement Operators:
Operator | Meaning |
---|---|
++ | Increment |
-- | Decrement |
The increment operator (++) adds one to a variable, while the decrement operator (--) subtracts one from a variable. These operators can be used in both pre-increment/decrement and post-increment/decrement forms.
Example Program:
// Example program for increment and decrement operators
#include <stdio.h>
int main() {
int a = 5;
printf("a++ = %d\n", a++);
printf("++a = %d\n", ++a);
printf("a-- = %d\n", a--);
printf("--a = %d\n", --a);
return 0;
}
Thank you for reading! Your thoughts and suggestions are always welcome—let’s connect in the comments below!
0 Comments
Got questions? Feel free to ask!