- 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 Assignment Operators
![]() Share with a Friend |
C Programming - C Assignment Operators
C Assignment Operators
Assignment operators in C are used to assign values to variables. In addition to the basic assignment operator (=), C provides a variety of compound assignment operators that combine arithmetic or bitwise operations with assignment.
List of Assignment Operators
| Operator | Description | Example | Equivalent To | Result |
|---|---|---|---|---|
|
= |
Simple Assignment |
a = b |
a = b |
Assigns b to a |
|
+= |
Add and Assign |
a += b |
a = a + b |
Adds b to a |
|
-= |
Subtract and Assign |
a -= b |
a = a - b |
Subtracts b from a |
|
*= |
Multiply and Assign |
a *= b |
a = a * b |
Multiplies a by b |
|
/= |
Divide and Assign |
a /= b |
a = a / b |
Divides a by b |
|
%= |
Modulus and Assign |
a %= b |
a = a % b |
Stores remainder of a / b |
|
&= |
Bitwise AND and Assign |
a &= b |
a = a & b |
Performs bitwise AND |
|
` |
Bitwise OR and Assign |
=` |
`a |
= b` |
|
^= |
Bitwise XOR and Assign |
a ^= b |
a = a ^ b |
Performs bitwise XOR |
|
<<= |
Left Shift and Assign |
a <<= b |
a = a << b |
Shifts a left by b bits |
|
>>= |
Right Shift and Assign |
a >>= b |
a = a >> b |
Shifts a right by b bits |
Examples of Assignment Operators
- Basic Assignment (=)
C
int a;
a = 10; // Assigns 10 to variable a
printf("a = %d\n", a); // Output: a = 10
- Compound Assignment Operators
Add and Assign (+=)
C
int a = 5;
a += 3; // Equivalent to a = a + 3
printf("a = %d\n", a); // Output: a = 8
Subtract and Assign (-=)
C
int a = 10;
a -= 4; // Equivalent to a = a - 4
printf("a = %d\n", a); // Output: a = 6
Multiply and Assign (*=)
C
int a = 7;
a *= 2; // Equivalent to a = a * 2
printf("a = %d\n", a); // Output: a = 14
Divide and Assign (/=)
C
int a = 20;
a /= 4; // Equivalent to a = a / 4
printf("a = %d\n", a); // Output: a = 5
Modulus and Assign (%=)
C
int a = 15;
a %= 4; // Equivalent to a = a % 4
printf("a = %d\n", a); // Output: a = 3
Bitwise AND and Assign (&=)
C
int a = 12; // Binary: 1100
a &= 10; // Binary: 1010
// Result: 1000 (8 in decimal)
printf("a = %d\n", a); // Output: a = 8
Bitwise OR and Assign (|=)
C
int a = 12; // Binary: 1100
a |= 10; // Binary: 1010
// Result: 1110 (14 in decimal)
printf("a = %d\n", a); // Output: a = 14
Bitwise XOR and Assign (^=)
C
int a = 12; // Binary: 1100
a ^= 10; // Binary: 1010
// Result: 0110 (6 in decimal)
printf("a = %d\n", a); // Output: a = 6
Left Shift and Assign (<<=)
C
int a = 3; // Binary: 0011
a <<= 2; // Shift left by 2: 1100 (12 in decimal)
printf("a = %d\n", a); // Output: a = 12
Right Shift and Assign (>>=)
C
int a = 12; // Binary: 1100
a >>= 2; // Shift right by 2: 0011 (3 in decimal)
printf("a = %d\n", a); // Output: a = 3
Precedence of Assignment Operators
In the operator precedence hierarchy, assignment operators have lower precedence than arithmetic, relational, and logical operators but higher precedence than the comma operator.
Example:
C
int a = 5, b = 10;
int result = a + b * 2; // Multiplication (*) is evaluated first
printf("result = %d\n", result); // Output: result = 25
Applications of Assignment Operators
- Efficient Code:
- Compound operators reduce redundancy and make code concise.
C
x = x + 10; // Can be written as x += 10;
- Bitwise Operations:
- Useful for low-level programming like hardware interfacing.
C
int flags = 0xF0;
flags &= 0x0F; // Clear upper 4 bits
- Shifting Bits:
- Optimizing multiplication or division by powers of 2.
C
int x = 5;
x <<= 3; // Multiply x by 8 (2^3)
- Iterative Computations:
- Using assignment operators in loops.
C
for (int i = 1; i <= n; i++) {
sum += i; // Add i to sum
}
Tips for Using Assignment Operators
- Always initialize variables before using assignment operators to avoid undefined behavior.
- Understand operator precedence to prevent unexpected results in complex expressions.
- Use compound operators for better readability and efficiency in code.
Assignment operators are foundational in C programming, enabling clear and concise handling of variables during computation.
