Basic Operators
Operators in C are symbols that perform operations on variables and values. Here is an overview of basic operators in C:
1. Arithmetic Operators
Arithmetic operators perform mathematical operations on numerical values.
Addition (
+
): Adds two operands.int result = 5 + 3; // Result is 8Subtraction (
-
): Subtracts the right operand from the left operand.int result = 10 - 4; // Result is 6Multiplication (
*
): Multiplies two operands.int result = 3 * 7; // Result is 21Division (
/
): Divides the left operand by the right operand.float result = 15.0 / 2.0; // Result is 7.5Modulus (
%
): Returns the remainder when the left operand is divided by the right operand.int result = 10 % 3; // Result is 1
2. Assignment Operator
The assignment operator (=
) is used to assign a value to a variable.
3. Comparison Operators
Comparison operators compare two values and return a Boolean result (true
or false
).
Equal to (
==
): Checks if two values are equal.int a = 5, b = 5; if (a == b) { // Code executed if 'a' is equal to 'b' }Not equal to (
!=
): Checks if two values are not equal.int x = 10, y = 20; if (x != y) { // Code executed if 'x' is not equal to 'y' }Greater than (
>
): Checks if the left operand is greater than the right operand.Less than (
<
): Checks if the left operand is less than the right operand.Greater than or equal to (
>=
): Checks if the left operand is greater than or equal to the right operand.Less than or equal to (
<=
): Checks if the left operand is less than or equal to the right operand.
4. Logical Operators
Logical operators perform logical operations and return a Boolean result.
Logical AND (
&&
): Returnstrue
if both operands aretrue
.Logical OR (
||
): Returnstrue
if at least one operand istrue
.Logical NOT (
!
): Returnstrue
if the operand isfalse
, and vice versa.
Conclusion
These basic operators in C provide the foundation for performing various operations in your programs. As you progress, you'll encounter more operators and gain a deeper understanding of their usage.
In the upcoming sections, we'll explore more advanced topics in C programming. If you have specific questions or areas you'd like to delve into further, feel free to ask. Happy coding!