- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- 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 - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Unary Operators
![]() Share with a Friend |
C Programming - C Unary Operators
C Unary Operators
Unary operators in C operate on a single operand to perform various operations like incrementing, negating, or checking the size of a value. These operators are crucial for concise and efficient programming.
List of Unary Operators
| Operator | Name | Description | Example |
|---|---|---|---|
|
+ |
Unary Plus |
Represents the positive value of the operand. |
+a |
|
- |
Unary Minus |
Negates the value of the operand. |
-a |
|
++ |
Increment |
Increases the value of the operand by 1. |
++a (pre-increment) |
|
-- |
Decrement |
Decreases the value of the operand by 1. |
--a (pre-decrement) |
|
! |
Logical NOT |
Inverts the truth value of the operand (true ↔ false). |
!a |
|
~ |
Bitwise NOT |
Inverts the bits of the operand. |
~a |
|
sizeof |
Sizeof |
Returns the size of the operand in bytes. |
sizeof(a) |
|
& |
Address-of |
Returns the memory address of the operand. |
&a |
|
* |
Dereference |
Accesses the value stored at a memory address. |
*ptr |
|
(type) |
Type Casting |
Converts the operand to a specified data type. |
(int) a |
Detailed Explanation
- Unary Plus (+)
Represents the positive value of an operand. Often redundant but used for clarity.
C
int a = 5;
printf("+a = %d\n", +a); // Output: +a = 5
- Unary Minus (-)
Negates the value of the operand.
C
int a = 5;
printf("-a = %d\n", -a); // Output: -a = -5
- Increment (++)
- Pre-increment (++a): Increments the operand before using its value.
- Post-increment (a++): Increments the operand after using its value.
Example:
C
int a = 5;
printf("Pre-increment: %d\n", ++a); // Output: Pre-increment: 6
printf("Post-increment: %d\n", a++); // Output: Post-increment: 6
printf("After Post-increment: %d\n", a); // Output: After Post-increment: 7
- Decrement (--)
- Pre-decrement (--a): Decrements the operand before using its value.
- Post-decrement (a--): Decrements the operand after using its value.
Example:
C
int a = 5;
printf("Pre-decrement: %d\n", --a); // Output: Pre-decrement: 4
printf("Post-decrement: %d\n", a--); // Output: Post-decrement: 4
printf("After Post-decrement: %d\n", a); // Output: After Post-decrement: 3
- Logical NOT (!)
Inverts the truth value of the operand.
C
int a = 0;
printf("!a = %d\n", !a); // Output: !a = 1 (true)
- Bitwise NOT (~)
Flips all bits of the operand.
C
int a = 5; // Binary: 00000101
printf("~a = %d\n", ~a); // Output: ~a = -6 (Two's complement)
- Sizeof
Returns the size of the operand in bytes.
C
int a = 5;
printf("Size of a = %zu bytes\n", sizeof(a)); // Output: Size of a = 4 bytes
- Address-of (&)
Returns the memory address of the operand.
C
int a = 5;
printf("Address of a = %p\n", &a); // Output: Address of a = (memory address)
- Dereference (*)
Accesses the value at the memory address pointed to by a pointer.
C
int a = 5;
int *ptr = &a;
printf("Value at ptr = %d\n", *ptr); // Output: Value at ptr = 5
- Type Casting ((type))
Converts the operand to a specified data type.
C
float a = 5.5;
int b = (int)a; // Type casting to int
printf("b = %d\n", b); // Output: b = 5
Examples
Using Multiple Unary Operators
C
int a = 5, b = 10;
printf("Result = %d\n", -(++a + --b)); // Pre-increment and pre-decrement
Logical and Bitwise NOT
C
int x = 0, y = 5;
printf("!x = %d\n", !x); // Logical NOT
printf("~y = %d\n", ~y); // Bitwise NOT
Applications of Unary Operators
- Increment/Decrement:
- Efficient for loops and counters.
C
for (int i = 0; i < 10; ++i) {
printf("%d ", i);
}
- Pointer Operations:
- Dereference pointers and manipulate memory addresses.
C
int a = 10, *ptr = &a;
printf("Value: %d\n", *ptr);
- Type Casting:
- Convert data types in mixed arithmetic operations.
C
int x = 10;
float y = (float)x / 3;
- Memory Management:
- Use sizeof for dynamic memory allocation.
C
int *arr = malloc(10 * sizeof(int));
Precautions
- Understand the distinction between pre- and post-increment/decrement to avoid unintended behavior.
- Avoid excessive use of unary operators in complex expressions as it may reduce readability.
- Ensure proper use of pointers and dereferencing to prevent segmentation faults.
Unary operators enhance code efficiency and play a vital role in arithmetic, logical, and memory-related tasks in C programming.
