Unary Operators in C (original) (raw)

In C programming, unary operators are operators that operate on a single operand. These operators are used to perform operations such as negation, incrementing or decrementing a variable, or checking the size of a variable. They provide a way to modify or manipulate the value of a single variable in an efficient manner.

C provides 9 unary operators that can be used to perform various operations on a single variable. These include:

Table of Content

**Increment Operator (++)

The **increment operator ( ++ )is used to increment the value of the variable by 1. The increment can be done in two ways:

**A. Prefix Increment

In this method, the operator precedes the operand (e.g., ++a). The value of the operand will be altered _before it is used.For example:

**int a = 1;
**int b = ****++a**; // b = 2

**B. Postfix Increment

In this method, the operator follows the operand (e.g., a++). The value operand will be altered _after it is used.For example:

**int a = 1;
**int b = **a++; // b = 1
**int c = **a; // c = 2

Below example shows the implementation of **increment ( ++ ):

C `

#include <stdio.h> int main(){ int a = 1; int b = 1; printf("Pre-Incrementing a = %d\n", ++a); printf("Post-Incrementing b = %d", b++); return 0; }

`

Output

Pre-Incrementing a = 2 Post-Incrementing b = 1

**Decrement Operator (--)

The **decrement operator ( -- ) is used to decrement the value of the variable by 1. The decrement can be done in two ways:

**A. Prefix Decrement

In this method, the operator precedes the operand (e.g., – -a). The value of the operand will be altered _before it is used.For example:

**int a = 1;
**int b = --a; // b = 0

**B. Postfix Decrement

In this method, the operator follows the operand (e.g., a- -). The value of the operand will be altered _after it is used.For example:

**int a = 1;
**int b = **a--; // b = 1
**int c = **a; // c = 0

Below example shows the implementation of **decrement (--):

C `

#include <stdio.h>

int main() { int a = 1; int b = 1; printf("Pre-Decrementing a = %d\n", --a); printf("Post-Decrementing b = %d", b--); return 0; }

`

Output

Pre-Decrementing a = 0 Post-Decrementing b = 1

**Unary Plus

The unary plus (+) operator does not change the sign of its argument; it simply returns the value as is. It is often used for code clarity rather than functionality.

int a = -10;
int b = +a; // b = -10

The unary plus is different from the addition operator, as addition requires two operands.

Below is the implementation of the unary plus (+) operator:

C `

#include <stdio.h> int main() {

// Declaring a negative integer
int a = -10;

// Using unary plus to keep value unchanged
int b = +a;  
printf("%d\n", a);
printf("%d", b);
return 0;

}

`

**Unary Minus

The **minus operator ( - ) changes the sign of its argument. A positive number becomes negative, and a negative number becomes positive.

int a = 10;
**int b = **-a; // b = -10

Unary minus is different from the subtraction operator, as subtraction requires two operands.

Below is the implementation of the **unary minus (-) operator:

C `

#include <stdio.h> int main(){

// declaring a positive integer
int a = 10;

// using - sign to make the value of positive integers
// to negative
int b = -a;
printf("%d\n", a);
printf("%d", b);
return 0;

}

`

**Logical NOT ( ! )

The **logical NOT operator ( ! ) is used to reverse the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false.

**Example:

If x is true, then !x is false
If x is false, then !x is true

Below is the implementation of the NOT (!) operator:

C `

#include <stdio.h> int main(){

int a = 10;
int b = 5;

if (!(a > b))
    printf("b is greater than a\n");
else
    printf("a is greater than b");

return 0;

}

`

Output

a is greater than b

**Bitwise NOT ( ~ )

The bitwise NOT (~) operator inverts all bits of its operand. Each 0 becomes 1, and each 1 becomes 0. It effectively calculates the two’s complement negative equivalent of a number in signed integers.

x = 5 (00000101 in binary),
~x = ~6 (11111010 in binary, two's complement representation)

Below is the implementation of the bitwise NOT (~) operator:

C `

#include <stdio.h> int main() {

// Declaring an integer
int x = 5;

// Applying bitwise NOT
int res = ~x;

printf("x = %d\n", x);
printf("~x = %d\n", res);
return 0;

}

`

**Addressof Operator (&)

The **addressof operator ( & ) gives an address of a variable. It is used to return the memory address of a variable. These addresses returned by the address-of operator are known as pointers because they “point” to the variable in memory.

**Example:

& gives an address on variable n
int a;
int *ptr;
ptr = &a; // address of a is copied to the location ptr.

Below is the implementation of the **Addressof operator(&):

C `

#include <stdio.h> int main(){

int a = 20;
printf("%p", &a);

return 0;

}

`

**Indirection Operator (*)

The **indirection operator (*), also known as the dereference operator, is used to access the value stored at a memory address. It is used with pointers to retrieve the value stored at the referenced memory location.

a = 20;
ptr = &a;
*ptr // This will give 20

Below is the implementation of indirection operator:

C `

#include <stdio.h> int main(){ int a = 20; int *ptr = &a; printf("%d", *ptr); return 0; }

`

**sizeof()

This operator returns the size of its operand, in bytes. The **sizeof() operator always precedes its operand. The operand is an expression, or it may be a cast.

**Note: The `sizeof()` operator in C++ is machine dependent. For example, the size of an 'int' in C++ may be 4 bytes in a 32-bit machine but it may be 8 bytes in a 64-bit machine.

Below is the implementation of sizeof() operator:

C `

#include <stdio.h> int main(){

// printing the size of double and int using sizeof
printf("%d\n", sizeof(double));
printf("%d", sizeof(int));

return 0;

}

`