C Programs Tutorials | IT Developer
IT Developer

C Programming - C Unary Operators



Share with a Friend

C Programming - C Unary Operators

C Unary Operators

Unary operators in C operate on a single operand to perform various operations like incrementing, negating, or checking the size of a value. These operators are crucial for concise and efficient programming.

List of Unary Operators

Operator Name Description Example

+

Unary Plus

Represents the positive value of the operand.

+a

-

Unary Minus

Negates the value of the operand.

-a

++

Increment

Increases the value of the operand by 1.

++a (pre-increment)
a++ (post-increment)

--

Decrement

Decreases the value of the operand by 1.

--a (pre-decrement)
a-- (post-decrement)

!

Logical NOT

Inverts the truth value of the operand (true ↔ false).

!a

~

Bitwise NOT

Inverts the bits of the operand.

~a

sizeof

Sizeof

Returns the size of the operand in bytes.

sizeof(a)

&

Address-of

Returns the memory address of the operand.

&a

*

Dereference

Accesses the value stored at a memory address.

*ptr

(type)

Type Casting

Converts the operand to a specified data type.

(int) a

Detailed Explanation

  1. Unary Plus (+)

Represents the positive value of an operand. Often redundant but used for clarity.

C

int a = 5;

printf("+a = %d\n", +a); // Output: +a = 5

  1. Unary Minus (-)

Negates the value of the operand.

C

int a = 5;

printf("-a = %d\n", -a); // Output: -a = -5

  1. Increment (++)
  • Pre-increment (++a): Increments the operand before using its value.
  • Post-increment (a++): Increments the operand after using its value.

Example:

C

int a = 5;

printf("Pre-increment: %d\n", ++a); // Output: Pre-increment: 6

printf("Post-increment: %d\n", a++); // Output: Post-increment: 6

printf("After Post-increment: %d\n", a); // Output: After Post-increment: 7

  1. Decrement (--)
  • Pre-decrement (--a): Decrements the operand before using its value.
  • Post-decrement (a--): Decrements the operand after using its value.

Example:

C

int a = 5;

printf("Pre-decrement: %d\n", --a); // Output: Pre-decrement: 4

printf("Post-decrement: %d\n", a--); // Output: Post-decrement: 4

printf("After Post-decrement: %d\n", a); // Output: After Post-decrement: 3

  1. Logical NOT (!)

Inverts the truth value of the operand.

C

int a = 0;

printf("!a = %d\n", !a); // Output: !a = 1 (true)

  1. Bitwise NOT (~)

Flips all bits of the operand.

C

int a = 5; // Binary: 00000101

printf("~a = %d\n", ~a); // Output: ~a = -6 (Two's complement)

  1. Sizeof

Returns the size of the operand in bytes.

C

int a = 5;

printf("Size of a = %zu bytes\n", sizeof(a)); // Output: Size of a = 4 bytes

  1. Address-of (&)

Returns the memory address of the operand.

C

int a = 5;

printf("Address of a = %p\n", &a); // Output: Address of a = (memory address)

  1. Dereference (*)

Accesses the value at the memory address pointed to by a pointer.

C

int a = 5;

int *ptr = &a;

printf("Value at ptr = %d\n", *ptr); // Output: Value at ptr = 5

  1. Type Casting ((type))

Converts the operand to a specified data type.

C

float a = 5.5;

int b = (int)a; // Type casting to int

printf("b = %d\n", b); // Output: b = 5

Examples

Using Multiple Unary Operators

C

int a = 5, b = 10;

printf("Result = %d\n", -(++a + --b)); // Pre-increment and pre-decrement

Logical and Bitwise NOT

C

int x = 0, y = 5;

printf("!x = %d\n", !x); // Logical NOT

printf("~y = %d\n", ~y); // Bitwise NOT

Applications of Unary Operators

  1. Increment/Decrement:
    • Efficient for loops and counters.

C

for (int i = 0; i < 10; ++i) {

    printf("%d ", i);

}

  1. Pointer Operations:
    • Dereference pointers and manipulate memory addresses.

C

int a = 10, *ptr = &a;

printf("Value: %d\n", *ptr);

  1. Type Casting:
    • Convert data types in mixed arithmetic operations.

C

int x = 10;

float y = (float)x / 3;

  1. Memory Management:
    • Use sizeof for dynamic memory allocation.

C

int *arr = malloc(10 * sizeof(int));

Precautions

  1. Understand the distinction between pre- and post-increment/decrement to avoid unintended behavior.
  2. Avoid excessive use of unary operators in complex expressions as it may reduce readability.
  3. Ensure proper use of pointers and dereferencing to prevent segmentation faults.

Unary operators enhance code efficiency and play a vital role in arithmetic, logical, and memory-related tasks in C programming.