Updated April 6, 2023
Introduction to Unary Operator in C
Unary Operator in C is used to produce a new value by acting upon a single operand. All unary operators are having equal precedence from right side to left side associativity. Unary minus(-), unary plus(+), prefix increment(++a) and decrement(–a), postfix increment(a++) and decrement(a–), Logical negation(!), address operator(&), indirection operator(*), cast operator and sizeof() operator comes under “Unary operator”.
Types of Unary Operators
Given below are the types of unary operators:
1. Unary minus(-)
2. Unary plus(+)
3. Increment(++)
- Pre increment(++variable)
- Post increment(variable++)
4. Decrement(–)
- Pre decrement(–variable)
- Post decrement(variable–)
5. Logical Negation(!)
6. Address Operator(&)
7. sizeof() Operator
How does Unary Operators work in C?
Unary Operator in C works based on which type of operator we are applied on a variable, according to that it will perform its corresponding operation.
1. Unary minus(-)
Unary minus changes the sign of the any argument. It will change positive number becomes negative and negative number becomes positive.
Syntax:
int variable1= value;
int variable2= -value //value becomes negative
Code:
//used to include basice c library files
#include <stdio.h>
//main method for run the C application
intmain()
{
//declaring variables
int a, unaryMinus;
//Asking user to enter any number
printf("Please enter any number \n");
//store the enter number in the int variable
scanf("%d",&a);
//unary minus operation performed, negative number becomes positive and positive number becomes negative
unaryMinus=-(a);
//displaying output
printf("Unary minus operation of %d is = %d ",a, unaryMinus);
return 0;
}
Output:
2. Unary plus(+)
Unary plus changes the sign of the any negative argument. It will change negative number becomes positive and positive number becomes positive.
Syntax:
int variable1= -value;
int variable2= +value //negative value becomes positive
Code:
//used to include basice c library files
#include <stdio.h>
//main method for run the C application
intmain()
{
//declaring variables
int a, unaryPlus;
//Asking user to enter any number
printf("Please enter any number \n");
//store the enter number in the int variable
scanf("%d",&a);
//unary plus operation performed, negative number becomes positive and positive number becomes positive only
unaryPlus=+(a);
//displaying output
printf("Unary plus operation of %d is =%d ",a, unaryPlus);
return 0;
}
Output:
3. Increment(++)
a. Pre increment(++variable)
It will increment variable value by 1 before assigning the value to the variable.
Syntax:
intvar=11;
int out=++var; //out becomes 12
Code:
//used to include basice c library files
#include <stdio.h>
//main method for run the C application
intmain()
{
//declaring variables
int a, pre_increment;
//Asking user to enter any number
printf("Please enter any number \n");
//store the enter number in the int variable
scanf("%d",&a);
//take temp variable for showing actual number in output
int temp=a;
//increment value by 1 before assigning the value
pre_increment=++a;
//displaying output
printf("Pre increment operation of %d is =%d ",temp, pre_increment);
return 0;
}
Output:
b. Post increment(variable++)
It will increment variable value by 1 after assigning the value to the variable.
Syntax:
intvar=11;
int out=var++; //out becomes 11
Code:
//used to include basice c library files
#include <stdio.h>
//main method for run the C application
intmain()
{
//declaring variables
int a, post_increment;
//Asking user to enter any number
printf("Please enter any number \n");
//store the enter number in the int variable
scanf("%d",&a);
//take temp variable for showing actual number in output
int temp=a;
//increment value by 1 after assigning the value
post_increment=a++;
//displaying output
printf("Post increment operation of %d is =%d ",temp, post_increment);
return 0;
}
Output:
4. Decrement(–)
a. Pre decrement(–variable)
It will decrement variable value by 1 before assigning the value to the variable.
Syntax:
intvar=11;
int out=--var; //out becomes 10
Code:
//used to include basice c library files
#include <stdio.h>
//main method for run the C application
intmain()
{
//declaring variables
int a, pre_decrement;
//Asking user to enter any number
printf("Please enter any number \n");
//store the enter number in the int variable
scanf("%d",&a);
//take temp variable for showing actual number in output
int temp=a;
//decrement value by 1 before assigning the value
pre_decrement=--a;
//displaying output
printf("Pre decrement operation of %d is =%d ",temp, pre_decrement);
return 0;
}
Output:
b. Post decrement(variable–)
It will decrement variable value by 1 after assigning the value to the variable.
Syntax:
intvar=11;
int out=var--; //out becomes 11
Code:
//used to include basice c library files
#include <stdio.h>
//main method for run the C application
intmain()
{
//declaring variables
int a, post_decrement;
//Asking user to enter any number
printf("Please enter any number \n");
//store the enter number in the int variable
scanf("%d",&a);
//take temp variable for showing actual number in output
int temp=a;
//decrement value by 1 before assigning the value
post_decrement=a--;
//displaying output
printf("Post decrement operation of %d is =%d ",temp, post_decrement);
return 0;
}
Output:
5. Logical Negation(!)
It is used to reverse the logical state of its operand like true become false and false becomes true vice versa.
Syntax:
bool b=false;
bool out=!b //beocmes out is true
Code:
//used to include basice c library files
#include <stdio.h>
#include <stdbool.h>
//main method for run the C application
intmain()
{
//declaring variables
bool a=false, negation;
//take temp variable for showing actual number in output
bool temp=a;
//negation operator
negation=!a;
//displaying output
//In C o means false and 1 means true
printf("Negation of %d is =%d ",temp, negation);
return 0;
}
Output:
6. Address Operator(&)
It will give the address of the variable. It is used to return the memory address of the any variable. This is also called as pointers in C.
Syntax:
int a=10;
int out=&a // Based on compiler value may varies
Code:
//used to include basice c library files
#include <stdio.h>
//main method for run the C application
intmain()
{
//declaring variables
int a=12, address;
//take temp variable for showing actual number in output
int temp=a;
//address operator assigning to the variable
address=&a;
//displaying output
printf("Address of %d is =%d ",temp, address);
return 0;
}
Output:
7. sizeof() Operator
It will return the size of the variable in bytes. It always precedes its operand.
Syntax:
int a=10;
int out=sizeof(a); //return the int size as 2 or 4 based on platform
Code:
//used to include basice c library files
#include <stdio.h>
//main method for run the C application
intmain()
{
//declaring variables
int a=12, sizeof_value;
//sizeof operator assigning to the variable
sizeof_value=sizeof(a);
//displaying output
//it is inter so size either 2 or 4
printf("size of of %d is =%d ",a, sizeof_value);
return 0;
}
Output:
Conclusion
Unary Operator in C is used to apply on single variable or operand. Unary minus, pre increment and decrement, post increment and decrement, negation, address and sizeof() operators are unary operators in C.
Recommended Articles
This is a guide to Unary Operator in C. Here we discuss the introduction to unary operators, types and how does operators work with respective examples. You may also have a look at the following articles to learn more –