common.loading

C Operators: AND, OR, and Pointer Operations in C Code

0
C Operators: AND, OR, and Pointer Operations in C Code
Helpful
0
Not Helpful

In the C programming language, operators play a fundamental role in performing calculations, comparisons, and logical decisions. Whether you're working with numbers or making decisions based on conditions, understanding operators in C is crucial. They form the building blocks of every program by helping developers manipulate data, control program flow, and execute tasks efficiently. Common questions like "What does C operator do?" or "What is in C operator?" reflect the importance of understanding how these tools work in real-world scenarios.

Some of the most frequently used operators in C include arithmetic operators such as addition (+) and multiplication (*), relational operators like greater than (>), and logical operators such as AND (&&) or OR (||). Additionally, specialized operators like the pointer dereference (*) or address-of operator (&) allow advanced memory manipulation. This introduction to operators in C lays the groundwork for understanding how programs process data and execute commands.

The operators help you:

  • Perform calculations (like addition, subtraction, etc.).

  • Compare values (like checking if one number is greater than another).

  • Combine conditions (like checking if two conditions are true at the same time).

  • Assign values to variables (like storing a result in a variable).

Without operators, you wouldn’t be able to do much in a program because they are the building blocks for performing actions on data.

Arithmetic Operators in C

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division. They are the most commonly used operators in programming because almost every program involves some kind of calculation.

Addition (+): The + operator is used to add two numbers together.

int a = 5, b = 3; int sum = a + b; // sum is 8

Subtraction (-): The - operator subtracts one number from another.

int a = 10, b = 4; int difference = a - b; // difference is 6

Multiplication (*): The * operator multiplies two numbers.

int a = 6, b = 7; int product = a * b; // product is 42

Division (/): The / operator divides one number by another. Note that in integer division, the result will be truncated (no decimal points).

int a = 20, b = 4; int quotient = a / b; // quotient is 5

Note: If one of the numbers is a float, the result will also be a float. For example:

float a = 20, b = 3; float quotient = a / b; // quotient is approximately 6.6667

Modulus (%): The % operator gives the remainder of the division of one number by another. It works only with integers.

int a = 10, b = 3; int remainder = a % b; // remainder is 1

Increment (++): The ++ operator increases the value of a variable by 1. It has two forms:

  • Prefix: ++a (increments a before using it in the expression)
  • Postfix: a++ (uses the value of a first, then increments it)
int a = 5; int b = ++a; // a is incremented to 6, then b is assigned 6 int c = a++; // c is assigned 6, then a is incremented to 7

Decrement (--): The -- operator decreases the value of a variable by 1. Like increment, it also has prefix and postfix forms.

int a = 5; int b = --a; // a is decremented to 4, then b is assigned 4 int c = a--; // c is assigned 4, then a is decremented to 3

Table Summary of Arithmetic Operators

OperatorNameExampleResult (if a = 10, b = 3)
+Additiona + b13
-Subtractiona - b7
*Multiplicationa * b30
/Divisiona / b3 (integer division)
%Modulusa % b1 (remainder of 10 ÷ 3)
++Incrementa++ or ++aa becomes 11
--Decrementa-- or --aa becomes 9

Relational Operators in C

Relational operators are used to compare two values or variables. The result of using a relational operator is either true (1) or false (0). These operators are commonly used in decision-making statements like if, while, and for.

Equal to (==): The == operator checks if two values are equal. If they are, the result is true; otherwise, it’s false.

int a = 5, b = 5; if (a == b) { printf("a and b are equal\n"); // This will print }

Not equal to (!=): The != operator checks if two values are not equal. If they are not equal, the result is true.

int a = 5, b = 3; if (a != b) { printf("a and b are not equal\n"); // This will print }

Greater than (>): The > operator checks if the left-hand value is greater than the right-hand value.

int a = 10, b = 5; if (a > b) { printf("a is greater than b\n"); // This will print }

Less than (<): The < operator checks if the left-hand value is less than the right-hand value.

int a = 3, b = 8; if (a < b) { printf("a is less than b\n"); // This will print }

Greater than or equal to (>=): The >= operator checks if the left-hand value is greater than or equal to the right-hand value.

int a = 5, b = 5; if (a >= b) { printf("a is greater than or equal to b\n"); // This will print }

Less than or equal to (<=): The <= operator checks if the left-hand value is less than or equal to the right-hand value.

int a = 4, b = 6; if (a <= b) { printf("a is less than or equal to b\n"); // This will print }

Summary Table of Relational Operators

OperatorNameExampleResult (if a = 10, b = 20)
==Equal toa == b0 (false)
!=Not equal toa != b1 (true)
>Greater thana > b0 (false)
<Less thana < b1 (true)
>=Greater than or equal toa >= b0 (false)
<=Less than or equal toa <= b1 (true)

Logical Operators in C

Logical operators are used to combine or modify the results of relational or boolean expressions. These are crucial for making decisions in programs based on multiple conditions.

Logical AND (&&): The && operator checks if both conditions are true. If either condition is false, the result is false.

int a = 5, b = 10; if (a > 0 && b > 0) { printf("Both a and b are positive\n"); // This will print }

Logical OR (||): The || operator checks if at least one of the conditions is true. If both conditions are false, the result is false.

int a = -5, b = 10; if (a > 0 || b > 0) { printf("At least one of a or b is positive\n"); // This will print }

Logical NOT (!): The ! operator reverses the truth value of a condition. If the condition is true, it becomes false, and if it is false, it becomes true.

int a = 5; if (!(a < 0)) { printf("a is not negative\n"); // This will print }

Table Summary of Logical Operators

OperatorDescriptionExampleResult

[table]

[table]

[table]

Assignment Operators in C

Assignment operators are used to assign values to variables. Some of them also perform an operation (like addition or multiplication) before assigning the result to the variable.

Simple Assignment (=): The = operator assigns the value on the right to the variable on the left.

int a; a = 10; // a is assigned the value 10

Add and Assign (+=): The += operator adds the right-hand value to the variable on the left and then assigns the result to the variable.

int a = 5; a += 3; // a is now 8

Subtract and Assign (-=): The -= operator subtracts the right-hand value from the variable on the left and then assigns the result to the variable.

int a = 10; a -= 4; // a is now 6

Multiply and Assign (*=): The *= operator multiplies the variable on the left by the right-hand value and then assigns the result to the variable.

int a = 4; a *= 2; // a is now 8

Divide and Assign (/=): The /= operator divides the variable on the left by the right-hand value and then assigns the result to the variable.

int a = 20; a /= 5; // a is now 4

Modulus and Assign (%=): The %= operator finds the remainder when the variable on the left is divided by the right-hand value and then assigns the result to the variable.

int a = 10; a %= 3; // a is now 1

Table Summary of Assignment Operators

OperatorDescriptionExampleResult

[table]

[table]

[table]

[table]

[table]

[table]

Bitwise Operators in C

Bitwise operators operate directly on the binary representation of numbers. These operators are typically used for low-level programming tasks like setting, toggling, or checking specific bits in a variable.

Bitwise AND (&): The & operator performs a bitwise AND operation, comparing each bit of two numbers. The result is 1 only if both corresponding bits are 1.

int a = 5; // Binary: 0101 int b = 3; // Binary: 0011 int result = a & b; // Binary: 0001, result is 1

Bitwise OR (|): The | operator performs a bitwise OR operation. The result is 1 if at least one of the corresponding bits is 1.

int a = 5; // Binary: 0101 int b = 3; // Binary: 0011 int result = a | b; // Binary: 0111, result is 7

Bitwise XOR (^): The ^ operator performs a bitwise XOR (exclusive OR) operation. The result is 1 if the corresponding bits are different.

int a = 5; // Binary: 0101 int b = 3; // Binary: 0011 int result = a ^ b; // Binary: 0110, result is 6

Bitwise NOT (~): The ~ operator inverts all the bits of its operand. Each 0 becomes 1, and each 1 becomes 0.

int a = 5; // Binary: 0101 int result = ~a; // Binary: 1010 (in 32-bit representation, result is -6)

Left Shift (<<): The << operator shifts the bits of the left operand to the left by the number of positions specified by the right operand. New bits on the right are filled with 0.

int a = 5; // Binary: 0101 int result = a << 1; // Binary: 1010, result is 10

Right Shift (>>): The >> operator shifts the bits of the left operand to the right by the number of positions specified by the right operand. For signed integers, the leftmost bit depends on the system (arithmetic or logical shift).

int a = 5; // Binary: 0101 int result = a >> 1; // Binary: 0010, result is 2

Table Summary of Bitwise Operators

OperatorDescriptionExampleResult
&Bitwise AND5 & 31 (0101 & 0011 = 0001)
``Bitwise OR`5
^Bitwise XOR5 ^ 36 (0101 ^ 0011 = 0110)
~Bitwise NOT~5-6 (inverts bits)
<<Left Shift5 << 110 (0101 << 1 = 1010)
>>Right Shift5 >> 12 (0101 >> 1 = 0010)

Share