w3resource

C Programming: Assignment Operators

Assignment Operators in C Programming

Overview

In C programming, assignment operators are used to assign values to variables. The simple assignment operator is =. C also supports shorthand assignment operators that combine an operation with assignment, making the code more concise.

Key Topics:

Simple Assignment Operator

=: Simple assignment, assigns a value to a variable.

Example:

Here the simple assignment operator = is used to assign a value to a variable.

Code:

#include <stdio.h>
int main() {
    int a = 5;
    // Prefix increment: 'a' is incremented first, then used
    printf("Prefix increment: %d\n", ++a); // Output will be 6
    return 0;
}

Output:

a = 5

Explanation:

  • The variable a is assigned the value 5 using the simple assignment operator =.
  • The value is then printed.

Shorthand Addition Assignment (+=)

+=: Adds a value to the variable.

Example:

The += operator is used to add a value to the variable and assign the result back to it.

Code:

#include <stdio.h>
int main() {
    int a = 5;  // Assign 5 to 'a'
    a += 1;     // Same as a = a + 1
    printf("a = %d\n", a);  // Output: a = 6
    return 0;
}

Output:

a = 6

Explanation:

  • Initially, a is assigned 5.
  • The shorthand operator += is used to add 1 to a (a = a + 1).
  • The value of a becomes 6 and is printed.

Shorthand Subtraction Assignment (-=)

-=: Subtracts a value from the variable.

  • When to Use: Use this when you need the variable's value to be decremented before using it in an expression or operation.
  • Why to Use: It ensures that the updated (decremented) value is used in the current expression immediately.

Example:

The -= operator subtracts a value from the variable and assigns the result back to it.

Code:

#include <stdio.h>
int main() {
    int a = 5;  // Assign 5 to 'a'
    a -= 2;     // Same as a = a - 2
    printf("a = %d\n", a);  // Output: a = 3
    return 0;
}

Output:

a = 3

Explanation:

  • The initial value of a is 5.
  • The shorthand operator -= subtracts 2 from a, making it 3.

Shorthand Multiplication Assignment (*=)

*=: Multiplies the variable by a value.

Example:

The *= operator multiplies the variable by a value and assigns the result back to it.

Code:

#include <stdio.h>
int main() {
    int a = 5;  // Assign 5 to 'a'
    a *= 3;     // Same as a = a * 3
    printf("a = %d\n", a);  // Output: a = 15
    return 0;
}

Output:

a = 15

Explanation:

  • The initial value of a is 5.
  • The shorthand operator *= multiplies a by 3, resulting in 15.

Shorthand Division Assignment (/=)

/=: Divides the variable by a value.

Example:

The /= operator divides the variable by a value and assigns the result back to it.

Code:

#include <stdio.h>
int main() {
    int a = 10;  // Assign 10 to 'a'
    a /= 2;      // Same as a = a / 2
    printf("a = %d\n", a);  // Output: a = 5
    return 0;
}

Output:

a = 5

Explanation:

  • Initially, a is assigned the value 10.
  • The shorthand operator /= divides a by 2, making it 5.

Shorthand Modulus Assignment (%=)

%=: Assigns the remainder after dividing the variable by a value.

Example:

The %= operator calculates the remainder of division and assigns the result back to the variable.

Code:

#include <stdio.h>
int main() {
    int a = 10;  // Assign 10 to 'a'
    a %= 3;      // Same as a = a % 3
    printf("a = %d\n", a);  // Output: a = 1
    return 0;
}

Output:

a = 1

Explanation:

  • Initially, a is assigned 10.
  • The shorthand operator %= calculates the remainder when a is divided by 3, giving 1.


Become a Patron!

Follow us on Facebook and Twitter for latest update.