Operators and Expressions

Operators are symbols used to perform operations on variables and values. In C, operators can be categorized into several types

Arithmetic Operators

Used for arithmetic operations like addition, subtraction, multiplication, division, and more.

				
					int a = 10, b = 5;
int sum = a + b; // Addition
int difference = a - b; // Subtraction
int product = a * b; // Multiplication
float quotient = (float)a / b; // Division (casting one operand to float for accurate division)

				
			

Assignment Operators

Used to assign values to variables.

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

				
			

Comparison Operators

Used to compare values.

				
					int a = 5, b = 10;
if (a < b) {
    printf("a is less than b\n");
}

				
			

Logical Operators

Used to combine conditional statements.

				
					int x = 10, y = 5;
if (x > 5 && y < 10) {
    printf("x is greater than 5 AND y is less than 10\n");
}

				
			

Bitwise Operators

Used for bit-level manipulation

				
					int a = 5, b = 3;
int result = a & b; // Bitwise AND

				
			

Unary Operators

Operate on a single operand.

				
					int x = 5;
x++; // Increment x by 1

				
			

Ternary Operator

Conditional operator.

				
					int a = 10, b = 5;
int max = (a > b) ? a : b; // If a is greater than b, max = a; else max = b;

				
			

Expressions

Expressions are combinations of operators, variables, and constants that produce a single value.

				
					int a = 5, b = 10;
int result = (a * b) + (b - a);

				
			

Advanced Concepts

Operator Precedence

Defines the order of evaluation of operators in an expression.

  1. Parentheses: () – Parentheses are used to group expressions and have the highest precedence. Expressions within parentheses are evaluated first.

  2. Postfix Operators: ++, -- – Increment and decrement operators have higher precedence when used in postfix form.

  3. Prefix Operators: ++, --, +, -, !, ~ – Prefix increment, decrement, unary plus, unary minus, logical NOT, and bitwise NOT operators have higher precedence.

  4. Multiplicative Operators: *, /, % – Multiplication, division, and modulus operators have higher precedence than additive operators.

  5. Additive Operators: +, - – Addition and subtraction operators have lower precedence than multiplicative operators.

  6. Shift Operators: <<, >> – Left and right shift operators have lower precedence than additive operators.

  7. Relational Operators: <, <=, >, >= – Relational operators for less than, less than or equal to, greater than, and greater than or equal to have lower precedence than shift operators.

  8. Equality Operators: ==, != – Equality operators for equal to and not equal to have lower precedence than relational operators.

  9. Bitwise AND Operator: & – Bitwise AND operator has lower precedence than equality operators.

  10. Bitwise XOR Operator: ^ – Bitwise XOR (exclusive OR) operator has lower precedence than bitwise AND operator.

  11. Bitwise OR Operator: | – Bitwise OR operator has lower precedence than bitwise XOR operator.

  12. Logical AND Operator: && – Logical AND operator has lower precedence than bitwise OR operator.

  13. Logical OR Operator: || – Logical OR operator has lower precedence than logical AND operator.

  14. Conditional Operator (Ternary): ? : – Conditional operator has lower precedence than logical OR operator.

  15. Assignment Operators: =, +=, -=, *=, /=, %= etc. – Assignment operators have the lowest precedence among all operators.

				
					int a = 5, b = 10;
int result = (a * b) + (b - a);

				
			

Operator Associativity

Determines the order in which operators of the same precedence are evaluated.

				
					int result = 8 / 4 / 2; // result will be 1, not 4, due to left-to-right associativity

				
			

Typecasting

Converting one data type to another.

				
					int a = 10, b = 3;
float result = (float) a / b; // Typecasting one operand to float for accurate division

				
			

Increment and Decrement Operators

Increment (++) and decrement (--) operators are used to increase or decrease the value of a variable by 1.

				
					int x = 5;
x++; // Increment x by 1 (now x is 6)

				
			

Compound Assignment Operators:

Compound assignment operators combine arithmetic operations with assignment.

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

				
			

Conditional Operator (Ternary Operator)

The ternary operator ? : is a concise way to express conditional statements.

				
					int a = 10, b = 5;
int max = (a > b) ? a : b; // If a is greater than b, max = a; else max = b;

				
			

sizeof Operator:

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

				
					int size_of_int = sizeof(int); // Size of int data type

				
			

Comma Operator

Comma operator , is used to separate expressions. The value of the entire expression is the value of the last expression.

				
					int a = 5, b = 10, c = 15;
int result = (a++, b++, c++); // result will be 15 (value of c)

				
			

Bitwise Operators

Bitwise operators perform operations at the bit-level.

				
					int a = 5, b = 3;
int result_and = a & b; // Bitwise AND
int result_or = a | b; // Bitwise OR

				
			

Understanding operators and expressions in C is fundamental for writing efficient and effective code. Practice with various operators and expressions to become proficient in utilizing them to solve programming problems. Remember to consider operator precedence and associativity to avoid unexpected results in your code.By mastering operators and expressions, you'll have a strong foundation for further learning and development in the C language. Happy coding! ❤️

Table of Contents