Codeforwin

Assignment and shorthand assignment operator in C

Quick links.

  • Shorthand assignment

Assignment operator is used to assign value to a variable (memory location). There is a single assignment operator = in C. It evaluates expression on right side of = symbol and assigns evaluated value to left side the variable.

For example consider the below assignment table.

The RHS of assignment operator must be a constant, expression or variable. Whereas LHS must be a variable (valid memory location).

Shorthand assignment operator

C supports a short variant of assignment operator called compound assignment or shorthand assignment. Shorthand assignment operator combines one of the arithmetic or bitwise operators with assignment operator.

For example, consider following C statements.

The above expression a = a + 2 is equivalent to a += 2 .

Similarly, there are many shorthand assignment operators. Below is a list of shorthand assignment operators in C.

  • Shorthand Operators

Assignment Operators are used for assigning values to the variables in the program. There are two types of Assignment operators used. The first one being the Simple Assignment Operator and the other one being Shorthand Operators or Compound Assignment Operators. Expressions or Values can be assigned to the variable using Shorthand Assignment Operators. C++ supports both types of Assignment Operators. Let us look at the Shorthand Assignment Operators and their different types in this article below.

Shorthand Operators

  • Shorthand Assignment Operators combines one of the arithmetic or bitwise operators with the assignment operator. They are also called as compound assignment operators.
  • A Shorthand Assignment Operator is a shorter way of expressing something that is already available in the programming statements.

C++ language offers its users a few special shorthand’s that simplifies the coding of certain expressions. It also saves coding time by using these Shorthand Operators in the program. Shorthand Assignment Operators have the lowest order of precedence i.e., they are the last to be evaluated in an expression.

Shorthand Assignment Operators follow the following Syntax –

variable_name operator = expression/value ;

which is equivalent to :

variable_name = variable_name operator expression/value ;  

Note:  The variable data type and the value assigned to it should match. Or else the compiler will give an error while running the program.

Browse all the Topics Under Operator and Expressions: Operators

  • Arithmetic Operators
  • Assignment Operator s
  • Unary Operator
  • Increment and Decrement Operators
  • Relation Operator
  • Logical Operators

Types of Shorthand Assignment Operators

Shorthand Assignment Operators are Binary Operators which require 2 values or 1 variable and another value/expression. One on the left and the other on the right side.

The following are types of Shorthand Assignment Operators

This type of Operator is a combination of Arithmetic Operator ‘+’ and Assignment Operator ‘=’. This operator adds the variable on the left with the value on the right and then assigns/saves the result to the variable on the left.

These types of Shorthand Operators are used with a combination of Subtraction Arithmetic Operator ‘-‘ and Assignment operator ‘=’.

These Shorthand Operators use a combination of Multiplication type of Arithmetic Operator ‘*’ and Simple Assignment operator ‘=‘. The variable to the left is multiplied with the value or expression to the right side and then the result is stored into the variable defined to the left.

Here, a combination of Division Arithmetic operator ‘/’ and Simple Assignment Operator ‘=’ is seen. The variable is divided by the value first and then the value is stored to the left, in the variable.

This type of operator uses Modulus Operator ‘%’ and an Assignment Operator ‘=’ in its syntax. It returns the Remainder of the two operands and stores the value in the variable to the left side of the statement.

Apart from the Assignment Operators, the Shorthand Operators are also used to define the Bitwise Operators.

Some of them are:

FAQs on Shorthand Operators

Q1. Which of the following is a valid assignment operator?

  • All of the Above

Answer. Option D

Q2. What does the *= assignment operator do?

  • Multiplies the value twice
  • Multiplies variable with value once
  • Used as exponent like 2*3 = 8
  • None of the Above

Answer. Option B

Q3. Do the given two equations mean the same?

count += 1 ;

count = count + 1 ;

Answer. Option A

Q4. What is the final output of variable ‘a’ in the below program?

Answer. Option C.

Customize your course in 30 seconds

Which class are you in.

tutor

Operator and Expressions: Operators

  • Assignment Operators
  • Unary Operators
  • Increment & Decrement Operators
  • Relational Operators

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Download the App

Google Play

  • C++ Data Types
  • C++ Input/Output
  • C++ Pointers
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management

Assignment Operators In C++

In C++, the assignment operator forms the backbone of many algorithms and computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language that is used to assign some value to the variables in C++ or in other words, it is used to store some kind of information.

The right-hand side value will be assigned to the variable on the left-hand side. The variable and the value should be of the same data type.

The value can be a literal or another variable of the same data type.

Compound Assignment Operators

In C++, the assignment operator can be combined into a single operator with some other operators to perform a combination of two operations in one single statement. These operators are called Compound Assignment Operators. There are 10 compound assignment operators in C++:

  • Addition Assignment Operator ( += )
  • Subtraction Assignment Operator ( -= )
  • Multiplication Assignment Operator ( *= )
  • Division Assignment Operator ( /= )
  • Modulus Assignment Operator ( %= )
  • Bitwise AND Assignment Operator ( &= )
  • Bitwise OR Assignment Operator ( |= )
  • Bitwise XOR Assignment Operator ( ^= )
  • Left Shift Assignment Operator ( <<= )
  • Right Shift Assignment Operator ( >>= )

Lets see each of them in detail.

1. Addition Assignment Operator (+=)

In C++, the addition assignment operator (+=) combines the addition operation with the variable assignment allowing you to increment the value of variable by a specified expression in a concise and efficient way.

This above expression is equivalent to the expression:

2. Subtraction Assignment Operator (-=)

The subtraction assignment operator (-=) in C++ enables you to update the value of the variable by subtracting another value from it. This operator is especially useful when you need to perform subtraction and store the result back in the same variable.

3. Multiplication Assignment Operator (*=)

In C++, the multiplication assignment operator (*=) is used to update the value of the variable by multiplying it with another value.

4. Division Assignment Operator (/=)

The division assignment operator divides the variable on the left by the value on the right and assigns the result to the variable on the left.

5. Modulus Assignment Operator (%=)

The modulus assignment operator calculates the remainder when the variable on the left is divided by the value or variable on the right and assigns the result to the variable on the left.

6. Bitwise AND Assignment Operator (&=)

This operator performs a bitwise AND between the variable on the left and the value on the right and assigns the result to the variable on the left.

7. Bitwise OR Assignment Operator (|=)

The bitwise OR assignment operator performs a bitwise OR between the variable on the left and the value or variable on the right and assigns the result to the variable on the left.

8. Bitwise XOR Assignment Operator (^=)

The bitwise XOR assignment operator performs a bitwise XOR between the variable on the left and the value or variable on the right and assigns the result to the variable on the left.

9. Left Shift Assignment Operator (<<=)

The left shift assignment operator shifts the bits of the variable on the left to left by the number of positions specified on the right and assigns the result to the variable on the left.

10. Right Shift Assignment Operator (>>=)

The right shift assignment operator shifts the bits of the variable on the left to the right by a number of positions specified on the right and assigns the result to the variable on the left.

Also, it is important to note that all of the above operators can be overloaded for custom operations with user-defined data types to perform the operations we want.

Similar Reads

  • Assignment Operators In C++ In C++, the assignment operator forms the backbone of many algorithms and computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language that is used to assign 7 min read
  • Move Assignment Operator in C++ 11 In C++ programming, we have a feature called the move assignment operator, which was introduced in C++11. It helps us handle objects more efficiently, especially when it comes to managing resources like memory. In this article, we will discuss move assignment operators, when they are useful and call 5 min read
  • Assignment Operators in Programming Assignment operators in programming are symbols used to assign values to variables. They offer shorthand notations for performing arithmetic operations and updating variable values in a single step. These operators are fundamental in most programming languages and help streamline code while improvin 7 min read
  • Augmented Assignment Operators in Python An assignment operator is an operator that is used to assign some value to a variable. Like normally in Python, we write "a = 5" to assign value 5 to variable 'a'. Augmented assignment operators have a special role to play in Python programming. It basically combines the functioning of the arithmeti 4 min read
  • Solidity - Assignment Operators Solidity is a high-level, statically-typed programming language for Ethereum smart contracts. Python, JavaScript, and C++ impact it. Solidity has several variable and value assignment operators. Solidity supports the following types of operators: Simple Assignment Operator.Arithmetic Assignment Oper 5 min read
  • C++ Assignment Operator Overloading Prerequisite: Operator Overloading The assignment operator,"=", is the operator used for Assignment. It copies the right value into the left value. Assignment Operators are predefined to operate only on built-in Data types. Assignment operator overloading is binary operator overloading.Overloading a 4 min read
  • Self assignment check in assignment operator In C++, assignment operator should be overloaded with self assignment check. For example, consider the following class Array and overloaded assignment operator function without self assignment check. // A sample class class Array { private: int *ptr; int size; public: Array& operator = (const Ar 2 min read
  • Copy Constructor vs Assignment Operator in C++ Copy constructor and Assignment operator are similar as they are both used to initialize one object using another object. But, there are some basic differences between them: Copy constructor Assignment operator It is called when a new object is created from an existing object, as a copy of the exist 2 min read
  • Bitwise Operators in C++ There are various Operators present in C++. Every Operator has a particular symbol as well as an Operation to perform. We have various categories of operators in C++. Arithmetic OperatorsRelational OperatorsLogical OperatorsAssignment OperatorsBitwise OperatorsIn this article, we will learn about th 6 min read
  • C++ Arithmetic Operators Arithmetic Operators in C++ are used to perform arithmetic or mathematical operations on the operands. For example, ‘+’ is used for addition, ‘-‘ is used for subtraction, ‘*’ is used for multiplication, etc. In simple terms, arithmetic operators are used to perform arithmetic operations on variables 3 min read
  • Operators in C++ An operator is a symbol that operates on a value to perform specific mathematical or logical computations. They form the foundation of any programming language. In C++, we have built-in operators to provide the required functionality. An operator operates the operands. For example,  int c = a + b;He 13 min read
  • Address Operator & in C The Address Operator in C is a special unary operator that returns the address of a variable. It is denoted as the Ampersand Symbol ( & ). This operator returns an integer value which is the address of its operand in the memory. We can use the address operator (&) with any kind of variables, 3 min read
  • Default Assignment Operator and References in C++ We have discussed assignment operator overloading for dynamically allocated resources here. In this article, we discussed that when we don't write our own assignment operator, the compiler creates an assignment operator itself that does shallow copy and thus causes problems. The difference between s 2 min read
  • Vector assign() in C++ STL In C++, the vector assign() is a built-in method used to assign the new values to the given vector by replacing old ones. It also modifies the size of the vector according to the given number of elements. Let’s take a look at an example that shows the how to use this function. [GFGTABS] C++ #include 4 min read
  • When should we write our own assignment operator in C++? The answer is same as Copy Constructor. If a class doesn't contain pointers, then there is no need to write assignment operator and copy constructor. The compiler creates a default copy constructor and assignment operators for every class. The compiler created copy constructor and assignment operato 3 min read
  • Conversion Operators in C++ In C++, the programmer abstracts real-world objects using classes as concrete types. Sometimes, it is required to convert one concrete type to another concrete type or primitive type implicitly. Conversion operators play an important role in such situations. It is similar to the operator overloading 4 min read
  • C++ Increment and Decrement Operators Prerequisite: Operators in C++ What is a C++ increment Operator? The C++ increment operator is a unary operator. The symbol used to represent the increment operator is (++). The increment operator increases the value stored by the variable by 1. This operator is used for Numeric values only. There a 4 min read
  • Unary Operators In C++ Unary operators in C++ are those operators that work on a single value (operand). They perform operations like changing a value's sign, incrementing or decrementing it by one, or obtaining its address. Examples include ++ for increment, -- for decrement, + for positive values, - for negative values, 8 min read
  • set operator= in C++ STL The ‘=’ is an operator in C++ STL which copies (or moves) a set to another set and set::operator= is the corresponding operator function. There are three versions of this function: The first version takes reference of an set as an argument and copies it to an set. Syntax: ums1.operator=(set &set 2 min read
  • Geeks Premier League
  • Geeks Premier League 2023

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

BTech Geeks

C programming += – Assignment Operators in C Programming

C programming +=: Assignment Operators in C is used to assign a value to a variable. “=” is called simple assignment operator of C, it assigns values from right side operands(R value) to left side operand (L value).

The general syntax of assignment operator is:

For Example

  • value = 1234;
  • value = 4/2;

C also supports compound assignment operators or shorthand assignment operators. These operators are combination of arithmetic and assignment operators. They first perform arithematic operation between left and right operand and then assign the result to left operand. It first add 5 to A, and then assign result to A.

  • A+=5; is equivalent to A = A+5; It first add 5 to A, and then assign result to A.

Below is the list of Shorthand Assignment Operators Supported in C.

C Program to show use of Shorthand Assignment Operators

Assignment Operators in C Programming

shorthand arithmetic assignment operators

  • Learn C Programming

Introduction

  •  Historical Development of C
  •  Importance of C
  •  Basic Structure of C Program
  •  Executing a C Program
  •  Compiler, Assembler, and Interpreter

Problem Solving Using Computer

  • Problem Analysis
  •  Types of Errors
  •  Debugging, Testing, and Program Documentation
  •  Setting up C Programming Environment

C Fundamentals

  •  Character Set
  •  Identifiers and Keywords
  •  Data Types
  •  Constants and Variables
  •  Variable/Constant Declaration
  •  Pre-processor Directive
  •  Symbolic Constant

C Operators and Expressions

  • Operators and Types
  •  Arithmetic Operators
  •  Relational Operators
  •  Logical Operators
  •  Assignment Operators
  •  Conditional Operator
  •  Increment and Decrement Operators
  •  Bitwise Operators
  •  Special Operators
  •  Precedence and Associativity

C Input and Output

  • Input and Output functions
  • Unformatted I/O
  •  Formatted I/O

C Decision-making Statements

  • Decision-making Statements in C
  •  Nested if else
  •  Else-if ladder
  •  Switch Case
  •  Loop Control Statements in C

C Functions

  •  Get Started
  •  First Program

shorthand arithmetic assignment operators

Assignment Operators in C

Assignment operators are used to assigning the result of an expression to a variable. Up to now, we have used the shorthand assignment operator “=”, which assigns the result of a right-hand expression to the left-hand variable. For example, in the expression x = y + z, the sum of y and z is assigned to x.

Another form of assignment operator is variable operator_symbol= expression ; which is equivalent to variable = variable operator_symbol expression;

We have the following different types of assignment and assignment short-hand operators.

Expected Output:

IMAGES

  1. Shorthand Assignment Operator on Arithmetic operators

    shorthand arithmetic assignment operators

  2. PPT

    shorthand arithmetic assignment operators

  3. PPT

    shorthand arithmetic assignment operators

  4. การเขียนภาษาจาวา ตอนที่ 2

    shorthand arithmetic assignment operators

  5. PPT

    shorthand arithmetic assignment operators

  6. Assignment and Arithmetic expressions

    shorthand arithmetic assignment operators

VIDEO

  1. JavaScript Arithmetic Operators

  2. BUSINESS MATHEMATICS ASSIGNMENT 2 ( Arithmetic and Geometric Progression )

  3. PHP Operators Reference

  4. @5 Arithmetic operators and Assignment operators and Operator Functions

  5. C++ Chapter 2

  6. Python-Session2

COMMENTS

  1. Assignment and shorthand assignment operator in C

    Shorthand assignment operator combines one of the arithmetic or bitwise operators with assignment operator. For example, consider following C statements. int a = 5; a = a + 2; The above expression a = a + 2 is equivalent to a += 2. Similarly, there are many shorthand assignment operators. Below is a list of shorthand assignment operators in C.

  2. Assignment Operators in Programming

    Assignment operators in programming are symbols used to assign values to variables. They offer shorthand notations for performing arithmetic operations and updating variable values in a single step. These operators are fundamental in most programming languages and help streamline code while improving readability.

  3. Arithmetical Assignment Operators in C

    Understanding Arithmetical Assignment Operators. Arithmetical assignment operators are shorthand notations that perform arithmetic operations and assignments in a single step. These operators include: += (Add and Assign)-= (Subtract and Assign) *= (Multiply and Assign) /= (Divide and Assign) %= (Modulus and Assign)

  4. C Programming: Assignment Operators with Examples

    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; Shorthand Addition Assignment (+=) Shorthand Subtraction Assignment (-=)

  5. What are Shorthand Operators in C++

    Shorthand Operators are operators that combine one of the arithmetic or bitwise operators with the assignment operator. Shorthand Operators are a shorter way of expressing something that is already available in the programming statements. Expressions or Values can be assigned to the variable using Shorthand Operators.

  6. Assignment Operators in C

    1. "=": This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. Example: a = 10; b = 20; ch = 'y'; 2. "+=": This operator is combination of '+' and '=' operators.This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.

  7. Assignment Operators In C++

    Assignment operators in programming are symbols used to assign values to variables. They offer shorthand notations for performing arithmetic operations and updating variable values in a single step. These operators are fundamental in most programming languages and help streamline code while improvin

  8. C Operators

    Assignment Operators: Assignment operators are used to assign the result of an expression to a variable. Usually, '=' operator is used. There is an additional 'shorthand' assignment operators of the form. V op = exp; Here, V= variable . exp = expression and . op = a binary arithmetic operator. The Operator op= is known as the shorthand ...

  9. C programming +=

    C also supports compound assignment operators or shorthand assignment operators. These operators are combination of arithmetic and assignment operators. They first perform arithematic operation between left and right operand and then assign the result to left operand. It first add 5 to A, and then assign result to A.

  10. Assignment Operators in C

    Assignment operators are used to assigning the result of an expression to a variable. Up to now, we have used the shorthand assignment operator "=", which assigns the result of a right-hand expression to the left-hand variable. For example, in the expression x = y + z, the sum of y and z is assigned to x.