Unary vs Binary vs Ternary Operators in C

SPONSOR AD

Unary vs. Binary vs. Ternary Operators in C

In this article, I will discuss Unary vs. Binary vs. Ternary Operators in C with Examples. Please read our previous article discussing Unary Operators in C Language. In C programming, operators are symbols that tell the compiler to perform specific mathematical or logical manipulations. Operators are used in programs to manipulate data and variables. They can be categorized into unary, binary, and ternary operators based on the number of operands they take.

Unary Operators in C

Unary operators in C are operators that operate on a single operand. They are used to perform various operations, such as incrementing/decrementing a value, negating an expression, or dereferencing a pointer. Here are the most commonly used unary operators in C:

Increment (++): Increases the value of its operand by 1. It can be used in two forms:

  • Prefix (++x): Increments the value of x and then returns the new value.
  • Postfix (x++): Returns the value of x and then increments it.

Decrement (–): Decreases the value of its operand by 1. Similar to increment, it also has two forms:

SPONSOR AD
  • Prefix (–x): Decrements the value of x and then returns the new value.
  • Postfix (x–): Returns the value of x and then decrements it.

Unary Minus (-): Negates the value of its operand.

Logical NOT (!): Inverts the boolean value of its operand. If the operand is non-zero, it returns 0 (false), and if the operand is zero, it returns 1 (true).

Bitwise NOT (~): Performs a bitwise negation on its operand, inverting each bit.

Address-of (&): Returns the memory address of its operand.

Dereference (*): Accesses the value at the address pointed to by its operand (the opposite of the address-of operator).

Sizeof: Returns the size of a data type or an object in bytes.

These unary operators are widely used in C programming for various purposes, including memory management, arithmetic operations, and logical operations.

SPONSOR AD

Binary Operators in C

In the C programming language, binary operators are used to perform operations on two operands. Here are the most commonly used binary operators in C:

Arithmetic Operators:
  • Addition (+): Adds two operands. E.g., a + b.
  • Subtraction (-): Subtracts the second operand from the first. E.g., a – b.
  • Multiplication (*): Multiplies two operands. E.g., a * b.
  • Division (/): Divides the numerator by the denominator. E.g., a / b.
  • Modulus (%): Returns the remainder of a division. E.g., a % b.
Relational Operators:
  • Equal to (==): Checks if two operands are equal. Returns true if they are. E.g., a == b.
  • Not equal to (!=): Checks if two operands are not equal. E.g., a != b.
  • Greater than (>): Checks if the left operand is greater than the right operand. E.g., a > b.
  • Less than (<): Checks if the left operand is less than the right operand. E.g., a < b.
  • Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand. E.g., a >= b.
  • Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand. E.g., a <= b.
Logical Operators:
  • Logical AND (&&): Returns true if both operands are true. E.g., a && b.
  • Logical OR (||): Returns true if either of the operands is true. E.g., a || b.
  • Logical NOT (!): Used to reverse the logical state of its operand. E.g., !a.
Bitwise Operators:
  • Bitwise AND (&): Performs a bitwise AND on two integers. E.g., a & b.
  • Bitwise OR (|): Performs a bitwise OR on two integers. E.g., a | b.
  • Bitwise XOR (^): Performs a bitwise exclusive OR on two integers. E.g., a ^ b.
  • Bitwise left shift (<<): Shifts the bits of the first operand to the left by the number of positions specified by the second operand. E.g., a << 2.
  • Bitwise right shift (>>): Shifts the bits of the first operand to the right by the number of positions specified by the second operand. E.g., a >> 2.
Assignment Operators:
  • Simple assignment (=): Assigns the value of the right operand to the left operand. E.g., a = b.
  • Add and assign (+=): Adds the right operand to the left operand and assigns the result to the left operand. E.g., a += b.
  • Subtract and assign (-=): Subtracts the right operand from the left operand and assigns the result to the left operand. E.g., a -= b.
  • Multiply and assign (*=): Multiply the right operand with the left operand and assign the result to the left operand. E.g., a *= b.
  • Divide and assign (/=): Divides the left operand by the right operand and assigns the result to the left operand. E.g., a /= b.
  • Modulus and assign (%=): Applies modulus operation and assigns the result to the left operand. E.g., a %= b.

These binary operators are fundamental to performing calculations, making comparisons, and manipulating data at the bit level in C programming.

Ternary Operators in C

Ternary operators in C are a shorthand way of writing conditional statements. They are a compact form of the if-else statement and are often used for simple conditional assignments. The syntax of the ternary operator in C is:

condition ? expression1 : expression2;

Here’s how it works:

  • condition is an expression that evaluates to true or false.
  • expression1 is the value or statement that is executed if the condition is true.
  • expression2 is the value or statement that is executed if the condition is false.

For example:

int a = 5, b = 10;
int max;
max = (a > b) ? a : b;

In this example, the ternary operator checks if a is greater than b. If true, max is assigned the value of a; otherwise, it is assigned the value of b.

In the next article, I am going to discuss Variables in C Programming Language with Examples. Here, in this article, I explain Unary vs Binary vs Ternary Operators in C Language with examples. I hope you enjoy this Unary vs Binary vs Ternary Operators in C Language article. Please give your feedback and suggestions about this article.

SPONSOR AD
SPONSOR AD

Leave a Reply

Your email address will not be published. Required fields are marked *