Learn C Programming
Operators and expression
Operators and expressions play a fundamental role in C programming as they enable us to perform a wide range of operations and calculations. We use operators and expressions in C to carry out computations, manipulate values, and make decisions. By using operators such as arithmetic, relational, and logical operators, we can create expressions that combine variables, constants, and function calls to produce meaningful results. Mastering the concepts of operators and expressions is essential for writing efficient and effective C programs.
. By understanding the different types of operators and expressions in C, you will be able to write programs that are more efficient, accurate, and readable.ย
Operators:
Operators in C programming are symbols or characters that perform specific actions or computations on operands. They manipulate values or variables, triggering actions when applied to C variables and other objects.
Operators can be classified as follows:
Unary Operators:
A unary operator is one that operates on a single operand and returns a new value. In other words, it is an operator that uses unary operators to change the value of an operand or expression.
Unary operators are further classified into :- Increment:The unary increment operator in C programming is denoted by the symbol โ++โ. It signifies an increase of one in the value of the operand. It can be used in both post-increment and pre-increment forms.
- Prefix increment: In this method, the operator comes before the operand (for example, ++a). The operandโs value will be changed before it is used.
- postfix increment:In this method, the operator comes after the operand (for example, a++). The operandโs value will be changed once it is utilized.
- Decrement: The unary decrement operator works in the opposite direction as the unary increment operator. The unary decrement operator is represented by the double minus (โ) symbol and is used to lower the operand value by one depending on the kind of decrement. There are two types of unary decrement operators: pre decrement operators and post decrement operators.
- prefix decrement: The operator comes before the operand in this method (e.g., โ -a). The operandโs value will be changed before it is used.
- postfix decrement: The operator follows the operand in this method (e.g., a- -). The operandโs value will be changed once it is utilized.
Binary Operators:
Those operators that require two operands to act upon are called binary operators.
Binary operators are further classified into :Arithmetic operators:
An arithmetic operator applies mathematical operations to numerical numbers, such as addition, subtraction, multiplication, and division (constants and variables).
For instance:
Output// Working of arithmetic operators #include <stdio.h> int main() { int a = 9,b = 4, c; c = a+b; printf("a+b = %d \n",c); c = a-b; printf("a-b = %d \n",c); c = a*b; printf("a*b = %d \n",c); c = a/b; printf("a/b = %d \n",c); c = a%b; printf("Remainder when a divided by b = %d \n",c); return 0; }
a+b = 13 a-b = 5 a*b = 36 a/b = 2 Remainder when a divided by b = 1
Relational Operators:
The relationship between two operands is verified by a relational operator. It yields 1 if the connection is true and 0 if the relation is false. Decision-making and looping both require relational operators.
For instance:
Output// Working of relational operators #include <stdio.h> int main() { int a = 5, b = 5, c = 10; printf("%d == %d is %d \n", a, b, a == b); printf("%d == %d is %d \n", a, c, a == c); printf("%d > %d is %d \n", a, b, a > b); printf("%d > %d is %d \n", a, c, a > c); printf("%d < %d is %d \n", a, b, a < b); printf("%d < %d is %d \n", a, c, a < c); printf("%d != %d is %d \n", a, b, a != b); printf("%d != %d is %d \n", a, c, a != c); printf("%d >= %d is %d \n", a, b, a >= b); printf("%d >= %d is %d \n", a, c, a >= c); printf("%d <= %d is %d \n", a, b, a <= b); printf("%d <= %d is %d \n", a, c, a <= c); return 0; }
5 == 5 is 1 5 == 10 is 0 5 > 5 is 0 5 > 10 is 0 5 < 5 is 0 5 < 10 is 1 5 != 5 is 0 5 != 10 is 1 5 >= 5 is 1 5 >= 10 is 0 5 <= 5 is 1 5 <= 10 is 1
Logical Operators:
Depending on whether the outcome of the expression is true or false, an expression using logical operators yields either 0 or 1. In the decision-making process in C programming, programmers frequently use logical operators.
For instance:
Output// Working of logical operators #include <stdio.h> int main() { int a = 5, b = 5, c = 10, result; result = (a == b) && (c > b); printf("(a == b) && (c > b) is %d \n", result); result = (a == b) && (c < b); printf("(a == b) && (c < b) is %d \n", result); result = (a == b) || (c < b); printf("(a == b) || (c < b) is %d \n", result); result = (a != b) || (c < b); printf("(a != b) || (c < b) is %d \n", result); result = !(a != b); printf("!(a != b) is %d \n", result); result = !(a == b); printf("!(a == b) is %d \n", result); return 0; }
(a == b) && (c > b) is 1 (a == b) && (c < b) is 0 (a == b) || (c < b) is 1 (a != b) || (c < b) is 0 !(a != b) is 1 !(a == b) is 0
Bitwise Operators:
In C programming, bitwise operators operate on individual bits of integer values, allowing you to perform operations at the bit level. They operate on the binary representation of the values.
Here are the bitwise operators available in C:- Bitwise AND (&): The result has a 1 bit if both corresponding bits are 1, otherwise 0.
Example: a & b performs bitwise AND on a and b. - Bitwise OR (|): The result has a 1 bit if at least one of the corresponding bits is 1, otherwise 0.
Example: a | b performs bitwise OR on a and b. - Bitwise XOR (^): The result has a 1 bit if exactly one of the corresponding bits is 1, otherwise 0.
Example: a ^ b performs bitwise XOR on a and b. - Bitwise NOT (~): Flips the bits, i.e., it changes 1 to 0 and 0 to 1.
Example: ~a performs bitwise NOT on a. - Left Shift (<<): Shifts the bits of the left operand to the left by a specified number of positions.
Example: a << n shifts the bits of a n positions to the left. - Right Shift (>>): Shifts the bits of the left operand to the right by a specified number of positions. The behavior of right shift depends on the sign of the left operand.
For unsigned values, it fills with zero bits.
For signed values, it fills with the sign bit, which means the result depends on the implementation (arithmetic or logical right shift).
Example: a >> n shifts the bits of a n positions to the right.
For instance:
Output#include <stdio.h> int main() { int a = 5; // 0101 in binary int b = 3; // 0011 in binary int result; // Bitwise AND result = a & b; // 0001 printf("a & b = %d\n", result); // Bitwise OR result = a | b; // 0111 printf("a | b = %d\n", result); // Bitwise XOR result = a ^ b; // 0110 printf("a ^ b = %d\n", result); // Bitwise NOT result = ~a; // 1010 (assuming 4-bit integers) printf("~a = %d\n", result); // Left Shift result = a << 2; // 10100 printf("a << 2 = %d\n", result); // Right Shift result = a >> 1; // 0010 printf("a >> 1 = %d\n", result); return 0; }
Note: The specific binary representation of integers depends on the size of the integer type, and the examples provided assume 4-bit integers for simplicity.a & b = 1 a | b = 7 a ^ b = 6 ~a = -6 a << 2 = 20 a >> 1 = 2
- Bitwise AND (&): The result has a 1 bit if both corresponding bits are 1, otherwise 0.
Assignment Operators:
Operator Description Example Equivalent to = Simple assignment a = 5; a = 5; += Addition assignment a += 3; a = a + 3; -= Subtraction assignment a -= 2; a = a โ 2; *= Multiplication assignment a *= 4; a = a * 4; /= Division assignment a /= 2; a = a / 2; %= Modulus assignment a %= 3; a = a % 3; <<= Left shift assignment a <<= 2; a = a << 2; >>= Right shift assignment a >>= 1; a = a >> 1; &= Bitwise AND assignment a &= b; a = a & b; |= Bitwise OR assignment a |= b; a = a | b; ^= Bitwise XOR assignment a ^= b; a = a ^ b;
These assignment operators assign a value to a variable while simultaneously performing an operation. For example, a += 3; is equivalent to a = a + 3;, which adds 3 to the current value of a and assigns the result back to a. Itโs worth noting that compound assignment operators (+=, -=, *=, /=, %=, <<=, >>=, &=, \|=, ^=) combine an arithmetic or bitwise operation with assignment, making the code more concise and readable.
For instance:
Output// Working of assignment operators #include <stdio.h> int main() { int a = 5, c; c = a; // c is 5 printf("c = %d\n", c); c += a; // c is 10 printf("c = %d\n", c); c -= a; // c is 5 printf("c = %d\n", c); c *= a; // c is 25 printf("c = %d\n", c); c /= a; // c is 5 printf("c = %d\n", c); c %= a; // c = 0 printf("c = %d\n", c); return 0; }
c = 5 c = 10 c = 5 c = 25 c = 5 c = 0
ย
Expressions:
In C programming, an expression combines operands (variables, constants, or literals) and operators to yield a single value through evaluation. Expressions encompass arithmetic, logical, and relational operations, and they play a crucial role in assignments, conditional statements, loops, function calls, and various program components.By evaluating expressions, we perform calculations and make decisions based on the results obtained.
For instance:
int a = 5, b = 3, c;
float x = 2.0, y = 1.5, z;
c = a + b; // Addition expression
z = x / (y - 1.0); // Complex arithmetic expression
c = (a > b) && (x < y); // Logical expression
c = (a == b) ? x : y; // Ternary (conditional) expression
c = factorial(5); // Function call expression
ย
In the examples above, expressions involve arithmetic operations, logical operations, comparison operations, function calls, and assignment. Each expression assigns its result to a variable (such as c or z), enabling further utilization in the program.
Understanding expressions and their evaluation is crucial for writing effective and correct C programs.
ย