Unary Operators in C: Understanding Their Usage

Unary operators in C are operators that act on a single operand, contrasting with binary operators that require two. This section covers key unary operators like ++, --, and !, focusing on their functionalities and differences, particularly with the increment operator (++), which adds one to its operand and can be used in both prefix and postfix forms.



Unary Operators in C

While most operators in C are binary, there are also unary operators. A unary operator operates on a single operand, unlike binary operators that require two operands. Examples of unary operators in C include ++, --, and !.

The Increment Operator (++)

The increment operator ++ adds 1 to its operand and assigns the result back to the operand. It can appear before or after the operand, but its effect can vary depending on its position relative to other operators.

a++ is equivalent to a = a + 1. The ++ operator can be used in prefix form (++a) or postfix form (a++). The difference in usage affects the order of operations in expressions.

Example

Code

#include <stdio.h>

int main(){
   int a = 5;
   int b;

   b = a++;  // Postfix increment
   printf("b: %d, a: %d\n", b, a);

   b = ++a;  // Prefix increment
   printf("b: %d, a: %d\n", b, a);

   return 0;
}
Output

b: 5, a: 6
b: 7, a: 7

The Decrement Operator (--)

The decrement operator -- subtracts 1 from its operand and assigns the result back to the operand. Like the increment operator, it can appear in prefix (--a) or postfix form (a--).

a-- is equivalent to a = a - 1. The effect on the operand depends on whether the operator is used before or after the operand in expressions.

Example

Code

#include <stdio.h>

int main(){
   int a = 5;
   int b;

   b = a--;  // Postfix decrement
   printf("b: %d, a: %d\n", b, a);

   b = --a;  // Prefix decrement
   printf("b: %d, a: %d\n", b, a);

   return 0;
}
Output

b: 5, a: 4
b: 3, a: 3

The Unary + Operator

The + operator can also be used as a unary operator. It is generally used to indicate that a value is positive. This operator is often implicit when assigning positive values to numeric variables.

Example

Code

#include <stdio.h>

int main(){
   char x = 'A';
   char y = +x;

   float a = 1.55;
   float b = +a;

   printf("x: %c y: %c\n", x, y);
   printf("a: %f b: %f\n", a, b);

   return 0;
}
Output

x: A y: A
a: 1.550000 b: 1.550000

The Unary - Operator

The - operator can be used as a unary negation operator to indicate a negative value. It is used to negate the value of its operand.

Example

Code

#include <stdio.h>

int main(){
   int x = 5;
   int y = -x;

   printf("x: %d y: %d\n", x, y);

   return 0;
}
Output

x: 5 y: -5

The Address-of Operator (&)

The & operator returns the memory address of its operand. It is used to get the address of a variable.

Example

Code

#include <stdio.h>

int main(){
   char x = 'A';
   printf("Address of x: %p\n", (void*)&x);

   return 0;
}
Output

Address of x: 0x7ffdfb57f7f5

The Dereference Operator (*)

The * operator, typically used for multiplication, is also used as the dereference operator to access the value at a particular memory address. A pointer variable is declared with an asterisk (*) and holds the address of another variable.

Example 1

Code

#include <stdio.h>

int main(){
   int x = 10;
   int *y = &x;

   printf("x: %d Address of x: %p\n", x, (void*)&x);
   printf("Value at x with Dereference: %d\n", *y);

   return 0;
}
Output

x: 10 Address of x: 0x7ffdfb57f7f4
Value at x with Dereference: 10

Example 2

You can also use the dereference operator to modify the value of the original variable through a pointer.

Code

#include <stdio.h>

int main(){
   int x = 10;
   int *y = &x;

   printf("x: %d Address of x: %p\n", x, (void*)&x);

   *y = 20; // Modify the value of x through the pointer
   printf("x: %d with Dereference: %d\n", x, *y);

   return 0;
}
Output

x: 10 Address of x: 0x7ffdfb57f7f4
x: 20 with Dereference: 20

The Logical NOT Operator (!)

The logical NOT operator ! negates the Boolean value of its operand. It converts true to false and false to true.

Example 1

Code

#include <stdio.h>

int main(){
   int a = 0;
   int b = 20;

   if (!(a && b)){
      printf("Line 1 - Condition is true\n");
   }

   return 0;
}
Output

Line 1 - Condition is true

Example 2

In a while loop, the logical NOT operator can control the loop's execution based on the negated condition.

Code

#include <stdio.h>

int main(){
   int i = 0;

   while (!(i > 5)){
      printf("i = %d\n", i);
      i++;
   }

   return 0;
}
Output

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5

The 1's Complement Operator (~)

The 1's complement operator ~ flips all the bits of its operand. It converts 1's to 0's and 0's to 1's.

Example

Code

#include <stdio.h>

int main(){
   int a = 60; // 0011 1100 in binary
   int c = ~a; // 1100 0011 in binary, which is -61

   printf("Value of c is %d\n", c);

   return 0;
}
Output

Value of c is -61