30% Therapy – 40% Practice – 30% Work project

Unary Operators in C



While most of the operators in C are binary in nature, there are a few unary operators as well. An operator is said to be unary if it takes just a single operand, unlike a binary operator which needs two operands.

Some operators in C are binary as well as unary in their usage. Examples of unary operators in C include ++, , !, etc.

The Increment Operator in C

The increment operator (++) adds 1 to the value of its operand variable and assigns it back to the variable.

The statement a++ is equivalent to writing “a = a + 1.” The “++” operator can appear before or after the operand and it will have the same effect. Hence, a++ is equivalent to ++a.

However, when the increment operator appears along with other operators in an expression, its effect is not the same. The precedence of “prefix ++” is more than “postfix ++”. Hence, “b = a++;” is not the same as “b = ++a;

In the former case, “a” is assigned to “b” before the incrementation; while in the latter case, the incrementation is performed before the assignment.

The Decrement Operator in C

The decrement operator (–) subtracts 1 from the value of its operand variable and assigns it back to the variable.

The statement “a–;” is equivalent to writing “a = a – 1;

The “–” operator can appear before or after the operand and in either case, it will have the same effect. Hence, “a–” is equivalent to “–a“.

However, when the decrement operator appears along with other operators in an expression, its effect is not the same. The precedence of “prefix –” is more than “postfix –“. Hence, “b = a–” is not the same as “b = –a“.

In the former case, “a” is assigned to “b” before the decrementation; while in the latter case, the decrementation is performed before the assignment.

The Unary “+” Operator in C

The “+” and “–” operators are well known as binary addition and subtraction operators. However, they can also be used in unary fashion. When used as unary, they are prefixed to the operand variable.

The “+” operator is present implicitly whenever a positive value is assigned to any numeric variable. The statement “int x = 5;” is same as “int x = +5;“. The same logic applies to float and char variable too.

Example

Take a look at the following example −

#include <stdio.h>

int main(){

   char x = ''A
   char y = +x;

   float a = 1.55;
   float b = +a;

   printf ("x: %c y: %cn", x,y);
   printf ("a: %f y: %fn", a,b);

   return 0;
}

Output

When you run this code, it will produce the following output −

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

The Unary “−” Operator in C

The “” symbol, that normally represents the subtraction operator, also acts the unary negation operator in C. The following code shows how you can use the unary negation operator in C.

Example

In this code, the unary negation operator returns the negative value of “x” and assigns the same to another variable “y”.

#include <stdio.h>

int main(){

   int x = 5;
   int y = -x;

   printf("x: %d y: %dn", x, y);

   return 0;
}

Output

Run the code and check its output −

x: 5 y: -5

The Address-of Operator (&) in C

We use the & symbol in C as the binary AND operator. However, we also use the same & symbol in unary manner as the “address-of” operator.

Example

The & operator returns the memory address of its variable operand. Take a look at the following example −

#include <stdio.h>

int main(){

   char x = ''A
   printf ("Address of x: %dn", &x);

   return 0;
}

Output

Run the code and check its output −

Address of x: 6422047

Note: The C compiler assigns a random memory address whenever a variable is declared. Hence, the result may vary every time the address is printed.

The format specifier %p is used to get a hexadecimal representation of the memory address.

char x = ''A
printf ("Address of x: %pn", &x);

This prints the address of “x” in hexadecimal format −

Address of x: 000000000061FE1F

The address of a variable is usually stored in a “pointer variable”. The pointer variable is declared with a “*” prefix. In the code snippet below, “x” is a normal integer variable while “y” is a pointer variable.

int x = 10;
int *y = &x;

The Dereference Operator (*) in C

We normally use the “*” symbol as the multiplication operator. However, it is also used as the “dereference operator” in C.

When you want to store the memory address of a variable, the variable should be declared with an asterisk (*) prefixed to it.

int x = 10;
int *y = &x;

Here the variable “y” stores the address of “x”, hence “y” acts as a pointer to “x”. To access the value of “x” with the help of its pointer, use the dereference operator (*).

Example 1

Take a look at the following example −

#include <stdio.h>

int main(){

   int x = 10;
   int *y = &x;

   printf ("x: %d Address of x: %dn", x, &x);
   printf("Value at x with Dereference: %d", *y);

   return 0;
}

Output

Run the code and check its output −

x: 10 Address of x: 6422036
Value at x with Dereference: 10

Example 2

You can also assign a value to the original variable with the help of the dereference pointer −

#include <stdio.h>

int main(){

   int x = 10;
   
   int *y = &x;

   printf("x: %d Address of x %dn", x, &x);
   
   *y = 20;
   
   printf("x: %d with Dereference: %d", x, *y);

   return 0;
}

Output

Run the code and check its output −

x: 10 Address of x: 6422036
x: 20 with dereference: 20

The Logical NOT Operator (!) in C

The logical NOT operator (!) in C negates the value of a Boolean operand. True becomes False and False becomes True. The logical NOT operator (!) is a unary operator.

Example 1

The following example shows the usage of logical operators in C −

#include <stdio.h>

int main(){

   int a = 0;
   int b = 20;	
	
   if (!(a && b)){
      printf("Line 1 - Condition is truen" );
   }
   
   return 0;	
}

Output

Line 1 - Condition is true

Example 2

The following C code employs the NOT operator in a while loop −

#include <stdio.h>

int main(){

   int i = 0;

   while (!(i > 5)){
      printf("i = %dn", i);
      i++;
   }
   
   return 0;
}

Output

In this code, the while loop continues to iterate till the expression “!(i > 5)” becomes False, which will be when the value of “i” becomes more than 5.

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

The 1”s Complement Operator (~) in C

The 1”s complement operator (~) in C is a unary operator, needing just one operand. It has the effect of “flipping” the bits, which means the 1”s are replaced by 0”s and vice versa in the binary representation of any number.

a ~a
0 1
1 0

Assuming that the int variable “a” has the value 60 (equivalent to 0011 1100 in binary), the “~a” operation results in -61 in 2’s complement form, as per the bitwise right-shift of its corresponding bits.

~ 0011 1100 = 1100 0011

The binary number “1100 0011” corresponds to -61 in decimal.

Example

Take a look at this example code −

#include <stdio.h>

int main(){

   int a = 60;	/* 60 = 0011 1100 */
   int c = 0;

   c = ~a;      /* -61 = 1100 0011 */

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

   return 0;
}

Output

When you run this code, it will produce the following output −

Value of c is -61
Translate »