top

Search

C Tutorial

.

UpGrad

C Tutorial

Unary Operator in C

C programming, a versatile and effective language, offers a wide range of operators that assist developers in creating efficient code. Among these operators are the unary operators, which play a significant role. 

Let's walk through the concept of unary operators, exploring their types, functionality, and unary operator example to demonstrate their usage.

Introduction

The Unary operator in C is essential to the language's syntax. They play a significant role in developing algorithms, managing data structures, and modifying the behaviour of operands. Unary operators, as the name suggests, work with a single operand. They can be a part of more complex expressions or can be used individually.

What is a Unary Operator in C?

A unary operator in C is an operator that operates on a single operand, either transforming its value or determining a characteristic of its state. From increasing or decreasing values to retrieving the memory address of variables, unary operators facilitate a broad range of functionalities that are indispensable in programming tasks.

Let's examine the analogies provided below to determine if they accurately depict the behaviour and usage of unary operators in C programming and effectively convey their purpose:

Car's odometer: Consider the analogy of a car's odometer. If you were to liken the unary increment operator to pushing the accelerator of a car, you would see the odometer (the operand) increase by one with each push (operation). Similarly, pressing the brake could be likened to the unary decrement operator, causing the speed (another operand) to decrease with each push.

Mail tracking system:  When a package changes locations, the tracking number (operand) remains the same, but the location information associated with it changes (operation). This could be likened to the 'address of' operator in C, which retrieves the memory address of a variable.

Weight scale: Another real-world parallel could be drawn with a scale for measuring weight. The weight you see (the operand) can be positive if you're standing on the scale, or it could be negated to zero as soon as you step off - a process comparable to the unary minus operator.

But beyond these examples, the applications of unary operator in C extend to far more complex and fascinating uses in coding, algorithm development, and data structure management. 

Types of Unary Operators in C

There are several unary operators in C, each with its unique functionality. Let's explore the unary operator in C symbols and their usage.

Unary Plus (+) Operator

The unary plus operator doesn't affect the value of its operand. However, it can improve code readability and explicitly indicate the positive value.

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

Unary Minus (-) Operator

The unary minus operator negates the value of its operand. If it's applied to a positive value, the value becomes negative, and vice versa.

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

Increment (++) Operator

This unary operator in C increases the value of its operand by 1.

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

Decrement (--) Operator

Conversely, the decrement operator decreases the value of its operand by 1.

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

Address of (&) Operator in C

This operator returns the memory address of its operand.

int x = 5;
printf("%p\n", &x);  // Output: Memory address of x

sizeof() Operator

The sizeof operator returns the size (in bytes) of its operand.

int x;
printf("%zu\n", sizeof(x));  // Output: Size of integer on your system, commonly 4

Dereferencing (*) Operator

The dereference operator is used to access the value pointed by a pointer.

int x = 5;
int *p = &x;
printf("%d\n", *p);  // Output: 5

Logical NOT (!) Operator

This operator inverts the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false.

int x = 0;
printf("%d\n", !x);  // Output: 1

Bitwise NOT (~) Operator

The Bitwise NOT operator in C inverts all the bits of its operand.

int x = 5;
printf("%d\n", ~x);  // Output: -6

Functions of Unary Operators in C

Unary operator in C serves various functions that enhance the efficiency of code. Each operator has a unique purpose, and here, we'll delve deeper into these functions along with examples for each.

Negation

The unary minus operator (-) is used to negate the value of its operand. If the operand is positive, applying the unary minus operator makes it negative and vice versa.

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

In this example, we use the unary minus operator to negate the positive value of a, which results in -10.

Indication of Positivity

The unary plus operator (+) doesn't change the value of the operand. However, it can be used to indicate explicitly that a value is positive. It may be used to enhance the readability of the code.

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

Even though it does not affect the output, the unary plus in the example above signifies that a is a positive number.

Incrementing and Decrementing

The increment (++) and decrement (--) unary operators in C are used to increase or decrease the value of their operand by 1, respectively.

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

int b = 10;
b--;
printf("%d\n", b);  // Output: 9

In this example, we first increase the value of a by 1 using the increment operator, which results in 11. We then decrease the value of b by 1 using the decrement operator, resulting in 9.

Accessing Memory Locations

The address of (&) operator is used to get the memory address of its operand.

int a = 10;
printf("%p\n", &a);  // Output: Memory address of a

In this example, we use the address of the operator to print the memory address where a is stored.

Size Determination

The sizeof operator is used to get the size (in bytes) of its operand or a data type.

int a;
printf("%zu\n", sizeof(a));  // Output: Size of integer on your system, commonly 4

Here, we use the sizeof operator to print the size of the integer variable a, which is usually 4 bytes on most systems.

Dereferencing Pointers

The dereference (*) operator is used to access the value pointed by a pointer.

int a = 10;
int *p = &a;
printf("%d\n", *p);  // Output: 10

In this example, we use the dereference operator to access the value 10 pointed to by the pointer p.

Logical and Bitwise Operations

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

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

Here, !a returns 1 because a is 0, and logically, NOT 0 is 1.

The bitwise NOT (~) operator inverts all the bits of its operand.

int a = 5;  // Binary: 101
printf("%d\n", ~a);  // Output: -6, Binary: 1010

In this example, the bitwise NOT operator inverts the binary representation of 5 (101), resulting in -6.

What is the Difference Between the Unary Plus and the Unary Minus?

The unary plus and unary minus are two different types of unary operator in C that deal with numeric values.

The unary plus (+) operator does not alter the value of the operand. Its primary use is to enhance the readability of the code by explicitly showing that a value is positive. Despite not changing the value, its usage can make the intent of the code clearer, especially when dealing with numeric expressions that involve both positive and negative numbers.

Here's a simple example:

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

In this code, we apply the unary plus operator to the variable a, but it does not change the value. The output remains 10.

On the other hand, the unary minus (-) operator negates the value of the operand. It flips the sign of the operand: if it's positive, it becomes negative; if it's negative, it becomes positive.

Here's an example:

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

In this code, the unary minus operator is applied to the variable a, turning the positive 10 into -10.

What is the Difference Between Prefix and Postfix Increment/Decrement?

The increment and decrement operators in C can be used in two forms: prefix and postfix.

The prefix increment (++x) or decrement (--x) operator changes the value of x first, then the new value is used in the expression. Conversely, the postfix increment (x++) or decrement (x--) operator uses the value of x in the expression first, and then changes the value.

Here's an example illustrating the difference:

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

In this case, we're using a prefix increment. The value of x is incremented before it's used in the printf statement. So, the output is 6.

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

Here, we're using a postfix increment. The value of x is used in the printf statement first, then it's incremented. So, the output of the first printf statement is 5, but x becomes 6 after the increment.

Remember, the choice between prefix and postfix versions should be made based on the specific requirements of your code. Understanding how they work is essential to avoid unexpected results.

Conclusion

Understanding the unary operator in C is essential to writing efficient and clean code. This guide has aimed to demystify unary operators, their types, and their usage with examples. Mastering them will greatly enhance your C programming skills and open new avenues for algorithm development.

To dive deeper into C programming and other computer science concepts, explore upGrad’s Master of Science in Data Science program, offered under the guidance of Liverpool John Moores University. They offer a wide range of programs designed to hone your skills and increase your expertise. Embrace the opportunity to learn from the best and take your career to the next level.

FAQs

1. What is a unary operator in C?
A unary operator in C is an operator that operates on a single operand to produce a new value. Examples include the unary minus (-), unary plus (+), increment (++), decrement (--), logical NOT (!), bitwise NOT (~), address of (&), dereference (*), and sizeof operators.

2. How do unary operators work in C?
Unary operators in C work on a single operand. They perform a variety of operations like negation (with the unary minus), indicating a positive value (with unary plus), incrementing or decrementing a value (with ++ and --), inverting the logical state of a value (with !), inverting the bits of a value (with ~), returning the memory address of a value (with &), accessing the value pointed by a pointer (with *), or returning the size of a data type (with sizeof).

3. What is the difference between unary plus and unary minus?
The unary plus operator simply denotes that its operand is positive without changing the value. On the other hand, the unary minus operator negates the value of its operand, making positive values negative and vice versa.

4. What is the difference between prefix and postfix increment/decrement operators?
The prefix increment/decrement operator (++/-- variable) first increments or decrements the value of the variable and then returns it. Conversely, the postfix increment/decrement operator (variable ++/--) returns the value of the variable first and then performs the increment or decrement operation.

Leave a Reply

Your email address will not be published. Required fields are marked *