Unary operators in C operate on a single operand and perform various tasks such as incrementing, decrementing, negating, or getting the size of the operand.
They are commonly used in expressions, loops, and conditions to manipulate variable values.
1. Types of Unary Operators in C
Operator | Symbol | Description | Example |
---|---|---|---|
Increment | ++ | Increases the value of a variable by 1 | ++a or a++ |
Decrement | — | Decreases the value of a variable by 1 | –a or a– |
Unary plus | + | Indicates a positive value (no effect) | +a |
Unary minus | – | Negates the value of a variable | -a |
Logical NOT | ! | Inverts the logical state (true/false) | !a |
Bitwise NOT | ~ | Inverts each bit (0 to 1 and vice versa) | ~a |
Address | & | Returns the memory address of a variable | &a |
Dereference | * | Accesses the value at an address | *ptr |
Sizeof | sizeof | Returns the size of a variable or type in bytes | sizeof(a) |
2. Increment Operator (++)
The ++ operator increases the value of a variable by 1. It has two forms:
- Prefix (++a): Increments the value before using it in the expression.
- Postfix (a++): Uses the value in the expression first, then increments it.
Example: Prefix and Postfix Increment
#include <stdio.h> int main() { int a = 5; printf("Initial value of a: %d\n", a); printf("Using prefix increment (++a): %d\n", ++a); // Increments, then uses printf("Using postfix increment (a++): %d\n", a++); // Uses, then increments printf("Value of a after postfix increment: %d\n", a); return 0; }
Explanation:
- ++a increments a and then uses the incremented value.
- a++ uses the current value of a, then increments it afterward.
Output:
Initial value of a: 5 Using prefix increment (++a): 6 Using postfix increment (a++): 6 Value of a after postfix increment: 7
3. Decrement Operator (–)
The — operator decreases the value of a variable by 1. It also has two forms:
- Prefix (–a): Decrements the value before using it in the expression.
- Postfix (a–): Uses the value in the expression first, then decrements it.
Example: Prefix and Postfix Decrement
#include <stdio.h> int main() { int b = 5; printf("Initial value of b: %d\n", b); printf("Using prefix decrement (--b): %d\n", --b); // Decrements, then uses printf("Using postfix decrement (b--): %d\n", b--); // Uses, then decrements printf("Value of b after postfix decrement: %d\n", b); return 0; }
Explanation:
- –b decrements b and then uses the decremented value.
- b– uses the current value of b, then decrements it afterward.
Output:
Initial value of b: 5 Using prefix decrement (--b): 4 Using postfix decrement (b--): 4 Value of b after postfix decrement: 3
4. Unary Plus (+) and Unary Minus (-)
- Unary Plus (+): Indicates a positive value. It has no effect on the value.
- Unary Minus (-): Changes the sign of the operand, making positive values negative and vice versa.
Example: Unary Plus and Minus
#include <stdio.h> int main() { int x = 10; int y = -15; printf("Unary plus (+x): %d\n", +x); // No effect printf("Unary minus (-x): %d\n", -x); // Changes to negative printf("Unary minus (-y): %d\n", -y); // Changes to positive return 0; }
Output:
Unary plus (+x): 10 Unary minus (-x): -10 Unary minus (-y): 15
5. Logical NOT Operator (!)
The ! operator inverts the logical state of its operand. If the operand is true (non-zero), ! makes it false (0); if false (0), it makes it true (1).
Example: Logical NOT
#include <stdio.h> int main() { int a = 0; int b = 10; printf("Logical NOT of a (!a): %d\n", !a); // 0 becomes 1 printf("Logical NOT of b (!b): %d\n", !b); // Non-zero becomes 0 return 0; }
Output:
Logical NOT of a (!a): 1 Logical NOT of b (!b): 0
6. Bitwise NOT Operator (~)
The ~ operator inverts each bit of an integer (0 to 1 and 1 to 0). This operation is performed in two’s complement form, which results in flipping all bits and then changing the sign of the value.
Example: Bitwise NOT
#include <stdio.h> int main() { int num = 5; // Binary: 0000 0101 printf("Bitwise NOT of num (~num): %d\n", ~num); // Inverts bits return 0; }
Explanation:
- 5 in binary is 0000 0101. The bitwise NOT operation results in 1111 1010 (two’s complement for -6).
Output:
Bitwise NOT of num (~num): -6
7. Address Operator (&)
The & operator is used to get the memory address of a variable, creating a pointer to that variable.
Example: Using the Address Operator
#include <stdio.h> int main() { int a = 10; int *ptr = &a; // Pointer to a, storing the address of a printf("Address of a: %p\n", &a); // Using & operator printf("Value stored in ptr: %p\n", ptr); return 0; }
Explanation:
- &a returns the address of a.
- ptr is a pointer that holds the address of a.
Output:
Address of a: 0x7ffdfd13a5b8 (example address) Value stored in ptr: 0x7ffdfd13a5b8 (example address)
8. Dereference Operator (*)
The * operator (when used with pointers) accesses the value at the memory address stored in a pointer. This is called dereferencing a pointer.
Example: Using Dereference Operator
#include <stdio.h> int main() { int b = 20; int *ptr = &b; printf("Value of b: %d\n", b); printf("Value of b using dereferencing (*ptr): %d\n", *ptr); return 0; }
Explanation:
- *ptr accesses the value stored at the address ptr points to, which is the value of b.
Output:
Value of b: 20 Value of b using dereferencing (*ptr): 20
9. Sizeof Operator (sizeof)
The sizeof operator returns the size of a variable or data type in bytes. It’s commonly used to determine the memory size of different data types.
Example: Using sizeof Operator
#include <stdio.h> int main() { int a = 10; double b = 20.5; char c = 'A'; printf("Size of int: %lu bytes\n", sizeof(a)); printf("Size of double: %lu bytes\n", sizeof(b)); printf("Size of char: %lu byte\n", sizeof(c)); printf("Size of float type: %lu bytes\n", sizeof(float)); return 0; }
Explanation:
- sizeof(a) returns the size of a in bytes.
- sizeof(float) returns the size of a float type.
Output (sizes may vary based on system):
Size of int: 4 bytes Size of double: 8 bytes Size of char: 1 byte Size of float type: 4 bytes
Summary Table of Unary Operators
Operator | Description | Example |
---|---|---|
++ | Increment: increases the value by 1 | ++a, a++ |
— | Decrement: decreases the value by 1 | –a, a– |
+ | Unary plus: indicates a positive value (no effect) | +a |
– | Unary minus: negates the value | -a |
! | Logical NOT: inverts the logical state | !a |
~ | Bitwise NOT: inverts each bit | ~a |
& | Address: returns the memory address of a variable | &a |
* | Dereference: accesses the value at an address | *ptr |
sizeof | Returns the size of a variable or type in bytes | sizeof(a) |
Complete Example: Using Various Unary Operators
#include <stdio.h> int main() { int a = 5; int b = -3; int *ptr = &a; printf("Increment: ++a = %d\n", ++a); // Prefix increment printf("Decrement: --b = %d\n", --b); // Prefix decrement printf("Unary plus: +b = %d\n", +b); // Unary plus printf("Unary minus: -a = %d\n", -a); // Unary minus printf("Logical NOT: !b = %d\n", !b); // Logical NOT printf("Bitwise NOT: ~a = %d\n", ~a); // Bitwise NOT printf("Address of a: %p\n", (void*)&a); // Address of printf("Dereference ptr: *ptr = %d\n", *ptr); // Dereference printf("Size of int: %lu bytes\n", sizeof(a)); // Sizeof return 0; }
This program demonstrates the use of each unary operator in C.