Operator in c programming
Operator in C programming
In C programming, an operator is a symbol that tells the compiler to perform specific mathematical, relational, or logical operations on one or more operands (variables or values). Operators are essential for performing operations and making decisions within a program.
Types of Operators in C
There are several types of operators in C, each serving a different purpose. Some of the most commonly used types are:
1. Arithmetic Operators
These operators are used to perform basic arithmetic operations like addition, subtraction, multiplication, division, and modulus.
// Example:
int a = 10, b = 5;
int sum = a + b; // Addition
int diff = a - b; // Subtraction
int product = a * b; // Multiplication
int quotient = a / b; // Division
int remainder = a % b; // Modulus (remainder)
2. Relational Operators
Relational operators are used to compare two values or variables. They return a boolean value (true or false) based on the comparison.
// Example:
int a = 10, b = 5;
int result;
result = (a > b); // Greater than
result = (a < b); // Less than
result = (a == b); // Equal to
result = (a != b); // Not equal to
result = (a >= b); // Greater than or equal to
result = (a <= b); // Less than or equal to
3. Logical Operators
Logical operators are used to perform logical operations, typically on boolean values. They are often used in conditional statements.
// Example:
int a = 1, b = 0;
int result;
result = (a && b); // Logical AND
result = (a || b); // Logical OR
result = (!a); // Logical NOT
4. Assignment Operators
Assignment operators are used to assign values to variables.
// Example:
int a = 10; // Assigns 10 to a
a += 5; // Adds 5 to a (a = a + 5)
a -= 3; // Subtracts 3 from a (a = a - 3)
a *= 2; // Multiplies a by 2 (a = a * 2)
a /= 4; // Divides a by 4 (a = a / 4)
5. Increment and Decrement Operators
These operators are used to increase or decrease the value of a variable by 1.
// Example:
int a = 10;
a++; // Increment: a becomes 11
a--; // Decrement: a becomes 9
Comments
Post a Comment