In C programming, operators are symbols that perform operations on variables and values. The operators discussed in this document include Pre and Post Operators, Unary Operators, and Logical Operators, which are essential tools for manipulating data in microcontroller programming.
Arithmetic operators are essential for performing mathematical operations in C programming. These operators are used for basic arithmetic calculations, such as addition, subtraction, multiplication, and division, which are fundamental in both simple and complex computations in microcontroller programming.
Arithmetic operators perform operations on numeric types (such as int, float, double) and return the result of the operation.
-
+(Addition Operator): Adds the two operands.- It is used to sum two values.
-
-(Subtraction Operator): Subtracts the right operand from the left operand.- It is used to calculate the difference between two values.
-
*(Multiplication Operator): Multiplies two operands.- It is used to compute the product of two values.
-
/(Division Operator): Divides the left operand by the right operand.- It is used for division; however, be mindful of integer division and floating-point division.
-
%(Modulus Operator): Returns the remainder when the left operand is divided by the right operand.- It is particularly useful for finding remainders in division, and it's often used in loops and conditional checks to determine divisibility.
The addition operator adds two operands and returns their sum. It is the most common arithmetic operation used to increase a value or combine two numbers.
int a = 10, b = 5;
int result = a + b; // result will be 15- Here,
aandbare added together, so the result will be15.
The subtraction operator subtracts the right operand from the left operand. It is used to calculate differences or decrease a value by a specific amount.
int a = 10, b = 5;
int result = a - b; // result will be 5- Here,
bis subtracted froma, so the result will be5.
The multiplication operator multiplies two operands. It is used when you need to calculate the product of two values.
int a = 10, b = 5;
int result = a * b; // result will be 50- Here,
aandbare multiplied, so the result will be50.
The division operator divides the left operand by the right operand. This operator is often used for computing ratios, averages, or when breaking something into smaller parts.
int a = 10, b = 3;
int result = a / b; // result will be 3 because of integer division- Note: In integer division, any fractional part is truncated (i.e., rounded down to the nearest integer).
- In this case,
10 / 3equals3.3333, but sinceaandbare integers, the fractional part is discarded, and the result is3.
- In this case,
If you need to preserve the decimal part, you should use floating-point numbers (float or double).
float a = 10, b = 3;
float result = a / b; // result will be 3.3333- Here, since
aandbare floating-point numbers, the result retains the decimal portion (3.3333).
The modulus operator returns the remainder of division between the left operand and the right operand. It is primarily used to check divisibility and for operations that need to handle remainders.
int a = 10, b = 3;
int result = a % b; // result will be 1 (the remainder of 10 divided by 3)- Here,
10 % 3gives the remainder of the division, which is1(because10 / 3 = 3with a remainder of1).
- Integer Division: When both operands are integers, the result of division is an integer, and any fractional part is discarded.
- Floating-Point Division: To preserve the decimal part, at least one operand should be a floating-point type (
floatordouble). - Modulus: Useful for checking if a number is divisible by another (e.g., checking if a number is even or odd).
Relational operators are used to compare two values or variables. They return a boolean value (true or false) based on the comparison. These operators are essential for controlling the flow of the program in conditional statements like if, while, and loops, where decisions are made based on whether two values are equal, greater than, or less than each other.
Relational operators are commonly used for comparing numbers, strings, or any other comparable data types. The result of these comparisons is either true (1) or false (0).
-
==(Equality Operator): Checks if the two operands are equal.- Returns
trueif both operands are equal, andfalseif they are not.
- Returns
-
!=(Inequality Operator): Checks if the two operands are not equal.- Returns
trueif the operands are not equal, andfalseif they are equal.
- Returns
-
>(Greater than Operator): Checks if the left operand is greater than the right operand.- Returns
trueif the left operand is greater, andfalseotherwise.
- Returns
-
<(Less than Operator): Checks if the left operand is smaller than the right operand.- Returns
trueif the left operand is smaller, andfalseotherwise.
- Returns
-
>=(Greater than or Equal to Operator): Checks if the left operand is greater than or equal to the right operand.- Returns
trueif the left operand is greater than or equal to the right, andfalseotherwise.
- Returns
-
<=(Less than or Equal to Operator): Checks if the left operand is smaller than or equal to the right operand.- Returns
trueif the left operand is smaller than or equal to the right, andfalseotherwise.
- Returns
This operator checks whether two values are equal.
int a = 5, b = 10;
if (a == b)
{
// This condition is false because 5 is not equal to 10
}This operator checks whether two values are not equal.
int a = 5, b = 10;
if (a != b)
{
// This condition is true because 5 is not equal to 10
}This operator checks if the left operand is greater than the right operand.
int a = 5, b = 3;
if (a > b)
{
// This condition is true because 5 is greater than 3
}This operator checks if the left operand is smaller than the right operand.
int a = 5, b = 10;
if (a < b)
{
// This condition is true because 5 is less than 10
}This operator checks if the left operand is greater than or equal to the right operand.
int a = 5, b = 5;
if (a >= b)
{
// This condition is true because 5 is equal to 5
}This operator checks if the left operand is smaller than or equal to the right operand.
int a = 5, b = 10;
if (a <= b)
{
// This condition is true because 5 is less than or equal to 10
}- Relational operators help in comparing two values and make decisions based on the comparison.
- They return boolean results:
true(1) if the condition is satisfied, andfalse(0) otherwise. - Relational operators are often used in
ifstatements, loops, and conditional checks for controlling the flow of the program. - Be mindful of data types being compared. For instance, comparing strings or floating-point numbers may require special handling depending on the system or environment.
Assignment operators are used to assign values to variables. These operators are commonly used in expressions where the variable on the left side is updated based on the value of the right-hand side.
The simple assignment operator = is used to assign the value of the right-hand operand to the left-hand operand.
int a = 5; // Assigns the value 5 to variable a- Here, the value
5is assigned to the variablea. After this operation,aholds the value5.
The += operator adds the right-hand operand to the left-hand operand and assigns the result to the left-hand operand.
int a = 5;
a += 3; // Equivalent to: a = a + 3; Now, a becomes 8- Here,
3is added to the current value ofa, which is5, and then the result is assigned back toa. After this operation,abecomes8.
The -= operator subtracts the right-hand operand from the left-hand operand and assigns the result to the left-hand operand.
int a = 10;
a -= 4; // Equivalent to: a = a - 4; Now, a becomes 6- Here,
4is subtracted from the current value ofa, which is10, and the result is assigned back toa. After this operation,abecomes6.
The *= operator multiplies the left-hand operand by the right-hand operand and assigns the result to the left-hand operand.
int a = 5;
a *= 2; // Equivalent to: a = a * 2; Now, a becomes 10- Here,
ais multiplied by2. After this operation,abecomes10.
The /= operator divides the left-hand operand by the right-hand operand and assigns the result to the left-hand operand.
int a = 10;
a /= 2; // Equivalent to: a = a / 2; Now, a becomes 5- Here,
ais divided by2. After this operation,abecomes5.
The %= operator takes the modulus of the left-hand operand by the right-hand operand and assigns the result to the left-hand operand.
int a = 10;
a %= 3; // Equivalent to: a = a % 3; Now, a becomes 1- Here, the remainder when
10is divided by3is calculated, which is1. After this operation,abecomes1.
- Simple Assignment (
=): Assigns the right-hand operand to the left-hand operand. - Compound Assignment Operators (
+=,-=,*=,/=,%=): These operators combine an operation with assignment, allowing you to update the left-hand operand based on an operation with the right-hand operand.+=: Adds the right operand to the left operand.-=: Subtracts the right operand from the left operand.*=: Multiplies the left operand by the right operand./=: Divides the left operand by the right operand.%=: Assigns the remainder of the division of the left operand by the right operand.
The increment operators are used to increase the value of a variable by 1. However, their position determines when the increment happens during the evaluation of an expression.
- Pre-Increment (
++variable): Increments the value of the variable before using it in the expression.
int a = 5;
int result = ++a; // a is incremented to 6, then used in the expressionHere:
-
ais first incremented to 6, and then the new value (6) is assigned toresult. -
Post-Increment (
variable++): Uses the value of the variable first, then increments the value afterward.
int a = 5;
int result = a++; // result is assigned the value 5, then a is incremented to 6Here:
ais first used in the expression with its original value (5), and then it is incremented to 6 after the evaluation.
Similar to the increment operators, decrement operators decrease the value of a variable by 1. The position of the operator determines when the decrement happens during the evaluation.
- Pre-Decrement (
--variable): Decrements the value of the variable before using it in the expression.
int a = 5;
int result = --a; // a is decremented to 4, then used in the expression- Post-Decrement (
variable--): Uses the value of the variable first, then decrements the value afterward.
int a = 5;
int result = a--; // result is assigned the value 5, then a is decremented to 4Unary operators operate on a single operand. They are used to perform operations like incrementing, decrementing, logical negation, and address retrieval. These operators are essential for manipulating data and interacting with memory in embedded systems programming.
- Unary plus (
+): Used to indicate the positive value of a variable (though not commonly needed). - Unary minus (
-): Used to negate a value. - Logical NOT (
!): Inverts the boolean value. If the operand is non-zero, the result isfalse, and if the operand is zero, the result istrue. - Bitwise NOT (
~): Inverts all the bits of a variable. - Address-of (
&): Returns the memory address of a variable. - Dereference (
*): Used with pointers to access the value at the memory address.
The Unary plus (+) operator simply returns the value of the operand without changing it. It is often used to clarify that the operand is a positive number, although in most cases this is redundant because numbers are positive by default unless specified otherwise.
int a = 5;
int result = +a; // result will be 5, as the unary plus does not alter the valueIn this example:
- The unary plus does not affect the value of
a. The result will be the same value,5.
The Unary minus (-) operator negates the value of the operand. It is commonly used to change the sign of a number, turning positive values into negative ones and vice versa.
int a = 5;
int result = -a; // result will be -5In this example:
- The value of
ais negated, soresultbecomes-5.
The Logical NOT (!) operator inverts the boolean value of its operand. If the operand is a non-zero value (which represents true), it becomes zero (representing false), and if the operand is zero (representing false), it becomes one (representing true).
int a = 5;
int result = !a; // result will be 0, because a is non-zeroIn this example:
- Since
ais non-zero (5), the result becomes0(logical negation).
The Bitwise NOT (~) operator inverts all the bits of a variable. It is often used in low-level programming, such as when manipulating bitmasks or performing binary operations.
int a = 5; // In binary: 0000000000000101
int result = ~a; // In binary: 1111111111111010, which is -6 in two's complement representationIn this example:
- The binary representation of
5(0000000000000101) is inverted, resulting in1111111111111010(which represents-6in two's complement format).
The Address-of (&) operator is used to retrieve the memory address of a variable. This is fundamental when working with pointers, as it allows you to reference the location in memory where a variable is stored.
int a = 5;
int *ptr = &a; // ptr stores the address of aIn this example:
- The
&aoperator returns the memory address of the variablea, which is then stored in the pointerptr.
The Dereference (*) operator is used to access the value stored at the memory address pointed to by a pointer. It allows you to manipulate the value of a variable indirectly by accessing its memory location.
int a = 5;
int *ptr = &a; // ptr stores the address of a
int result = *ptr; // result will be 5, as *ptr dereferences the address to access the value of aIn this example:
- The
*ptroperator accesses the value stored at the memory address stored inptr, which is the value ofa(5).
- Unary plus and minus are used for numerical negation and clarifying the sign of a value.
- Logical NOT is used to invert boolean values, making it useful for conditional logic.
- Bitwise NOT inverts the bits of a number, which is essential for low-level programming and bit manipulation.
- Address-of and Dereference operators are fundamental for pointer manipulation, enabling direct memory access and modification.
Logical operators are used to perform logical operations on boolean values. These are typically used in conditional statements, loops, and expressions to evaluate multiple conditions.
- Logical AND (
&&): Returns true if both operands are true. - Logical OR (
||): Returns true if at least one of the operands is true. - Logical NOT (
!): Returns true if the operand is false.
The logical AND operator returns true (1) if both operands are non-zero (true). If either operand is zero (false), the result is false.
int a = 5, b = 10;
if (a > 0 && b > 0)
{
// This condition is true, because both a and b are greater than 0
}The logical OR operator returns true (1) if at least one operand is non-zero. If both operands are zero, the result is false.
int a = 5, b = 0;
if (a > 0 || b > 0)
{
// This condition is true because a is greater than 0
}The logical NOT operator is used to negate a boolean expression. If the expression is true (non-zero), it becomes false (0); if false (0), it becomes true (1).
int a = 0;
if (!a)
{
// This condition is true, because a is 0 and !a becomes 1 (true)
}In C programming, understanding operators is crucial for performing various tasks, such as manipulating data, controlling program flow, and optimizing performance. The operators discussed in this document, including Arithmetic Operators, Relational Operators, Assignment Operators, Pre and Post Operators, Unary Operators, and Logical Operators, are fundamental tools for any C programmer, especially in embedded systems development.
-
Arithmetic Operators: These operators are essential for basic mathematical computations, such as addition, subtraction, multiplication, division, and modulus. They form the foundation for performing calculations in embedded applications.
-
Relational Operators: These operators are used to compare two values and return a boolean result (
trueorfalse), helping in decision-making processes in conditions and loops. -
Assignment Operators: Used to assign values to variables, these operators simplify the process of updating variable values. Compound assignment operators allow for concise expressions that perform arithmetic operations and assign the result back to the variable.
-
Pre and Post Operators: The increment and decrement operators are used for efficiently increasing or decreasing variable values, which is particularly useful in loops and counting mechanisms.
-
Unary Operators: These operators operate on a single operand, and they are crucial for tasks such as negating values, inverting boolean conditions, and handling pointers for memory manipulation.
-
Logical Operators: Logical operators are essential in controlling the flow of a program based on multiple conditions. They are widely used in
ifstatements, loops, and conditional checks.
By mastering these operators, C programmers can write more efficient, concise, and readable code, which is especially important in embedded systems programming, where performance and resource usage are critical. Understanding when and how to use each operator appropriately ensures that the program behaves as expected, leading to more robust and reliable applications.
Effective use of operators in embedded programming can greatly impact the overall performance, memory efficiency, and ease of debugging. Therefore, a thorough understanding of these operators is fundamental to becoming proficient in C programming, particularly for microcontroller and embedded systems development.
If you found this repository useful:
- Subscribe to my YouTube Channel.
- Share this repository with others.
- Give this repository and my other repositories a star.
- Follow my GitHub account.
Feel free to reach out to me through any of the following platforms: