Operator in c programming | AsgarTech
here are some of the operators in C programming with examples:
Arithmetic operators: These operators are used to perform arithmetic operations on operands. For example, the + operator is used to add two operands, the - operator is used to subtract two operands, the * operator is used to multiply two operands, and the / operator is used to divide two operands.
C
int a = 10;
int b = 20;
int c = a + b; // c = 30
int d = a - b; // d = -10
int e = a * b; // e = 200
int f = a / b; // f = 0.5
Relational operators: These operators are used to compare two operands and return a Boolean value. For example, the == operator is used to check if two operands are equal, the != operator is used to check if two operands are not equal, the > operator is used to check if the first operand is greater than the second operand, and the < operator is used to check if the first operand is less than the second operand.
C
int a = 10;
int b = 20;
int c = a == b; // c = false
int d = a != b; // d = true
int e = a > b; // e = false
int f = a < b; // f = true
Logical operators: These operators are used to combine two Boolean expressions. For example, the && operator is used to check if both Boolean expressions are true, the || operator is used to check if either Boolean expression is true, and the ! operator is used to negate a Boolean expression.
C
int a = 10;
int b = 20;
int c = a == 10 && b == 20; // c = true
int d = a == 10 || b == 20; // d = true
int e = !a == 10; // e = false
Assignment operators: These operators are used to assign a value to a variable. For example, the = operator is used to assign a value to a variable, the += operator is used to add a value to a variable, and the -= operator is used to subtract a value from a variable.
C
int a = 10;
a = 20; // a is now 20
a += 10; // a is now 30
a -= 10; // a is now 20
Bitwise operators: These operators are used to perform bitwise operations on operands. For example, the & operator is used to perform a bitwise AND operation, the | operator is used to perform a bitwise OR operation, and the ^ operator is used to perform a bitwise XOR operation.
C
int a = 10;
int b = 20;
int c = a & b; // c = 0
int d = a | b; // c = 30
int e = a ^ b; // c = 10
Miscellaneous operators: These operators are used for miscellaneous purposes. For example, the sizeof operator is used to get the size of a variable, the ?: operator is used to perform a conditional assignment, and the , operator is used to separate multiple expressions.
C
int a = 10;
int size = sizeof(a); // size = 4
int b = 20;
int c = a > b ? a : b; // c = 20
int d = a, e = b; // d = 10, e = 20
No comments: