Assignment Operators

Introduction

Assignment operators are used to assign values to variables in C programming. The basic assignment operator (=) simply assigns the value on the right side to the variable on the left side. C also provides compound assignment operators that combine arithmetic operations with assignment, making code more concise and efficient.

These operators are fundamental to programming as they allow you to store, modify, and update values in variables throughout your program execution.

Key Concepts

Assignment: Storing a value in a variable L-value: Left-hand side of assignment (must be a variable) R-value: Right-hand side of assignment (can be value, variable, or expression) Compound Assignment: Combining arithmetic operation with assignment

Basic Assignment Operator (=)

Simple Assignment

#include <stdio.h>
int main() {
    int a, b, c;
    
    // Basic assignment
    a = 10;
    b = 20;
    c = a + b;
    
    printf("a = %d\n", a);
    printf("b = %d\n", b);
    printf("c = %d\n", c);
    
    return 0;
}

Multiple Assignments

#include <stdio.h>
int main() {
    int a, b, c;
    
    // Multiple assignment (right to left associativity)
    a = b = c = 10;
    
    printf("a = %d, b = %d, c = %d\n", a, b, c);
    
    // Chain assignment with expression
    int x, y, z;
    x = y = z = 5 + 3;
    
    printf("x = %d, y = %d, z = %d\n", x, y, z);
    
    return 0;
}

Compound Assignment Operators

Addition Assignment (+=)

#include <stdio.h>
int main() {
    int a = 10;
    
    printf("Initial value: a = %d\n", a);
    
    a += 5;  // Equivalent to: a = a + 5
    printf("After a += 5: a = %d\n", a);
    
    a += a;  // Equivalent to: a = a + a
    printf("After a += a: a = %d\n", a);
    
    // With floating point
    float x = 3.5;
    x += 2.2;
    printf("x = %.1f\n", x);
    
    return 0;
}

Subtraction Assignment (-=)

#include <stdio.h>
int main() {
    int balance = 1000;
    int withdrawal = 250;
    
    printf("Initial balance: %d\n", balance);
    
    balance -= withdrawal;  // Equivalent to: balance = balance - withdrawal
    printf("After withdrawal of %d: %d\n", withdrawal, balance);
    
    balance -= 100;
    printf("After another withdrawal: %d\n", balance);
    
    return 0;
}

Multiplication Assignment (*=)

#include <stdio.h>
int main() {
    int base = 5;
    int multiplier = 3;
    
    printf("Initial value: %d\n", base);
    
    base *= multiplier;  // Equivalent to: base = base * multiplier
    printf("After base *= %d: %d\n", multiplier, base);
    
    // Double the value
    base *= 2;
    printf("After base *= 2: %d\n", base);
    
    return 0;
}

Division Assignment (/=)

#include <stdio.h>
int main() {
    int total = 100;
    int divisor = 4;
    
    printf("Initial total: %d\n", total);
    
    total /= divisor;  // Equivalent to: total = total / divisor
    printf("After total /= %d: %d\n", divisor, total);
    
    // Floating point division
    float amount = 150.0;
    amount /= 3.0;
    printf("Amount after division: %.2f\n", amount);
    
    return 0;
}

Modulus Assignment (%=)

#include <stdio.h>
int main() {
    int number = 17;
    int mod = 5;
    
    printf("Initial number: %d\n", number);
    
    number %= mod;  // Equivalent to: number = number % mod
    printf("After number %%= %d: %d\n", mod, number);
    
    // Finding last digit
    int value = 12345;
    value %= 10;
    printf("Last digit: %d\n", value);
    
    return 0;
}

Practical Examples

Interest Calculator

#include <stdio.h>
int main() {
    float principal = 1000.0;
    float rate = 0.05;  // 5% annual interest
    int years = 3;
    
    printf("Initial principal: $%.2f\n", principal);
    
    for (int i = 1; i <= years; i++) {
        principal *= (1 + rate);  // Compound interest
        printf("After year %d: $%.2f\n", i, principal);
    }
    
    return 0;
}

Grade Point Average Calculator

#include <stdio.h>
int main() {
    float gpa = 0.0;
    int credits = 0;
    int total_credits = 0;
    float grade;
    
    // Course 1
    grade = 3.7;
    credits = 3;
    gpa += grade * credits;
    total_credits += credits;
    
    // Course 2
    grade = 4.0;
    credits = 4;
    gpa += grade * credits;
    total_credits += credits;
    
    // Course 3
    grade = 3.3;
    credits = 2;
    gpa += grade * credits;
    total_credits += credits;
    
    // Calculate final GPA
    gpa /= total_credits;
    
    printf("Total credits: %d\n", total_credits);
    printf("GPA: %.2f\n", gpa);
    
    return 0;
}

Shopping Cart Total

#include <stdio.h>
int main() {
    float total = 0.0;
    float tax_rate = 0.08;  // 8% tax
    
    // Add items to cart
    total += 25.99;  // Item 1
    total += 15.50;  // Item 2
    total += 8.75;   // Item 3
    
    printf("Subtotal: $%.2f\n", total);
    
    // Apply discount
    total *= 0.9;  // 10% discount
    printf("After 10%% discount: $%.2f\n", total);
    
    // Add tax
    total *= (1 + tax_rate);
    printf("Final total with tax: $%.2f\n", total);
    
    return 0;
}

Advanced Usage

Bitwise Assignment Operators

#include <stdio.h>
int main() {
    int a = 12;  // Binary: 1100
    int b = 10;  // Binary: 1010
    
    printf("Initial: a = %d, b = %d\n", a, b);
    
    a &= b;  // Bitwise AND assignment
    printf("After a &= b: a = %d\n", a);
    
    a = 12;  // Reset
    a |= b;  // Bitwise OR assignment
    printf("After a |= b: a = %d\n", a);
    
    a = 12;  // Reset
    a ^= b;  // Bitwise XOR assignment
    printf("After a ^= b: a = %d\n", a);
    
    return 0;
}

Left and Right Shift Assignment

#include <stdio.h>
int main() {
    int value = 8;  // Binary: 1000
    
    printf("Initial value: %d\n", value);
    
    value <<= 2;  // Left shift by 2 (multiply by 4)
    printf("After value <<= 2: %d\n", value);
    
    value >>= 1;  // Right shift by 1 (divide by 2)
    printf("After value >>= 1: %d\n", value);
    
    return 0;
}

Assignment with Different Data Types

Type Conversion in Assignment

#include <stdio.h>
int main() {
    int i;
    float f;
    double d;
    
    // Float to int (truncation)
    f = 3.14159;
    i = f;
    printf("float %.2f assigned to int: %d\n", f, i);
    
    // Int to float (promotion)
    i = 42;
    f = i;
    printf("int %d assigned to float: %.2f\n", i, f);
    
    // Double to float (possible precision loss)
    d = 3.141592653589793;
    f = d;
    printf("double %.15f assigned to float: %.6f\n", d, f);
    
    return 0;
}

Common Mistakes and Solutions

Confusing Assignment with Equality

// Wrong - assignment instead of comparison
int a = 5;
if (a = 10) {  // Assignment, not comparison!
    printf("This will always execute\n");
}

// Correct - comparison
if (a == 10) {  // Equality comparison
    printf("This will execute only if a equals 10\n");
}

Uninitialized Variables

// Wrong - using uninitialized variable
int x;
x += 5;  // Undefined behavior - x has garbage value

// Correct - initialize first
int x = 0;
x += 5;  // Now x has value 5

Modifying Constants

// Wrong - trying to modify constant
const int MAX_SIZE = 100;
MAX_SIZE += 10;  // Compilation error

// Correct - use regular variable if modification needed
int max_size = 100;
max_size += 10;  // This works

Important Points

  • Right-to-Left Associativity: Assignment operators are evaluated from right to left
  • L-value Requirement: Left side of assignment must be a modifiable l-value
  • Type Conversion: Automatic type conversion may occur during assignment
  • Efficiency: Compound operators are often more efficient than separate operations
  • Readability: Use compound operators for cleaner, more concise code
  • Constants: Cannot assign values to const variables after declaration

Summary

Assignment operators are fundamental tools for storing and modifying values in C programming. The basic assignment operator (=) stores values in variables, while compound assignment operators (+=, -=, *=, /=, %=) combine arithmetic operations with assignment for more efficient and readable code. Understanding the right-to-left associativity of assignment operators and the concept of l-values vs r-values is crucial for proper usage. Compound operators not only make code more concise but often generate more efficient machine code. Be careful to distinguish between assignment (=) and equality (==) operators, and always initialize variables before using compound assignment operators. These operators are essential for updating variables in loops, calculations, and maintaining program state.


Part of BCA Programming with C Course (UGCOA22J201)