Unary Operator in C | GATE Notes (2024)

These are the type of operators that act upon just a single operand for producing a new value. All the unary operators have equal precedence, and their associativity is from right to left. When we combine the unary operator with an operand, we get the unary expression.

In this article, we will dig deeper into the Unary Operator in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

  • Types Of Unary Operator In C
  • Functions Of Unary Operator In C
  • Practice Problems On Unary Operator In C
  • Faqs

Types of Unary Operator in C

There are following types of unary operators found in the C language:

  • unary minus (-)
  • unary plus (+)
  • decrement (- -)
  • increment (++)
  • NOT (!)
  • sizeof ()
  • Address of operator (&)

Functions of Unary Operator in C

Here is how we use all the unary operators:

Unary Minus

This operator changes the sign of any given argument. It means that a positive number will become negative, and a negative number will become positive.

But the unary minus is different from that of the subtractor operator. It is because we require two operands for subtraction.

int p = 20;

int q = -p; // q = -20

Unary Plus

This operator changes the sign of any negative argument. It means that a negative number will become positive, and a positive number will also become positive.

But the unary minus is different from that of the subtractor operator. It is because we require two operands for subtraction.

int p = -20;

int q = +p; // q = 20

NOT (!)

We use this operator for reversing the logical state of the available operand. It means that the logical NOT operator will make an available condition to be false if it is already true.

In simple words,

  • If p is true, then !p will be false.
  • If p is false, then !p will be true.

Increment

We use this operator to increment the overall value of any given variable by a value of 1. We can perform increment using two major ways:

  • Prefix increment
  • Postfix Increment

Prefix Increment

The operator in this method precedes the given operand (Example, ++p). Thus, the value of the operand gets altered before we finally use it.

For instance,

int p = 1;

int q = ++p; // q = 2

Postfix Increment

The operator in this method follows the given operand (Example, p++). Thus, the value of the available operand gets altered after we use it.

For instance,

int p = 1;

int q = p++; // q = 1

int r = p; // r = 2

Decrement

We use this operator to decrement the overall value of any given variable by a value of 1. We can perform decrement using two major ways:

Prefix Decrement

The operator in this method precedes the given operand (Example, –p). Thus, the value of the operand gets altered before we finally use it.

For instance,

int p = 1;

int q = –p; // q = 0

Postfix Decrement

The operator in this method follows the given operand (Example, p–). Thus, the value of the available operand gets altered after we use it.

For instance,

int p = 1;

int q = p–; // q = 1

int r = p; // r = 0

Address of Operator (&)

This type of operator provides the user with the address of any variable. The address of operator is used to return the address (memory address) of any variable. The addresses that are returned using this operator are called pointers. It is because they point towards the variable present in the memory.

For instance,

& gives an address on variable n

int a;

int *ptr;

ptr = &a; // address of a is copied to the location ptr.

Sizeof ()

The function of the size of operator is to return the original size of the available operand in terms of bytes. This type of operator always precedes the given operand. Here, the operand can either be an expression or it can also be a cast.

For instance,

#include <iostream>

using namespace std;

int main()

{

float n = 0;

cout << “size of n: ” << sizeof(n);

return 1;

}

The output obtained here would be –

size of n: 4

Practice Problems on Unary Operator in C

1. Which of these is a unary operator in the C language?

1. !

2. sizeof

3. ~

4. &&

A. 1, 2

B. 1, 3

C. 2, 4

D. 1, 2, 3

Answer – D) 1, 2, 3

2. What is the function of the ++ operator in the C programming language?

A. It is a unary operator

B. The operand may come after or before an operator

C. It increases the value of the available variable

D. All of the above

Answer – D) All of the above

FAQs

Q1

What is the difference between the unary plus and the unary minus?

The unary minus operator changes the sign of any negative argument. It means that a negative number will become positive, and a positive number will also become positive. The unary plus operator changes the sign of any given argument. It means that a positive number will become negative, and a negative number will become positive.

Q2

What is the difference between prefix and postfix increment/ decrement?

The operator in the prefix increment/ decrement method precedes the given operand (Example, ++p and –p). Thus, the value of the operand gets altered before we finally use it. The operator in the postfix increment/ decrement method follows the given operand (Example, p++, p–). Thus, the value of the available operand gets altered after we use it.

Keep learning and stay tuned to get the latest updates onGATE Examalong withGATE Eligibility Criteria,GATE 2023,GATE Admit Card,GATE Syllabus for CSE (Computer Science Engineering),GATE CSE Notes,GATE CSE Question Paper, and more.

Also Explore,

  • Decision Control Statement in C
  • Factorial Program in C Using Recursion
  • Function in C
  • Function Pointer in C
  • Loop Control Statements in C
  • Recursion In C
  • Recursive Function in C
  • Type Casting in C
Unary Operator in C | GATE Notes (2024)

FAQs

What are unary operators in C short note? ›

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.

How do you solve an unary operator? ›

To fix the 'bash unary operator expected' , ensure all variables in your comparisons are assigned and not empty. This error typically occurs when you're comparing variables in a Bash script and one of the variables is unassigned or empty. In this example, we've declared a variable var and assigned it an empty string.

Is ++ a unary operator? ›

Unary Increment (++)

In this type, the Unary Operator is denoted by the '++' sign. It increases the value by 1 to the operand and then stores the value to the variable. It works for both Prefix and Postfix positions.

How many operates are needed for unary operators? ›

In mathematics, a unary operation is an operation with only one operand, i.e. a single input. This is in contrast to binary operations, which use two operands.

How does an unary operator work? ›

The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean. The increment/decrement operators can be applied before (prefix) or after (postfix) the operand.

What is an example of a unary operation? ›

An operation that has only one input. Example: the square root function. There are many more: factorial, sine, cosine, etc. are all unary operations.

How to use unary operator in C? ›

Example 1:
  1. #include <stdio. h>
  2. #include <conio. h>
  3. int main ()
  4. {
  5. int a = 5; // positive value of a.
  6. int b = -(a); // use unary minus operator to change the value.
  7. int n1 = 20;
  8. int n2 = -30;

How do you use unary? ›

Use unary plus operator: The program uses the unary plus operator + to perform a positive operation on num. The result of the operation is stored in the result variable of type int. Use unary minus operator: The program uses the unary minus operator – to perform a negative operation on num.

How to use unary minus operator in C? ›

1. Unary Minus
  1. Unary Minus. The minus operator ( – ) changes the sign of its argument. A positive number becomes negative, and a negative number becomes positive. int a = 10; ...
  2. Unary Minus. The minus operator ( – ) changes the sign of its argument. A positive number becomes negative, and a negative number becomes positive.
Dec 26, 2023

Is unary operator in C? ›

These are the type of operators that act upon just a single operand for producing a new value. All the unary operators have equal precedence, and their associativity is from right to left.

How to solve and operator? ›

In contrast, the and operator evaluates the operand on the right only if the first operand is true. In this case, the final result depends on the right operand's truth value. If it's true, then the whole expression is true. Otherwise, the expression is false.

Which symbol is used in unary operator? ›

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.

Is Void an unary operator? ›

Of all the unary operators, void offers the best semantic, because it clearly signals that the return value of the function invocation should be discarded. void function () { console. log("Executed!"); }(); // Logs "Executed!"

How to overload unary operator in C++? ›

To overload a unary operator in C++, you define a member function with the return type of your requirement. Then comes the "operator" keyword, followed by the sign of the operator. The function takes no arguments and returns a value.

Which unary operators can be overloaded? ›

You can overload the following unary operators on user-defined types:
  • ! (logical NOT)
  • & (address-of)
  • ~ (complement)
  • * (pointer dereference)
  • + (unary plus)
  • - (unary negation)
  • ++ (prefix increment) or (postfix increment)
  • -- (prefix decrement) or (postfix decrement)
Jul 10, 2022

Which are the unary operators? ›

The unary operators are as follows:
  • Indirection operator ( * )
  • Address-of operator ( & )
  • Unary plus operator ( + )
  • Unary negation operator ( - )
  • Logical negation operator ( ! )
  • One's complement operator ( ~ )
  • Prefix increment operator ( ++ )
  • Prefix decrement operator ( -- )
Nov 30, 2021

What is the unary and binary operator in C? ›

The unary assignment operators are the increment ( ++ ) and decrement ( -- ) operators; the binary assignment operators are the simple-assignment operator ( = ) and the compound-assignment operators. Each compound-assignment operator is a combination of another binary operator with the simple-assignment operator.

What is binary and unary in C? ›

Unary operators are those that work on single operand e.g. increment operator( ++) or the decrement operator( - - ). int a =b++; or something like int C =d- -; Binary operators are the one that operate on two operands e.g. '+', ' -', ' *', '/'.

What is unary operator overloading in C? ›

Unary operator overloading in C++ is polymorphism in which we overload an operator to perform a similar operation with the class objects. We do operator overloading to give operators a user-defined meaning, which is utilized for user-defined data type (object of a class) operations.

Top Articles
Latest Posts
Article information

Author: Virgilio Hermann JD

Last Updated:

Views: 6238

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Virgilio Hermann JD

Birthday: 1997-12-21

Address: 6946 Schoen Cove, Sipesshire, MO 55944

Phone: +3763365785260

Job: Accounting Engineer

Hobby: Web surfing, Rafting, Dowsing, Stand-up comedy, Ghost hunting, Swimming, Amateur radio

Introduction: My name is Virgilio Hermann JD, I am a fine, gifted, beautiful, encouraging, kind, talented, zealous person who loves writing and wants to share my knowledge and understanding with you.