Unary Operators in C: Everything You Need to Know
This article will discuss about different unaru operators like Increment operator, Decrement operator,Negation operator, Logical NOT operator,Bitwise NOT operator, Indirection operator, Operator Precedence.
The C programming language has unary operators that work on a single operand, resulting in a new value. These operators can perform various functions such as negation, increment, and decrement. These unary operators are useful for modifying variables, performing arithmetic operations, or controlling the execution flow. Do go through our tutorial blog on Operators in C Programming.
Also, Explore Data Types in C.
Multiple unary operators in C perform various operations. So, without further ado, let’s discuss each type in detail.
We will be covering the following sections today:
- What are Unary Operators in C?
- Increment operator (++)
- Decrement operator (–)
- Negation operator (-)
- Logical NOT operator (!)
- Bitwise NOT operator (~)
- Address operator (&)
- Indirection operator (*)
- Operator Precedence
What are Unary Operators in C?
Unary operators work on a single operand, requiring only one operand. These operators perform various functions on the operand and produce a new value. Some of the most common unary operators in C include:
- Increment operator (++) – adds 1 to the value of the operand
- Decrement operator (–) – subtracts 1 from the value of the operand
- Negation operator (-) – reverses the sign of the operand
- Logical NOT operator (!) – reverses the Boolean value of the operand
- Bitwise NOT operator (~) – reverses the bits of the operand
- Address operator (&) – returns the memory address of the operand
- Indirection operator (*) – returns the value at the memory address pointed to by the operand
Unary operators can be used for various purposes, including arithmetic operations, memory manipulation, and program control flow. Let’s discuss each of them with examples below:
Best-suited C / C++ courses for you
Learn C / C++ with these high-rated online courses
Increment operator (++)
The increment operator (++) in C is a unary operator that adds 1 to the value of its operand. The operator can be used with variables of numeric data types such as int, float, double, etc.
Here’s an example:
#include <stdio.h>
int main() {
int num = 5;
printf("The value of num before increment: %d\n", num);
num++; printf("The value of num after increment: %d\n", num);
return 0;
}
Output
In this example, we declared an integer variable “num” and initialized it with 5. Then, we printed the value of “num” using printf() function. After that, we incremented the value of “num” by 1 using the increment operator (++) and printed the updated value of “num”. As we can see in the output, the value of “num” is increased by 1 after the increment operator is applied.
Decrement operator (- -)
The decrement operator (–) in C is a unary operator that subtracts 1 from the value of its operand. The operator can be used with variables of numeric data types such as int, float, double, etc.
Here’s an example:
#include <stdio.h> int main() {
int num = 10; printf("The value of num before decrement: %d\n", num);
num--; printf("The value of num after decrement: %d\n", num); return 0; }
Output
In this example, we declared an integer variable “num” and initialized it with 10. Then, we printed the value of “num” using printf() function. After that, we decremented the value of “num” by 1 using the decrement operator (–) and printed the updated value of “num”. As we can see in the output, the value of “num” is decreased by 1 after the decrement operator is applied.
Check out: C Programming Online Courses & Certifications
Check out: Understanding the Difference Between Structure and Union in C
Negation operator (-)
The negation operator (-) in C is a unary operator that reverses the sign of its operand. The operator can be used with variables of numeric data types such as int, float, double, etc.
Here’s an example:
#include <stdio.h>
int main() {
int num = 5; printf("The value of num before negation: %d\n", num);
num = -num; printf("The value of num after negation: %d\n", num); return 0;
}
Output
In this example, we declared an integer variable “num” and initialized it with 5. Then, we printed the value of “num” using printf() function. After that, we applied the negation operator (-) on “num” and assigned the result back to “num”. Finally, we printed the updated value of “num”. As we can see in the output, the value of “num” is negated (i.e., reversed the sign) after the negation operator is applied.
You can also explore: Understanding Data Structures in C: Types And Operations
Logical NOT operator (!)
The logical NOT operator (!) in C is a unary operator that reverses the Boolean value of its operand. The operator can be used with expressions that evaluate a Boolean value (i.e., true or false).
Here are some examples:
Example 1: Using logical NOT operator with a Boolean variable
#include <stdio.h>
#include <stdbool.h>
int main() { bool is_sunny = false;
printf("The value of is_sunny before NOT operator: %d\n", is_sunny);
is_sunny = !is_sunny;
printf("The value of is_sunny after NOT operator: %d\n", is_sunny);
return 0;
}
Output
In this example, we declared a Boolean variable “is_sunny” and initialized it with the value false. Then, we printed the value of “is_sunny” using printf() function. After that, we applied the logical NOT operator (!) on “is_sunny” and assigned the result back to “is_sunny”. Finally, we printed the updated value of “is_sunny”. As we can see in the output, the value of “is_sunny” is reversed (i.e., true) after the logical NOT operator is applied.
Example 2: Using logical NOT operator with an expression
#include <stdio.h>
int main() {
int num = 10;
printf("The value of num before NOT operator: %d\n", num);
if(!num) {
printf("The value of num is zero.\n");
} else {
printf("The value of num is non-zero.\n");
} return 0;
}
Output:
In this example, we declared an integer variable “num” and initialized it with 10. Then, we printed the value of “num” using printf() function. After that, we used the logical NOT operator (!) with “num” in an if statement. The if statement checks if the value of “num” is zero. Since the value of “num” is non-zero, the else block is executed and “The value of num is non-zero.” is printed.
Learn more about Data Types in C Programming With Examples
Bitwise NOT operator (~)
The bitwise NOT operator (~) in C is a unary operator that performs a bitwise complement of its operand. The operator flips all the bits of its operand, changing 0s to 1s and 1s to 0s.
Here are some examples:
Example 1: Using bitwise NOT operator with an integer variable
#include <stdio.h>
int main() {
int num = 5; printf("The value of num before NOT operator: %d\n", num); num = ~num; printf("The value of num after NOT operator: %d\n", num); return 0; }
Output
The value of num before NOT operator: 5 The value of num after NOT operator: -6
In this example, we declared an integer variable “num” and initialized it with 5. Then, we printed the value of “num” using printf() function. After that, we applied the bitwise NOT operator (~) on “num” and assigned the result back to “num”. Finally, we printed the updated value of “num”. As we can see in the output, the value of “num” is changed after the bitwise NOT operator is applied.
Explanation: The value of “num” is initially stored as 00000101 in binary form. After the bitwise NOT operator is applied, the value becomes 11111010 in binary form. This is equivalent to -6 in decimal form using 2’s complement representation.
Example 2: Using bitwise NOT operator with a bit field
#include <stdio.h>
struct Flags { unsigned int a: 4; unsigned int b: 4; unsigned int c: 4; unsigned int d: 4;};
int main() { struct Flags flags = {0x0A, 0x05, 0x0F, 0x03}; printf("The value of flags.a before NOT operator: %x\n", flags.a); flags.a = ~flags.a; printf("The value of flags.a after NOT operator: %x\n", flags.a); return 0;}
Output
The value of flags.a before NOT operator: a The value of flags.a after NOT operator: 5
In this example, we declared a structure “Flags” with four 4-bit bit fields. Then, we initialized an instance of the structure “flags” with some values. After that, we printed the value of “flags.a” using printf() function. Next, we applied the bitwise NOT operator (~) on “flags.a” and assigned the result back to “flags.a”. Finally, we printed the updated value of “flags.a”. As we can see in the output, the value of “flags.a” is changed after the bitwise NOT operator is applied.
Explanation: The initial value of “flags.a” is stored as 1010 in binary form, equivalent to A in hexadecimal form. After applying the bitwise NOT operator, the value becomes 111100101 in binary form, equivalent to F5 in hexadecimal form.
Address operator (&)
In C, the address operator (&) is a unary operator used to get a variable’s memory address. It returns the address of the variable in memory.
Here are some examples:
Example 1: Using the address operator with an integer variable
#include <stdio.h>
int main() { int num = 5; printf("The value of num is: %d\n", num); printf("The address of num is: %p\n", &num); return 0;}
Output
In this example, we declared an integer variable “num” and initialized it with 5. Then, we printed the value of “num” using printf() function. After that, we used the address operator (&) to get the memory address of “num” and printed it using printf() function with the format specifier %p. The memory address is represented in hexadecimal notation.
Example 2: Using the address operator with a character array
#include <stdio.h>
int main() { char str[] = "Hello, world!"; printf("The value of str is: %s\n", str); printf("The address of str is: %p\n", &str); return 0;}
Output:
The value of str is: Hello, world! The address of str is: 0x7ffc9553988a
In this example, we declared a character array “str” and initialized it with the string “Hello, world!”. Then, we printed the ” str ” value using printf() function. After that, we used the address operator (&) to get the memory address of “str” and printed it using printf() function with the format specifier %p. The memory address is represented in hexadecimal notation.
Note that the address of the first element of an array is the same as the address of the array itself. So, &str is equivalent to str.
In C, the indirection operator (*) is a unary operator that is used to access the value of the variable pointed to by a pointer. It returns the value stored at the memory address pointed to by the pointer.
Indirection operator (*)
Here are some examples:
Example 1: Using the indirection operator with an integer pointer
#include <stdio.h>
int main() {
int num = 5;
int *p = #
printf("The value of num is: %d\n", num);
printf("The value of p is: %p\n", p);
printf("The value pointed to by p is: %d\n", *p);
return 0;
}
Output:
The value of num is: 5 The value of p is: 0x7ffef0c45734 The value pointed to by p is: 5
In this example, we declared an integer variable “num” and initialized it with 5. Then, we declared an integer pointer “p” and assigned it the memory address of “num”. We printed the value of “num” using printf() function, the value of “p” using printf() function with the format specifier %p, and the value pointed to by “p” using the indirection operator (*) with printf() function.
Example 2: Using the indirection operator with a structure pointer
#include <stdio.h>
struct student {
char name[20];
int age;
};
int main() {
struct student s = {"John", 20};
struct student *p = &s;
printf("The value of s.name is: %s\n", s.name);
printf("The value of s.age is: %d\n", s.age);
printf("The value of p is: %p\n", p);
printf("The value of (*p).name is: %s\n", (*p).name);
printf("The value of (*p).age is: %d\n", (*p).age);
printf("The value of p->name is: %s\n", p->name);
printf("The value of p->age is: %d\n", p->age);
return 0;
}
Output:
The value of s.name is: John The value of s.age is: 20 The value of p is: 0x7ffd8078b5f0 The value of (*p).name is: John The value of (*p).age is: 20 The value of p->name is: John The value of p->age is: 20
In this example, we declared a structure “student” with two members “name” and “age”. Then, we declared a structure variable “s” and initialized it with the values “John” and 20. We declared a structure pointer “p” and assigned it the memory address of “s”. We printed the values of “s.name” and “s.age” using printf() function. Then, we printed the value of “p” using printf() function with the format specifier %p. We used the indirection operator (*) and the member selection operator (.) to access the values of the members of the structure pointed to by “p”. We also used the arrow operator (->) to do the same thing more shortly.
Operator Precedence
In C, the operator precedence of unary operators is higher than that of binary operators. This means that unary operators are evaluated before binary operators. Here is a table of the unary operators in order of precedence, with the highest-precedence operators at the top:
Operator | Description |
+ | Unary plus |
– | Unary minus |
! | Logical NOT |
~ | Bitwise NOT |
++ | Prefix increment |
— | Prefix decrement |
(type) | Type cast |
* | Indirection (dereference) |
& | Address-of |
sizeof | Size of a type or variable |
Endnotes
This article was helpful for you in understanding unary operators in C. We learned about the different kinds of operators and how they are implemented. We also learned about the precedence of operators. If you wish to learn more about C and solidify your basics, you can explore more about C Programming. And if you wish to apply them and want to learn more about this language, take C programming courses from the best vendors.
Contributed by Prerna Singh
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio