The Wayback Machine - https://web.archive.org/web/20240917155454/https://www.geeksforgeeks.org/unary-operators-cc/
Open In App

Unary operators in C

Last Updated : 26 Dec, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Unary operators are the operators that perform operations on a single operand to produce a new value.

Types of unary operators

Types of unary operators are mentioned below:

  1. Unary minus ( – )
  2. Increment ( ++ )
  3. Decrement ( — )
  4. NOT ( ! )
  5. Addressof operator ( & )
  6. sizeof()

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;
int b = -a; // b = -10

Unary minus is different from the subtraction operator, as subtraction requires two operands.

Below is the implementation of the unary minus (-) operator:

C




#include <stdio.h>
 
int main()
{
    // declaring a positive integer
    int positiveInteger = 100;
    // using - sign to make the value of positive integers
    // to negative
    int negativeInteger = -positiveInteger;
 
    printf("Positive Integer = %d\n", positiveInteger);
    printf("Negative Integer = %d", negativeInteger);
    return 0;
}


Output

Positive Integer = 100
Negative Integer = -100

2. Increment

The increment operator ( ++ ) is used to increment the value of the variable by 1. The increment can be done in two ways:

2.1 prefix increment

In this method, the operator precedes the operand (e.g., ++a). The value of the operand will be altered before it is used.

Example:

  int a = 1;
int b = ++a; // b = 2

2.2 postfix increment

In this method, the operator follows the operand (e.g., a++). The value operand will be altered after it is used.

Example:

 int a = 1;
int b = a++; // b = 1
int c = a; // c = 2

Below is the implementation of the increment ( ++ ):

C




// C program to illustrate increment
 
#include <stdio.h>
 
int main()
{
    int a = 5;
    int b = 5;
    printf("Pre-Incrementing a = %d\n", ++a);
    printf("Post-Incrementing b = %d", b++);
    return 0;
}


3. Decrement

The decrement operator ( — ) is used to decrement the value of the variable by 1. The decrement can be done in two ways:

3.1 prefix decrement

In this method, the operator precedes the operand (e.g., – -a). The value of the operand will be altered before it is used.

Example:

  int a = 1;
int b = --a; // b = 0

3.2 postfix decrement

In this method, the operator follows the operand (e.g., a- -). The value of the operand will be altered after it is used.

Example:

 int a = 1;
int b = a--; // b = 1
int c = a; // c = 0

Below is the implementation of the decrement ( — ):

C




// C program to illustrate decrement
 
#include <stdio.h>
 
int main()
{
    int a = 5;
    int b = 5;
    printf("Pre-Decrementing a = %d\n", --a);
    printf("Post-Decrementing b = %d", b--);
    return 0;
}


4. NOT ( ! )

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

Example:

   If x is true, then !x is false
If x is false, then !x is true

Below is the implementation of the NOT (!) operator:

C




// C program to illustrate NOT operator
#include <stdio.h>
 
int main()
{
 
    int a = 10;
    int b = 5;
 
    if (!(a > b))
        printf("b is greater than a\n");
    else
        printf("a is greater than b");
 
    return 0;
}


Output

a is greater than b

5. Addressof operator ( & )

The addressof operator ( & ) gives an address of a variable. It is used to return the memory address of a variable. These addresses returned by the address-of operator are known as pointers because they “point” to the variable in memory.

Example:

    & gives an address on variable n
int a;
int *ptr;
ptr = &a; // address of a is copied to the location ptr.

Below is the implementation of the Addressof operator(&):

C




// C program to demonstrate the use of 'address-of(&)'
// operator
#include <stdio.h>
 
int main()
{
 
    int a = 20;
    printf("Address of a = %p", &a;);
 
    return 0;
}


Output

Address of a = 0x7fff78c3f37c

6. sizeof()

This operator returns the size of its operand, in bytes. The sizeof() operator always precedes its operand. The operand is an expression, or it may be a cast.

Note: The `sizeof()` operator in C++ is machine dependent. For example, the size of an ‘int’ in C++ may be 4 bytes in a 32-bit machine but it may be 8 bytes in a 64-bit machine.

Below is the implementation of sizeof() operator:

C




// C program to illustrate the sizeof operator
#include <stdio.h>
 
int main()
{
    // printing the size of double and int using sizeof
    printf("Size of double: %d\n", sizeof(double));
    printf("Size of int: %d\n", sizeof(int));
 
    return 0;
}


Output

Size of double: 8
Size of int: 4



Previous Article
Next Article

Similar Reads

Arithmetic Operators in C
Arithmetic Operators are the type of operators in C that are used to perform mathematical operations in a C program. They can be used in programs to define expressions and mathematical formulas. What are C Arithmetic Operators? The C arithmetic operators are the symbols that are used to perform mathematical operations on operands. There are a total
7 min read
C | Operators | Question 2
#include &lt;stdio.h&gt; int main() {     int i = 1, 2, 3;          printf("%d", i);          return 0; } (A) 1 (B) 3 (C) Garbage value (D) Compile time error Answer: (D) Explanation: Comma acts as a separator here. The compiler creates an integer variable and initializes it with 1. The compiler fails to create integer variable 2 because 2 is not a
1 min read
C | Operators | Question 3
#include &lt;stdio.h&gt; int main() {     int i = (1, 2, 3);          printf("%d", i);          return 0; } (A) 1 (B) 3 (C) Garbage value (D) Compile time error Answer: (B) Explanation: The bracket operator has higher precedence than assignment operator. The expression within bracket operator is evaluated from left to right but it is always the res
1 min read
C | Operators | Question 4
#include &lt;stdio.h&gt; int main() {     int i;        i = 1, 2, 3;     printf("%d", i);          return 0; } (A) 1 (B) 3 (C) Garbage value (D) Compile time error Answer: (A) Explanation: Comma acts as an operator. The assignment operator has higher precedence than comma operator. So, the expression is considered as (i = 1), 2, 3 and 1 gets assign
1 min read
C | Operators | Question 5
C/C++ Code #include int main() { int i = 3; printf(\"%d\", (++i)++); return 0; } What is the output of the above program? (A)3 (B)4 (C)5 (D)Compile-time error Answer: (D)Explanation: In C, prefix and postfix operators need l-value to perform operation and return r-value. The expression (++i)++ when executed increments the value of variable i(i is a
1 min read
C | Operators | Question 6
What is the output of below program? #include &lt;stdio.h&gt; int foo(int* a, int* b) { int sum = *a + *b; *b = *a; return *a = sum - *b; } int main() { int i = 0, j = 1, k = 2, l; l = i++ || foo(&amp;j, &amp;k); printf(&quot;%d %d %d %d&quot;, i, j, k, l); return 0; } (A) 1 2 1 1 (B) 1 1 2 1 (C) 1 2 2 1 (D) 1 2 2 2 Answer: (A) Explanation: The con
1 min read
C | Operators | Question 7
#include &amp;lt;stdio.h&amp;gt; int main() { int i = 5, j = 10, k = 15; printf(&amp;quot;%d &amp;quot;, sizeof(k /= i + j)); printf(&amp;quot;%d&amp;quot;, k); return 0; } Assume size of an integer as 4 bytes. What is the output of above program? (A) 4 1 (B) 4 15 (C) 2 1 (D) Compile-time error Answer: (B) Explanation: The main theme of the program
1 min read
C | Operators | Question 8
#include &lt;stdio.h&gt; int main() { //Assume sizeof character is 1 byte and sizeof integer is 4 bytes printf(&quot;%d&quot;, sizeof(printf(&quot;GeeksQuiz&quot;))); return 0; } (A) GeeksQuiz4 (B) 4GeeksQuiz (C) GeeksQuiz9 (D) 4 (E) Compile-time error Answer: (D) Explanation: An expression doesn't get evaluated inside sizeof operator. GeeksQuiz wi
1 min read
C | Operators | Question 9
Output of following program? #include &lt;stdio.h&gt; int f1() { printf (&quot;Geeks&quot;); return 1;} int f2() { printf (&quot;Quiz&quot;); return 1;} int main() { int p = f1() + f2(); return 0; } (A) GeeksQuiz (B) QuizGeeks (C) Compiler Dependent (D) Compiler Error Answer: (C) Explanation: The operator ‘+’ doesn't have a standard defined order o
1 min read
C | Operators | Question 10
What is the output of following program? #include &lt;stdio.h&gt; int main() { int a = 1; int b = 1; int c = a || --b; int d = a-- &amp;&amp; --b; printf(&quot;a = %d, b = %d, c = %d, d = %d&quot;, a, b, c, d); return 0; } (A) a = 0, b = 1, c = 1, d = 0 (B) a = 0, b = 0, c = 1, d = 0 (C) a = 1, b = 1, c = 1, d = 1 (D) a = 0, b = 0, c = 0, d = 0 Ans
2 min read
C | Operators | Question 11
#include &lt;stdio.h&gt; int main() { int a = 10, b = 20, c = 30; if (c &gt; b &gt; a) printf(&quot;TRUE&quot;); else printf(&quot;FALSE&quot;); return 0; } (A) TRUE (B) FALSE (C) Compiler Error (D) Output is compiler dependent Answer: (B) Explanation: Let us consider the condition inside the if statement. Since there are two greater than (>) opera
1 min read
C | Operators | Question 12
#include&lt;stdio.h&gt; int main() { char *s[] = { &quot;knowledge&quot;,&quot;is&quot;,&quot;power&quot;}; char **p; p = s; printf(&quot;%s &quot;, ++*p); printf(&quot;%s &quot;, *p++); printf(&quot;%s &quot;, ++*p); return 0; } (A) is power (B) nowledge nowledge s (C) is ower (D) nowledge knowledge is Answer: (B) Explanation: Let us consider the
1 min read
C | Operators | Question 13
C/C++ Code #include int main(void) { int a = 1; int b = 0; b = a++ + a++; printf(\"%d %d\",a,b); return 0; } (A) 3 6 (B) Compiler Dependent (C) 3 4 (D) 3 3 Answer: (B) Explanation: It is compiler dependent as different compilers takes different bytes for integers. Quiz of this Question Please comment below if you find anything wrong in the above po
1 min read
C | Operators | Question 27
Which of the following is not a logical operator? (A)&amp;&amp; (B)! (C)|| (D)| Answer: (D)Explanation: &amp;&amp;- Logical AND !- Logical NOT ||- Logical OR |- Bitwise OR(used in bitwise manipulations) Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
C | Operators | Question 15
Predict the output of following program. Assume that the characters are represented using ASCII Values. #include &lt;stdio.h&gt; #define VAL 32 int main() { char arr[] = &quot;geeksquiz&quot;; *(arr + 0) &amp;= ~VAL; *(arr + 5) &amp;= ~VAL; printf(&quot;%s&quot;, arr); return 0; } (A) GeeksQuiz (B) geeksQuiz (C) Geeksquiz (D) geeksquiz (E) Garbage
1 min read
C | Operators | Question 16
Predict the output of the below program: #include &lt;stdio.h&gt; int main() { printf(&quot;%d&quot;, 1 &lt;&lt; 2 + 3 &lt;&lt; 4); return 0; } (A) 112 (B) 52 (C) 512 (D) 0 Answer: (C) Explanation: The main logic behind the program is the precedence and associativity of the operators. The addition(+) operator has higher precedence than shift(&lt;
1 min read
C | Operators | Question 17
Which of the following can have different meaning in different contexts? (A) &amp; (B) * (C) Both of the above (D) There are no such operators in C Answer: (C) Explanation: '&amp;' can be used to get address of a variable and can also be used to do bitwise and operation. Similarly '*' can be used to get value at an address and can also be used to m
1 min read
C | Operators | Question 18
In C, two integers can be swapped using minimum (A) 0 extra variable (B) 1 extra variable (C) 2 extra variable (D) 4 extra variable Answer: (A) Explanation: We can swap two variables without any extra variable using bitwise XOR operator '^'. Let X and Y be two variables to be swapped. Following steps swap X and Y. X = X ^ Y; Y = X ^ Y; X = X ^ Y; S
1 min read
C | Operators | Question 26
What does the following statement do? x = x | 1 &lt;&lt; n; (A) Sets x as 2n (B) Sets (n+1)th bit of x (C) Toggles (n+1)th bit of x (D) Unsets (n+1)th bit of x Answer: (B) Explanation: Let n be 3, the value of expression 1 &lt;&lt; n would be 8 (In binary 00001000). If we do bitwise or of binary 00001000 with any number x would set its (n+1)th bit
1 min read
C | Operators | Question 27
Predict the output of following C program C/C++ Code #include int main() { int i = 0; do { printf(\"GeeqsQuiz \"); i = i++; } while (i (A) GeeqsQuiz GeeqsQuiz GeeqsQuiz GeeqsQuiz GeeqsQuiz (B) Infinite time GeeksQuiz (C) Undefined Behavior (D) None of the above Answer: (C) Explanation: The below statement causes undefined behavior. i = i++; Quiz of
1 min read
C | Operators | Question 21
Assume that the size of an integer is 4 bytes, predict the output of following program. #include &lt;stdio.h&gt; int main() { int i = 12; int j = sizeof(i++); printf(&quot;%d %d&quot;, i, j); return 0; } (A) 12 4 (B) 13 4 (C) Compiler Error (D) 0 4 Answer: (A) Explanation: The expressions written inside sizeof are not evaluated, so i++ is not perfo
1 min read
C | Operators | Question 22
#include&lt;stdio.h&gt; int main() { int a = 2,b = 5; a = a^b; b = b^a; printf(&quot;%d %d&quot;,a,b); return 0; } (A) 5 2 (B) 2 5 (C) 7 7 (D) 7 2 Answer: (D) Explanation: ^ is bitwise xor operator. a = 2 (10) b = 5 (101) a = a^b (10 ^ 101) = 7(111) b = a^b (111 ^ 101) = 2(10)Quiz of this Question
1 min read
C | Operators | Question 23
Predict the output of following program? C/C++ Code #include int main() { int x = 10; int y = 20; x += (y += 10); printf("%d %d", x, y); return 0; } (A) 40 20 (B) 40 30 (C) 30 30 (D) 30 40 Answer: (B) Explanation: The main statement in question is "x += y += 10". Since there are two += operators in the statement, associativity comes into the pictur
1 min read
C | Operators | Question 24
Output of following C code will be? C/C++ Code #include int main() { int x = 10; int y = (x++, x++, x++); printf("%d %d\n", x, y); return 0; } (A) 13 12 (B) 13 13 (C) 10 10 (D) Compiler Dependent Answer: (A) Explanation: Option A is correct. Quiz of this Question Please comment below if you find anything wrong in the above post
1 min read
C | Operators | Question 26
Output of following C code will be? C/C++ Code #include int main() { int y = 0; int x = (y != 0); printf("%d", x); return 0; } (A) 0 (B) 1 (C) A bog negative Number (D) Compiler Error Answer: (A) Explanation: 0 is the output of the following C code. Quiz of this Question Please comment below if you find anything wrong in the above post
1 min read
C | Operators | Question 27
Output of following C code will be? C/C++ Code #include int main() { int a = 0; int b; a = (a == (a == 1)); printf("%d", a); return 0; } (A) 0 (B) 1 (C) Big negative number (D) -1 Answer: (B) Explanation: We need to figure out value of "(a == (a == 1))" (a == 1) returns false as a is initialized as 0. So in outer bracket, false is compared with a.
1 min read
C | Operators | Question 27
Output of following C code will be? C/C++ Code #include #include int top = 0; char fun1() { char a[] = {'a', 'b', 'c', '(', 'd'}; return a[top++]; } int main() { char b[10]; char ch2; int i = 0; while ((ch2 = fun1()) != '(') { b[i++] = ch2; } b[i] = '\0'; // Add null-terminating character to mark the end of the string printf("%s", b); return 0; } (
1 min read
Largest in array without using conditionals / bitwise / ternary operators
Define a function int max(int a[], int n) which returns the largest integer from an integer array consisting of n elements (without using conditionals / bitwise / ternary operators / library function to find largest Examples: Input : arr[] = {16, 14, 15, 17, 9} Output : 17 Input : arr[] = {10, 13, 11, 13} Output : 13 The idea is similar to Maximum
4 min read
Go Operators
Operators are the foundation of any programming language. Thus the functionality of the Go language is incomplete without the use of operators. Operators allow us to perform different kinds of operations on operands. In the Go language, operators Can be categorized based on their different functionality: Arithmetic OperatorsRelational OperatorsLogi
9 min read
Java Arithmetic Operators with Examples
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types: Arithmetic Operator
6 min read
Practice Tags :