Unary operators work on a single operand. C programming language supports the following unary operators:
- Unary minus (-)
- Increment (++)
- Decrement (–)
- NOT (!)
- Address Operator (&)
- Sizeof() operator
1. Unary Minus (-) Operator Example
The unary minus operator is used to change the sign of an operand. It changes a positive operand to negative and a negative operand to positive. For example:
int num = 10; //positive number int num2 = -num; //value of num2 is -10
Difference between minus(-) unary operator and minus(-) arithmetic operator:
The minus (-) unary operator and minus(-) arithmetic operator look same, however they are completely different. The unary – operator works on a single operand while the arithmetic – operator works on two operands. For example:
int num = 10, num2 = 5, num3 = 100, sub; //this is minus(-) arithmetic operator //working on two operands num and num2 sub = num - num2; //this is minus (-) unary operator //working on a single operand int inverseNum3 = -num3; //value of inverseNum3 is -100
Example of Unary minus(-) Operator:
#include <stdio.h> int main () { int num = 5; // We are using minus unary operator to find additive // inverse number. The sum of number and its additive // inverse is zero. The difference is only the sign. int inverseNum = -num; printf ("Original number: %d \n", num); printf ("Opposite of given number: %d \n", inverseNum); return 0; }
Output:
2. Increment (++) Operator Example
Increment operator is used to increase the value of an operand by 1. There are two types of increment operators:
1. Prefix increment Operator (for example: ++num)
2. Postfix increment Operator (for example: num++)
The difference between prefix and postfix increment operators is that prefix increment increase the value of an operand instantly. However the postfix increment operator increase the value after it is used.
In this example, we have two variables num
and num2
, initialized with the same value. This is to show you the difference between prefix and postfix increment. The ++num
(pre) printed the increased value, however the num2++
(post) did not print the increased value. This is because postfix increment operator increase the value after it is used, you can clearly see when we printed the num2
value in the next statement, it printed the increased value.
#include <stdio.h> int main () { //two integer variables with the same values int num = 100, num2 = 100; //pre increment increments the value instantly printf("Pre-increment variable num: %d\n", ++num); //post increment operator increase the value at the end of //the statement, before executing next statement printf("Post-increment variable num2: %d\n", num2++); //displaying num2 value after the post increment printf("Value of num2 after post increment: %d", num2); return 0; }
Output:
3. Decrement (–) Operator Example
Decrement operator decreases the value of an operand by 1. Similar to ++ operator, decrement operator also has two types:
1. Prefix decrement Operator Decreases the value by 1 instantly, for example: –num.
2. Postfix decrement Operator Decreases the value of an operand by 1 after it is used, for example: num–.
#include <stdio.h> int main () { //two integer variables with the same values int num = 100, num2 = 100; //prefix decrement operator decreases value before the operand is used printf("Value of num after prefix decrement: %d\n",--num); //postfix decrement operator decreases the value after it is used printf("Value of num2 after postfix decrement: %d\n", num2--); //displaying num2 value after the postfix decrement printf("Value of num2 after it is used in previous statement: %d", num2); return 0; }
Output:
Value of num after prefix decrement: 99 Value of num2 after postfix decrement: 100 Value of num2 after it is used in previous statement: 99
4. NOT (!) Operator Example
NOT unary operator is used to reverse the logical state of an operand. If the value of an operand is true then ! operator will make it false and if the value is false then it will make it true.
If the logical state of variable bool is true, then !bool is false If the logical state of variable bool is false, then !bool is true
Let’s see an example: In this example, we have a variable mathMarks
, which represents the marks in math subject. We are writing a C program to print message "Congratulations! Admission granted"
, if student marks in math subject is not less than 60 else we are printing a message "Sorry! Admission not granted."
.
#include <stdio.h> int main () { int mathMarks=65; //if marks in math subject is not less than 60 then //prints the message "admission granted" if(!(mathMarks<60)){ printf("Congratulations! Admission granted."); } else{ printf("Sorry! Admission not granted."); } return 0; }
5. AddressOf operator(&) Operator Example
The AddressOf & operator is used to get the address of a variable. This operator is represented by & (ampersand) symbol.
int num=100; //int variable int *ptr; //pointer to store int variable address ptr = # // address of variable num is stored in pointer ptr.
Example:
#include <stdio.h> int main () { // int variable int num = 100; //int pointer variable to store the address of //an int variable int *ptr; // use addressof (&) operator to get the address ptr = # printf ("Value of variable num is: %d", num); printf ("\nAddress of variable num is: %p", ptr); //value of num using pointer, *ptr gives the value //stored at the address, represented by ptr printf ("\nValue of num using pointer: %d\n", *ptr); return 0; }
Output:
6. sizeof() Operator Example
As the name suggests, this operator is used to get the size of a data item in bytes. For example, size of int is 4 bytes so if you use this operator on an int operator, it will return 4.
The Correct format specifier for sizeof() operator is %zu, you can also use %lu but it is not portable.
#include <stdio.h> int main () { char ch; //char variable int i; //int variable float f; //float variable double d; //double variable // Using sizeof() operator to find the size of various data types printf ("Size of the char data type: %zu", sizeof(ch)); printf ("\nSize of the int data type: %zu", sizeof(i)); printf ("\nSize of the float data type: %zu", sizeof(f)); printf ("\nSize of the double data type: %zu", sizeof(d)); return 0; }
Output: