Operators
1. Operators
Operators in C are symbols that perform operations on variables and values. They are classified into several categories:
1.1. Arithmetic Operators
Used for basic arithmetic operations:
Addition:
+int a = 5 + 3; // a = 8Subtraction:
-int a = 5 - 3; // a = 2Multiplication:
*int a = 5 * 3; // a = 15Division:
/int a = 6 / 3; // a = 2Modulus (remainder):
%int a = 5 % 3; // a = 2
1.2. Relational Operators
Used to compare values:
Equal to:
==if (a == b) // Checks if a is equal to bNot equal to:
!=if (a != b) // Checks if a is not equal to bGreater than:
>if (a > b) // Checks if a is greater than bLess than:
<if (a < b) // Checks if a is less than bGreater than or equal to:
>=if (a >= b) // Checks if a is greater than or equal to bLess than or equal to:
<=if (a <= b) // Checks if a is less than or equal to b
1.3. Logical Operators
Used to perform logical operations:
Logical AND:
&&if (a > 0 && b > 0) // True if both conditions are trueLogical OR:
||if (a > 0 || b > 0) // True if at least one condition is trueLogical NOT:
!if (!(a > 0)) // True if a is not greater than 0
1.4. Bitwise Operators
Operate on bits and perform bit-level operations:
Bitwise AND:
&int a = 5 & 3; // Result is 1 (0101 & 0011 = 0001)Bitwise OR:
|int a = 5 | 3; // Result is 7 (0101 | 0011 = 0111)Bitwise XOR:
^int a = 5 ^ 3; // Result is 6 (0101 ^ 0011 = 0110)Bitwise NOT:
~int a = ~5; // Result is -6 (complement of 0101 is 1010)Left Shift:
<<int a = 5 << 1; // Result is 10 (0101 << 1 = 1010)Right Shift:
>>int a = 5 >> 1; // Result is 2 (0101 >> 1 = 0010)
1.5. Assignment Operators
Used to assign values to variables:
Simple Assignment:
=int a = 5;Add and Assign:
+=a += 3; // Equivalent to a = a + 3Subtract and Assign:
-=a -= 2; // Equivalent to a = a - 2Multiply and Assign:
*=a *= 4; // Equivalent to a = a * 4Divide and Assign:
/=a /= 2; // Equivalent to a = a / 2Modulus and Assign:
%=a %= 3; // Equivalent to a = a % 3
1.6. Increment and Decrement Operators
Used to increase or decrease the value of a variable:
Increment:
++int a = 5; a++; // Equivalent to a = a + 1Decrement:
--int a = 5; a--; // Equivalent to a = a - 1
1.7. Conditional (Ternary) Operator
A shorthand for if-else statements:
int a = (b > 0) ? 1 : -1; // If b > 0, a = 1, else a = -11.8. Sizeof Operator
Used to get the size of a variable or type in bytes:
int size = sizeof(int); // Size of int in bytes2. Instructions
Instructions in C are the basic commands that the compiler translates into machine code. Here are a few key instructions:
2.1. Assignment
int x = 10; // Assigns 10 to x2.2. Control Flow Instructions
if Statement
if (condition) { // Code to execute if condition is true }else Statement
if (condition) { // Code to execute if condition is true } else { // Code to execute if condition is false }switch Statement
switch (expression) { case value1: // Code to execute for value1 break; case value2: // Code to execute for value2 break; default: // Code to execute if no case matches }while Loop
while (condition) { // Code to execute while condition is true }for Loop
for (initialization; condition; increment) { // Code to execute while condition is true }do-while Loop
do { // Code to execute } while (condition); // Condition checked after execution
2.3. Function Calls
To call a function:
functionName(arguments); // Calls a function with provided arguments2.4. Return Statement
Returns a value from a function and exits the function:
return value; // Returns value to the caller2.5. Break and Continue
break: Exits the current loop or switch statement
break;continue: Skips the rest of the code in the current loop iteration and starts the next iteration
continue;
Summary
Operators in C include arithmetic, relational, logical, bitwise, assignment, increment/decrement, conditional, and sizeof operators.
Instructions include assignment, control flow statements (
if,else,switch), loops (while,for,do-while), function calls,return, and loop control (break,continue)(We'll study more about them in Control Flow).
Last updated
Was this helpful?