- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Unary Operators in C
While most of the operators in C are binary in nature, there are a few unary operators as well. An operator is said to be unary if it takes just a single operand, unlike a binary operator which needs two operands.
Some operators in C are binary as well as unary in their usage. Examples of unary operators in C include ++, --, !, etc.
The Increment Operator in C
The increment operator (++) adds 1 to the value of its operand variable and assigns it back to the variable.
The statement a++ is equivalent to writing "a = a + 1." The "++" operator can appear before or after the operand and it will have the same effect. Hence, a++ is equivalent to ++a.
However, when the increment operator appears along with other operators in an expression, its effect is not the same. The precedence of "prefix ++" is more than "postfix ++". Hence, "b = a++;" is not the same as "b = ++a;"
In the former case, "a" is assigned to "b" before the incrementation; while in the latter case, the incrementation is performed before the assignment.
The Decrement Operator in C
The decrement operator (--) subtracts 1 from the value of its operand variable and assigns it back to the variable.
The statement "a--;" is equivalent to writing "a = a - 1;"
The "--" operator can appear before or after the operand and in either case, it will have the same effect. Hence, "a--" is equivalent to "--a".
However, when the decrement operator appears along with other operators in an expression, its effect is not the same. The precedence of "prefix --" is more than "postfix --". Hence, "b = a--" is not the same as "b = --a".
In the former case, "a" is assigned to "b" before the decrementation; while in the latter case, the decrementation is performed before the assignment.
The Unary "+" Operator in C
The "+" and "–" operators are well known as binary addition and subtraction operators. However, they can also be used in unary fashion. When used as unary, they are prefixed to the operand variable.
The "+" operator is present implicitly whenever a positive value is assigned to any numeric variable. The statement "int x = 5;" is same as "int x = +5;". The same logic applies to float and char variable too.
Example
Take a look at the following example −
#include <stdio.h> int main(){ char x = 'A'; char y = +x; float a = 1.55; float b = +a; printf ("x: %c y: %c\n", x,y); printf ("a: %f y: %f\n", a,b); return 0; }
Output
When you run this code, it will produce the following output −
x: A y: A a: 1.550000 y: 1.550000
The Unary "−" Operator in C
The "−" symbol, that normally represents the subtraction operator, also acts the unary negation operator in C. The following code shows how you can use the unary negation operator in C.
Example
In this code, the unary negation operator returns the negative value of "x" and assigns the same to another variable "y".
#include <stdio.h> int main(){ int x = 5; int y = -x; printf("x: %d y: %d\n", x, y); return 0; }
Output
Run the code and check its output −
x: 5 y: -5
The Address-of Operator (&) in C
We use the & symbol in C as the binary AND operator. However, we also use the same & symbol in unary manner as the "address-of" operator.
Example
The & operator returns the memory address of its variable operand. Take a look at the following example −
#include <stdio.h> int main(){ char x = 'A'; printf ("Address of x: %d\n", &x); return 0; }
Output
Run the code and check its output −
Address of x: 6422047
Note: The C compiler assigns a random memory address whenever a variable is declared. Hence, the result may vary every time the address is printed.
The format specifier %p is used to get a hexadecimal representation of the memory address.
char x = 'A'; printf ("Address of x: %p\n", &x);
This prints the address of "x" in hexadecimal format −
Address of x: 000000000061FE1F
The address of a variable is usually stored in a "pointer variable". The pointer variable is declared with a "*" prefix. In the code snippet below, "x" is a normal integer variable while "y" is a pointer variable.
int x = 10; int *y = &x;
The Dereference Operator (*) in C
We normally use the "*" symbol as the multiplication operator. However, it is also used as the "dereference operator" in C.
When you want to store the memory address of a variable, the variable should be declared with an asterisk (*) prefixed to it.
int x = 10; int *y = &x;
Here the variable "y" stores the address of "x", hence "y" acts as a pointer to "x". To access the value of "x" with the help of its pointer, use the dereference operator (*).
Example 1
Take a look at the following example −
#include <stdio.h> int main(){ int x = 10; int *y = &x; printf ("x: %d Address of x: %d\n", x, &x); printf("Value at x with Dereference: %d", *y); return 0; }
Output
Run the code and check its output −
x: 10 Address of x: 6422036 Value at x with Dereference: 10
Example 2
You can also assign a value to the original variable with the help of the dereference pointer −
#include <stdio.h> int main(){ int x = 10; int *y = &x; printf("x: %d Address of x %d\n", x, &x); *y = 20; printf("x: %d with Dereference: %d", x, *y); return 0; }
Output
Run the code and check its output −
x: 10 Address of x: 6422036 x: 20 with dereference: 20
The Logical NOT Operator (!) in C
The logical NOT operator (!) in C negates the value of a Boolean operand. True becomes False and False becomes True. The logical NOT operator (!) is a unary operator.
Example 1
The following example shows the usage of logical operators in C −
#include <stdio.h> int main(){ int a = 0; int b = 20; if (!(a && b)){ printf("Line 1 - Condition is true\n" ); } return 0; }
Output
Line 1 - Condition is true
Example 2
The following C code employs the NOT operator in a while loop −
#include <stdio.h> int main(){ int i = 0; while (!(i > 5)){ printf("i = %d\n", i); i++; } return 0; }
Output
In this code, the while loop continues to iterate till the expression "!(i > 5)" becomes False, which will be when the value of "i" becomes more than 5.
i = 0 i = 1 i = 2 i = 3 i = 4 i = 5
The 1's Complement Operator (~) in C
The 1's complement operator (~) in C is a unary operator, needing just one operand. It has the effect of "flipping" the bits, which means the 1's are replaced by 0's and vice versa in the binary representation of any number.
a | ~a |
---|---|
0 | 1 |
1 | 0 |
Assuming that the int variable "a" has the value 60 (equivalent to 0011 1100 in binary), the "~a" operation results in -61 in 2’s complement form, as per the bitwise right-shift of its corresponding bits.
~ 0011 1100 = 1100 0011
The binary number "1100 0011" corresponds to -61 in decimal.
Example
Take a look at this example code −
#include <stdio.h> int main(){ int a = 60; /* 60 = 0011 1100 */ int c = 0; c = ~a; /* -61 = 1100 0011 */ printf("Value of c is %d \n", c); return 0; }
Output
When you run this code, it will produce the following output −
Value of c is -61