This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

C Compound Assignment

  • 6 contributors

The compound-assignment operators combine the simple-assignment operator with another binary operator. Compound-assignment operators perform the operation specified by the additional operator, then assign the result to the left operand. For example, a compound-assignment expression such as

expression1 += expression2

can be understood as

expression1 = expression1 + expression2

However, the compound-assignment expression is not equivalent to the expanded version because the compound-assignment expression evaluates expression1 only once, while the expanded version evaluates expression1 twice: in the addition operation and in the assignment operation.

The operands of a compound-assignment operator must be of integral or floating type. Each compound-assignment operator performs the conversions that the corresponding binary operator performs and restricts the types of its operands accordingly. The addition-assignment ( += ) and subtraction-assignment ( -= ) operators can also have a left operand of pointer type, in which case the right-hand operand must be of integral type. The result of a compound-assignment operation has the value and type of the left operand.

In this example, a bitwise-inclusive-AND operation is performed on n and MASK , and the result is assigned to n . The manifest constant MASK is defined with a #define preprocessor directive.

C Assignment Operators

Was this page helpful?

Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback .

Submit and view feedback for

Additional resources

C Data Types

C operators.

  • C Input and Output
  • C Control Flow
  • C Functions
  • C Preprocessors

C File Handling

  • C Cheatsheet

C Interview Questions

  • C Programming Language Tutorial
  • C Language Introduction
  • Features of C Programming Language
  • C Programming Language Standard
  • C Hello World Program
  • Compiling a C Program: Behind the Scenes
  • Tokens in C
  • Keywords in C

C Variables and Constants

  • C Variables
  • Constants in C
  • Const Qualifier in C
  • Different ways to declare variable as constant in C
  • Scope rules in C
  • Internal Linkage and External Linkage in C
  • Global Variables in C
  • Data Types in C
  • Literals in C
  • Escape Sequence in C
  • Integer Promotions in C
  • Character Arithmetic in C
  • Type Conversion in C

C Input/Output

  • Basic Input and Output in C
  • Format Specifiers in C
  • printf in C
  • Scansets in C
  • Formatted and Unformatted Input/Output functions in C with Examples
  • Operators in C
  • Arithmetic Operators in C
  • Unary operators in C
  • Relational Operators in C
  • Bitwise Operators in C
  • C Logical Operators

Assignment Operators in C

  • Increment and Decrement Operators in C
  • Conditional or Ternary Operator (?:) in C
  • sizeof operator in C
  • Operator Precedence and Associativity in C

C Control Statements Decision-Making

  • Decision Making in C (if , if..else, Nested if, if-else-if )
  • C - if Statement
  • C if...else Statement
  • C if else if ladder
  • Switch Statement in C
  • Using Range in switch Case in C
  • while loop in C
  • do...while Loop in C
  • For Versus While
  • Continue Statement in C
  • Break Statement in C
  • goto Statement in C
  • User-Defined Function in C
  • Parameter Passing Techniques in C
  • Function Prototype in C
  • How can I return multiple values from a function?
  • main Function in C
  • Implicit return type int in C
  • Callbacks in C
  • Nested functions in C
  • Variadic functions in C
  • _Noreturn function specifier in C
  • Predefined Identifier __func__ in C
  • C Library math.h Functions

C Arrays & Strings

  • Properties of Array in C
  • Multidimensional Arrays in C
  • Initialization of Multidimensional Array in C
  • Pass Array to Functions in C
  • How to pass a 2D array as a parameter in C?
  • What are the data types for which it is not possible to create an array?
  • How to pass an array by value in C ?
  • Strings in C
  • Array of Strings in C
  • What is the difference between single quoted and double quoted declaration of char array?
  • C String Functions
  • Pointer Arithmetics in C with Examples
  • C - Pointer to Pointer (Double Pointer)
  • Function Pointer in C
  • How to declare a pointer to a function?
  • Pointer to an Array | Array Pointer
  • Difference between constant pointer, pointers to constant, and constant pointers to constants
  • Pointer vs Array in C
  • Dangling, Void , Null and Wild Pointers in C
  • Near, Far and Huge Pointers in C
  • restrict keyword in C

C User-Defined Data Types

  • C Structures
  • dot (.) Operator in C
  • Structure Member Alignment, Padding and Data Packing
  • Flexible Array Members in a structure in C
  • Bit Fields in C
  • Difference Between Structure and Union in C
  • Anonymous Union and Structure in C
  • Enumeration (or enum) in C

C Storage Classes

  • Storage Classes in C
  • extern Keyword in C
  • Static Variables in C
  • Initialization of static variables in C
  • Static functions in C
  • Understanding "volatile" qualifier in C | Set 2 (Examples)
  • Understanding "register" keyword in C

C Memory Management

  • Memory Layout of C Programs
  • Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
  • Difference Between malloc() and calloc() with Examples
  • What is Memory Leak? How can we avoid?
  • Dynamic Array in C
  • How to dynamically allocate a 2D array in C?
  • Dynamically Growing Array in C

C Preprocessor

  • C Preprocessor Directives
  • How a Preprocessor works in C?
  • Header Files in C
  • What’s difference between header files "stdio.h" and "stdlib.h" ?
  • How to write your own header file in C?
  • Macros and its types in C
  • Interesting Facts about Macros and Preprocessors in C
  • # and ## Operators in C
  • How to print a variable name in C?
  • Multiline macros in C
  • Variable length arguments for Macros
  • Branch prediction macros in GCC
  • typedef versus #define in C
  • Difference between #define and const in C?
  • Basics of File Handling in C
  • C fopen() function with Examples
  • EOF, getc() and feof() in C
  • fgets() and gets() in C language
  • fseek() vs rewind() in C
  • What is return type of getchar(), fgetc() and getc() ?
  • Read/Write Structure From/to a File in C
  • C Program to print contents of file
  • C program to delete a file
  • C Program to merge contents of two files into a third file
  • What is the difference between printf, sprintf and fprintf?
  • Difference between getc(), getchar(), getch() and getche()

Miscellaneous

  • time.h header file in C with Examples
  • Input-output system calls in C | Create, Open, Close, Read, Write
  • Signals in C language
  • Program error signals
  • Socket Programming in C
  • _Generics Keyword in C
  • Multithreading in C
  • C Programming Interview Questions (2024)
  • Commonly Asked C Programming Interview Questions | Set 1
  • Commonly Asked C Programming Interview Questions | Set 2
  • Commonly Asked C Programming Interview Questions | Set 3

compound assignment operators in c

Assignment operators are used for assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error.

Different types of assignment operators are shown below:

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:

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. Example:

If initially value stored in a is 5. Then (a += 6) = 11.

3. “-=” This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the value on the right from the current value of the variable on left and then assigns the result to the variable on the left. Example:

If initially value stored in a is 8. Then (a -= 6) = 2.

4. “*=” This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies 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. Example:

If initially value stored in a is 5. Then (a *= 6) = 30.

5. “/=” This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. Example:

If initially value stored in a is 6. Then (a /= 2) = 3.

Below example illustrates the various Assignment Operators:

Please Login to comment...

  • C-Operators
  • cpp-operator
  • 10 Best Free Code Learning Apps for Android in 2024
  • 5 Best AI Tools for Plagiarism Detection 2024
  • 10 Best iMovie Alternatives in 2024
  • 10 Best AI Tools for Sentiment Analysis
  • 30 OOPs Interview Questions and Answers (2024)

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

cppreference.com

Assignment operators.

Assignment operators modify the value of the object.

[ edit ] Definitions

Copy assignment replaces the contents of the object a with a copy of the contents of b ( b is not modified). For class types, this is performed in a special member function, described in copy assignment operator .

For non-class types, copy and move assignment are indistinguishable and are referred to as direct assignment .

Compound assignment replace the contents of the object a with the result of a binary operation between the previous value of a and the value of b .

[ edit ] Assignment operator syntax

The assignment expressions have the form

  • ↑ target-expr must have higher precedence than an assignment expression.
  • ↑ new-value cannot be a comma expression, because its precedence is lower.

[ edit ] Built-in simple assignment operator

For the built-in simple assignment, the object referred to by target-expr is modified by replacing its value with the result of new-value . target-expr must be a modifiable lvalue.

The result of a built-in simple assignment is an lvalue of the type of target-expr , referring to target-expr . If target-expr is a bit-field , the result is also a bit-field.

[ edit ] Assignment from an expression

If new-value is an expression, it is implicitly converted to the cv-unqualified type of target-expr . When target-expr is a bit-field that cannot represent the value of the expression, the resulting value of the bit-field is implementation-defined.

If target-expr and new-value identify overlapping objects, the behavior is undefined (unless the overlap is exact and the type is the same).

In overload resolution against user-defined operators , for every type T , the following function signatures participate in overload resolution:

For every enumeration or pointer to member type T , optionally volatile-qualified, the following function signature participates in overload resolution:

For every pair A1 and A2 , where A1 is an arithmetic type (optionally volatile-qualified) and A2 is a promoted arithmetic type, the following function signature participates in overload resolution:

[ edit ] Built-in compound assignment operator

The behavior of every built-in compound-assignment expression target-expr   op   =   new-value is exactly the same as the behavior of the expression target-expr   =   target-expr   op   new-value , except that target-expr is evaluated only once.

The requirements on target-expr and new-value of built-in simple assignment operators also apply. Furthermore:

  • For + = and - = , the type of target-expr must be an arithmetic type or a pointer to a (possibly cv-qualified) completely-defined object type .
  • For all other compound assignment operators, the type of target-expr must be an arithmetic type.

In overload resolution against user-defined operators , for every pair A1 and A2 , where A1 is an arithmetic type (optionally volatile-qualified) and A2 is a promoted arithmetic type, the following function signatures participate in overload resolution:

For every pair I1 and I2 , where I1 is an integral type (optionally volatile-qualified) and I2 is a promoted integral type, the following function signatures participate in overload resolution:

For every optionally cv-qualified object type T , the following function signatures participate in overload resolution:

[ edit ] Example

Possible output:

[ edit ] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

[ edit ] See also

Operator precedence

Operator overloading

  • Recent changes
  • Offline version
  • What links here
  • Related changes
  • Upload file
  • Special pages
  • Printable version
  • Permanent link
  • Page information
  • In other languages
  • This page was last modified on 25 January 2024, at 22:41.
  • This page has been accessed 410,142 times.
  • Privacy policy
  • About cppreference.com
  • Disclaimers

Powered by MediaWiki

compound assignment operators in c

  • Introduction to C
  • Download MinGW GCC C Compiler
  • Configure MinGW GCC C Compiler
  • The First C Program
  • Data Types in C
  • Variables, Keywords, Constants
  • If Statement
  • If Else Statement
  • Else If Statement
  • Nested If Statement
  • Nested If Else Statement
  • Do-While Loop
  • Break Statement
  • Switch Statement
  • Continue Statement
  • Goto Statement
  • Arithmetic Operator in C
  • Increment Operator in C
  • Decrement Operator in C
  • Compound Assignment Operator
  • Relational Operator in C
  • Logical Operator in C
  • Conditional Operator in C
  • 2D array in C
  • Functions with arguments
  • Function Return Types
  • Function Call by Value
  • Function Call by Reference
  • Recursion in C
  • Reading String from console
  • C strchr() function
  • C strlen() function
  • C strupr() function
  • C strlwr() function
  • C strcat() function
  • C strncat() function
  • C strcpy() function
  • C strncpy() function
  • C strcmp() function
  • C strncmp() function
  • Structure with array element
  • Array of structures
  • Formatted Console I/O functions
  • scanf() and printf() function
  • sscanf() and sprintf() function
  • Unformatted Console I/O functions
  • getch(), getche(), getchar(), gets()
  • putch(), putchar(), puts()
  • Reading a File
  • Writing a File
  • Append to a File
  • Modify a File

Advertisement

+= operator

  • Add operation.
  • Assignment of the result of add operation.
  • Statement i+=2 is equal to i=i+2 , hence 2 will be added to the value of i, which gives us 4.
  • Finally, the result of addition, 4 is assigned back to i, updating its original value from 2 to 4.

Example with += operator

-= operator.

  • Subtraction operation.
  • Assignment of the result of subtract operation.
  • Statement i-=2 is equal to i=i-2 , hence 2 will be subtracted from the value of i, which gives us 0.
  • Finally, the result of subtraction i.e. 0 is assigned back to i, updating its value to 0.

Example with -= operator

*= operator.

  • Multiplication operation.
  • Assignment of the result of multiplication operation.
  • Statement i*=2 is equal to i=i*2 , hence 2 will be multiplied with the value of i, which gives us 4.
  • Finally, the result of multiplication, 4 is assigned back to i, updating its value to 4.

Example with *= operator

/= operator.

  • Division operation.
  • Assignment of the result of division operation.
  • Statement i/=2 is equal to i=i/2 , hence 4 will be divided by the value of i, which gives us 2.
  • Finally, the result of division i.e. 2 is assigned back to i, updating its value from 4 to 2.

Example with /= operator

Please share this article -.

Facebook

Please Subscribe

Decodejava Facebook Page

Notifications

Please check our latest addition C#, PYTHON and DJANGO

CsTutorialPoint - Computer Science Tutorials For Beginners

Assignment Operators In C [ Full Information With Examples ]

Assignment Operators In C

Assignment Operators In C

Assignment operators is a binary operator which is used to assign values in a variable , with its right and left sides being a one-one operand. The operand on the left side is variable in which the value is assigned and the right side operands can contain any of the constant, variable, and expression.

The Assignment operator is a lower priority operator. its priority has much lower than the rest of the other operators. Its priority is more than just the comma operator. The priority of all other operators is more than the assignment operator.

We can assign the same value to multiple variables simultaneously by the assignment operator.

x = y = z = 100

Here x, y, and z are initialized to 100.

In C language, the assignment operator can be divided into two categories.

  • Simple assignment operator
  • Compound assignment operators

1. Simple Assignment Operator In C

This operator is used to assign left-side values ​​to the right-side operands, simple assignment operators are represented by (=).

2. Compound Assignment Operators In C

Compound Assignment Operators use the old value of a variable to calculate its new value and reassign the value obtained from the calculation to the same variable.

Examples of compound assignment operators are: (Example: + =, – =, * =, / =,% =, & =, ^ =)

Look at these two statements:

Here in this example, adding 5 to the x variable in the second statement is again being assigned to the x variable.

Compound Assignment Operators provide us with the C language to perform such operation even more effecient and in less time.

Syntax of Compound Assignment Operators

Here op can be any arithmetic operators (+, -, *, /,%).

The above statement is equivalent to the following depending on the function:

Let us now know about some important compound assignment operators one by one.

“+ =” -: This operator adds the right operand to the left operand and assigns the output to the left operand.

“- =” -: This operator subtracts the right operand from the left operand and returns the result to the left operand.

“* =” -: This operator multiplies the right operand with the left operand and assigns the result to the left operand.

“/ =” -: This operator splits the left operand with the right operand and assigns the result to the left operand.

“% =” -: This operator takes the modulus using two operands and assigns the result to the left operand.

There are many other assignment operators such as left shift and (<< =) operator, right shift and operator (>> =), bitwise and assignment operator (& =), bitwise OR assignment operator (^ =)

List of Assignment Operators In C

Read More -:

  • What is Operators In C
  • Relational Operators In C
  • Logical Operators In C
  • Bitwise Operators In C
  • Arithmetic Operators In C
  • Conditional Operator in C
  • Download C Language Notes Pdf
  • C Language Tutorial For Beginners
  • C Programming Examples With Output
  • 250+ C Programs for Practice PDF Free Download

Friends, I hope you have found the answer of your question and you will not have to search about the Assignment operators in C Language 

However, if you want any information related to this post or related to programming language, computer science, then comment below I will clear your all doubts.

If you want a complete tutorial of C language, then see here  C Language Tutorial . Here you will get all the topics of C Programming Tutorial step by step.

Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the Assignment operators in C Language 

To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.

' src=

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Reply Cancel reply

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

Save my name, email, and website in this browser for the next time I comment.

Compound assignment operators

The compound assignment operators consist of a binary operator and the simple assignment operator. They perform the operation of the binary operator on both operands and store the result of that operation into the left operand, which must be a modifiable lvalue.

The following table shows the operand types of compound assignment expressions:

Note that the expression

is equivalent to

The following table lists the compound assignment operators and shows an expression using each operator:

Although the equivalent expression column shows the left operands (from the example column) twice, it is in effect evaluated only once.

Using Compound Assignment Operators in C: A Beginner's Guide

An assignment operator is used for assigning a value to a variable. The most common assignment operator is = (equal). The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error.

The code is a C program that demonstrates the use of compound assignment operators in C. Here is an explanation of each line of the code:

  • int a=10,b=5; declares two variables a and b of type int and assigns them the values 10 and 5 respectively.
  • a+=b; is the compound assignment operator +=, it performs a+b and assigns the value to a. It is equivalent to a=a+b.
  • printf("A : %d",a); prints the value of a which is 15, this is the result after using += operator.
  • a-=10; is the compound assignment operator -=, it performs a-10 and assigns the value to a. It is equivalent to a=a-10.
  • printf("\nA : %d",a); prints the value of a which is 5, this is the result after using -= operator.
  • return 0; The return 0; statement is used to indicate that the main function has completed successfully. The value 0 is returned as the exit status of the program, which indicates a successful execution.

When you run this code, it will perform the compound assignment operation on variables and print the results in the console.

Source Code

List of programs.

  • C Introduction
  • Hello World
  • Basic Addition
  • Data Types in C
  • Variables and Literals in C
  • Arithmetic Operator in C
  • Example for Arithmetic Operator in C
  • Assignment Operators in C Program
  • Relational Operators in C Program
  • Logical Operators in C Program
  • Increment and Decrement Operators in C Program
  • Bitwise Operators in C Program
  • if Statement in C Program
  • if else Statement in C Program
  • else if Statement in C Program
  • Nested if Statement in C Program
  • Switch Statement in C Program
  • Conditional Operator Statement in C Program
  • School Management in C Program
  • Library Management in C Program
  • Find the given number is odd or even in C Program
  • Check the given number is vowels or not in C Program
  • Check the given number is Armstrong or not in C Program
  • Hotel Management in C Program
  • Goto Statement in C Program
  • Hotel Management using goto in C Program
  • While Loop in C Program
  • Do while Loop in C Program
  • For Loop in C Program
  • Nested For Loop in C Program
  • Break and Continue Statement in C Program
  • ASCII Values in C Program
  • Single Dimensional Array in C Program
  • 2D Array in C Program
  • String Function in C Program
  • Math Function in C Program
  • Function in C Program
  • No Return Without Argument Function in C Program
  • No Return With Argument Function in C Program
  • Return Without Argument Function in C Program
  • Return With Argument Function in C Program
  • Recursion Function in C Program
  • Call by Reference Function in C Program
  • Local Variable in C Program
  • Global Variable in C Program
  • Static Variable in C Program
  • Enumeration or enum in C Program
  • Single pointer in C Program
  • Double & Triple pointer in C Program
  • Pointer Arithmetic in C Program
  • Pointer Handle Array Values in C Program
  • Void * Pointer in C Program
  • Malloc Function in C Program
  • Calloc Function in C Program
  • Realloc Function in C Program
  • Free Function in C Program
  • Dangling Pointer in C Program
  • Using Const in Pointer in C Program
  • Structure in C Program
  • Local and Global Structure in C Program
  • Typedef in C Program
  • Initializing & Accessing the Structure Members in C Program
  • Access members of structure using pointer in C Program
  • Structure as function arguments in C Program
  • Array of Objects Structure in C Program
  • Union in C Program
  • Shop Management using Structure and Union in C Program
  • Input and output functions in C Program
  • Preprocessor Directives in C Program
  • Read File in C Program
  • Write File in C Program
  • Multiplication tables in C Program
  • Total no of even and odd numbers in an array in C Program
  • Count Alphabets Digits and Special Characters in a String in C Program
  • Convert string to uppercase in C Program
  • Convert string to lowercase in C Program
  • Display Fibonacci Sequence in C Program
  • Greatest of n numbers in an array in C Program

Sample Programs

Switch case in c.

  • Present Elevator Position
  • Check Vowel or Consonent
  • Hotel Management using Swtich Case

Conditional Operators in C

  • Find Greatest Number
  • Check Find Smallest Number
  • Check Number Equal or Not
  • Check Char. Vowel or Consonent
  • Check Character Capital or Small

Goto Statement in C

  • Consecutive Numbers
  • Print Text Using Goto
  • Print Odd Numbers
  • Printing Tables
  • Printing Sum of Values
  • Printing Reverse Table
  • Print Factorial in C

While Loop Example Programs

  • Print numbers using While Loop
  • Armstrong Number using While Loop
  • Print Odd and Even numbers
  • Print Positive and Negative numbers
  • Print Prime or Composite Number Upto Limit
  • Print Prime or Composite Number
  • Reverse table using While Loop
  • Print table using While Loop
  • Covert Decimal to Binary using While Loop

Looping Statements in C

For loop example programs.

  • Print Value upto limit
  • Print Number upto limit
  • Armstrong Number using For Loop
  • Square Pattern using For Loop
  • Flag Pattern using For Loop
  • Diamond Pattern using For Loop
  • Triangle Facing Downward using For Loop
  • Triangle Facing upside using For Loop
  • Right Angle Triangle facing left
  • Right Angle Triangle facing right
  • Reverse Number using For Loop
  • Prime Number using For Loop
  • Print Number divisible by 7
  • Print tables using For Loop
  • Reverse tables using For Loop
  • Separate odd and even numbers
  • Check prime or composite number
  • Separate positive and negative number
  • For Loop Patterns
  • Square Pattern Outline using For Loop
  • Triangle Outline Pattern
  • Diamond Pattern Outline

Array Examples in C

One dimensional array.

  • Add Elements to the Array
  • Arrange array elements in Ascending
  • Arrange array elements in Descending
  • Insert an element using Array
  • Update an element using Array
  • Delete an element using Array
  • Interchange an element using Array
  • Reverse an element using Array
  • Odd or Even Number using Array
  • Positive and Negative Numbers using Array
  • Armstrong Number 100 to 999
  • Greatest Number using Array
  • Smallest Number using Array
  • Print values divisible by 7 using Array
  • Convert binary to decimal using Array
  • Convert decimal to binary using Array
  • Convert decimal to octal using Array

Two Dimensional Array in C

  • Print Two Dimensional Array
  • Array Addition in Two-Dimenional Array
  • Array Subtraction in Two-Dimensional Array
  • Array Multiplication using Array
  • Lower Triangular Matrix using Array
  • Upper Triangluar Matrix using Array
  • Print Unit Matrix using Array
  • Check and Print Unit Matrix

String Example Programs in C

  • Print text using String
  • String Example
  • String Comparison in C
  • String Concatenation in C
  • String Copy Example in C
  • Convert Uppercase to Lowercase
  • Remove Duplicate String
  • Reverse text using String
  • Toggle Case using String
  • Conver text into Capital or Small
  • Convert Lowercase to Uppercase
  • Convert text into Ascending Order using
  • Capitalising the first letter
  • Check given strings Equal or Not
  • Copy String using Predefined Function
  • String Length using Predefined Function
  • String Length without Predefined Function
  • Uppercase to Lowercase using Predefined Function
  • Check Palindrome or Not

Functions Example Programs in C

  • Addition using return type and arguments
  • Print marks using return type & arguments
  • Function with no return and with arguments in c
  • Print tables without arguments

Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions

  • Trending Categories

Data Structure

  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary

Compound Assignment Operators in C++

The compound assignment operators are specified in the form e1 op= e2, where e1 is a modifiable l-value not of const type and e2 is one of the following −

  • An arithmetic type
  • A pointer, if op is + or –

The e1 op= e2 form behaves as e1 = e1 op e2, but e1 is evaluated only once.

The following are the compound assignment operators in C++ −

Let's have a look at an example using some of these operators −

This will give the output −

Note that Compound assignment to an enumerated type generates an error message. If the left operand is of a pointer type, the right operand must be of a pointer type or it must be a constant expression that evaluates to 0. If the left operand is of an integral type, the right operand must not be of a pointer type.

Govinda Sai

Related Articles

  • Compound assignment operators in C#
  • Compound assignment operators in Java\n
  • Assignment Operators in C++
  • What are assignment operators in C#?
  • Perl Assignment Operators
  • Assignment operators in Dart Programming
  • What are Assignment Operators in JavaScript?
  • Compound operators in Arduino
  • What is the difference between = and: = assignment operators?
  • Passing the Assignment in C++
  • Airplane Seat Assignment Probability in C++
  • What is an assignment operator in C#?
  • Copy constructor vs assignment operator in C++
  • Ternary Operators in C/C++
  • Unary operators in C/C++

Kickstart Your Career

Get certified by completing the course

PrepBytes Blog

ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING

Sign in to your account

Forgot your password?

Login via OTP

We will send you an one time password on your mobile number

An OTP has been sent to your mobile number please verify it below

Register with PrepBytes

Assignment operator in c.

' src=

Last Updated on June 23, 2023 by Prepbytes

compound assignment operators in c

This type of operator is employed for transforming and assigning values to variables within an operation. In an assignment operation, the right side represents a value, while the left side corresponds to a variable. It is essential that the value on the right side has the same data type as the variable on the left side. If this requirement is not fulfilled, the compiler will issue an error.

What is Assignment Operator in C language?

In C, the assignment operator serves the purpose of assigning a value to a variable. It is denoted by the equals sign (=) and plays a vital role in storing data within variables for further utilization in code. When using the assignment operator, the value present on the right-hand side is assigned to the variable on the left-hand side. This fundamental operation allows developers to store and manipulate data effectively throughout their programs.

Example of Assignment Operator in C

For example, consider the following line of code:

Types of Assignment Operators in C

Here is a list of the assignment operators that you can find in the C language:

Simple assignment operator (=): This is the basic assignment operator, which assigns the value on the right-hand side to the variable on the left-hand side.

Addition assignment operator (+=): This operator adds the value on the right-hand side to the variable on the left-hand side and assigns the result back to the variable.

x += 3; // Equivalent to x = x + 3; (adds 3 to the current value of "x" and assigns the result back to "x")

Subtraction assignment operator (-=): This operator subtracts the value on the right-hand side from the variable on the left-hand side and assigns the result back to the variable.

x -= 4; // Equivalent to x = x – 4; (subtracts 4 from the current value of "x" and assigns the result back to "x")

* Multiplication assignment operator ( =):** This operator multiplies the value on the right-hand side with the variable on the left-hand side and assigns the result back to the variable.

x = 2; // Equivalent to x = x 2; (multiplies the current value of "x" by 2 and assigns the result back to "x")

Division assignment operator (/=): This operator divides the variable on the left-hand side by the value on the right-hand side and assigns the result back to the variable.

x /= 2; // Equivalent to x = x / 2; (divides the current value of "x" by 2 and assigns the result back to "x")

Bitwise AND assignment (&=): The bitwise AND assignment operator "&=" performs a bitwise AND operation between the value on the left-hand side and the value on the right-hand side. It then assigns the result back to the left-hand side variable.

x &= 3; // Binary: 0011 // After bitwise AND assignment: x = 1 (Binary: 0001)

Bitwise OR assignment (|=): The bitwise OR assignment operator "|=" performs a bitwise OR operation between the value on the left-hand side and the value on the right-hand side. It then assigns the result back to the left-hand side variable.

x |= 3; // Binary: 0011 // After bitwise OR assignment: x = 7 (Binary: 0111)

Bitwise XOR assignment (^=): The bitwise XOR assignment operator "^=" performs a bitwise XOR operation between the value on the left-hand side and the value on the right-hand side. It then assigns the result back to the left-hand side variable.

x ^= 3; // Binary: 0011 // After bitwise XOR assignment: x = 6 (Binary: 0110)

Left shift assignment (<<=): The left shift assignment operator "<<=" shifts the bits of the value on the left-hand side to the left by the number of positions specified by the value on the right-hand side. It then assigns the result back to the left-hand side variable.

x <<= 2; // Binary: 010100 (Shifted left by 2 positions) // After left shift assignment: x = 20 (Binary: 10100)

Right shift assignment (>>=): The right shift assignment operator ">>=" shifts the bits of the value on the left-hand side to the right by the number of positions specified by the value on the right-hand side. It then assigns the result back to the left-hand side variable.

x >>= 2; // Binary: 101 (Shifted right by 2 positions) // After right shift assignment: x = 5 (Binary: 101)

Conclusion The assignment operator in C, denoted by the equals sign (=), is used to assign a value to a variable. It is a fundamental operation that allows programmers to store data in variables for further use in their code. In addition to the simple assignment operator, C provides compound assignment operators that combine arithmetic or bitwise operations with assignment, allowing for concise and efficient code.

FAQs related to Assignment Operator in C

Q1. Can I assign a value of one data type to a variable of another data type? In most cases, assigning a value of one data type to a variable of another data type will result in a warning or error from the compiler. It is generally recommended to assign values of compatible data types to variables.

Q2. What is the difference between the assignment operator (=) and the comparison operator (==)? The assignment operator (=) is used to assign a value to a variable, while the comparison operator (==) is used to check if two values are equal. It is important not to confuse these two operators.

Q3. Can I use multiple assignment operators in a single statement? No, it is not possible to use multiple assignment operators in a single statement. Each assignment operator should be used separately for assigning values to different variables.

Q4. Are there any limitations on the right-hand side value of the assignment operator? The right-hand side value of the assignment operator should be compatible with the data type of the left-hand side variable. If the data types are not compatible, it may lead to unexpected behavior or compiler errors.

Q5. Can I assign the result of an expression to a variable using the assignment operator? Yes, it is possible to assign the result of an expression to a variable using the assignment operator. For example, x = y + z; assigns the sum of y and z to the variable x.

Q6. What happens if I assign a value to an uninitialized variable? Assigning a value to an uninitialized variable will initialize it with the assigned value. However, it is considered good practice to explicitly initialize variables before using them to avoid potential bugs or unintended behavior.

Leave a Reply Cancel reply

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

Save my name, email, and website in this browser for the next time I comment.

  • Linked List
  • Segment Tree
  • Backtracking
  • Dynamic Programming
  • Greedy Algorithm
  • Operating System
  • Company Placement
  • Interview Tips
  • General Interview Questions
  • Data Structure
  • Other Topics
  • Computational Geometry
  • Game Theory

Related Post

Null character in c, ackermann function in c, median of two sorted arrays of different size in c, number is palindrome or not in c, implementation of queue using linked list in c, c program to replace a substring in a string.

IMAGES

  1. Compound Assignment Operators in C Programming Language

    compound assignment operators in c

  2. Exploring Compound Assignment Operators in C

    compound assignment operators in c

  3. Compound Assignment Operators in C Programming Language. 21

    compound assignment operators in c

  4. Assignment Operators in C++

    compound assignment operators in c

  5. PPT

    compound assignment operators in c

  6. PPT

    compound assignment operators in c

VIDEO

  1. C BASICS IN COMPETITIVE EXAMS (PART-1) || COMMA AS AN OPERATOR IN C || COMMA AS AN SEPARATOR IN C

  2. Augmented assignment operators in C

  3. Operators in C language

  4. Lesson16 Compound assignment with Arithmetic Operators & Assignment By Reference

  5. Assignment Operator in C Programming

  6. 61. Compound Operators

COMMENTS

  1. C Compound Assignment

    The compound-assignment operators combine the simple-assignment operator with another binary operator. Compound-assignment operators perform the operation specified by the additional operator, then assign the result to the left operand. For example, a compound-assignment expression such as. expression1 += expression2. can be understood as.

  2. Assignment operators

    Assignment performs implicit conversion from the value of rhs to the type of lhs and then replaces the value in the object designated by lhs with the converted value of rhs . Assignment also returns the same value as what was stored in lhs (so that expressions such as a = b = c are possible). The value category of the assignment operator is non ...

  3. Assignment Operators In C++

    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++:

  4. Assignment Operators in C

    Different types of assignment operators are shown below: 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: 2. "+=": This operator is combination of '+' and '=' operators.

  5. Assignment operators

    C++20 bitwise compound assignment operators for volatile types were deprecated while being useful for some platforms they are not deprecated See also. Operator precedence. Operator overloading. Common operators assignment: increment decrement: arithmetic: logical: comparison: member access: other: a = b a + = b a -= b a * = b a / = b a % = b a ...

  6. Assignment Operators in C

    In C, the assignment operator stores a certain value in an already declared variable. A variable in C can be assigned the value in the form of a literal, another variable or an expression. ... C allows you to combine arithmetic and bitwise operators with the = symbol to form augmented or compound assignment operator. The augmented operators ...

  7. C

    A special case scenario for all the compound assigned operators. int i= 2 ; i+= 2 * 2 ; //equals to, i = i+(2*2); In all the compound assignment operators, the expression on the right side of = is always calculated first and then the compound assignment operator will start its functioning. Hence in the last code, statement i+=2*2; is equal to i ...

  8. Assignment and shorthand assignment operator in C

    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.

  9. Assignment Operators In C [ Full Information With Examples ]

    Example -: 2. Compound Assignment Operators In C. Compound Assignment Operators use the old value of a variable to calculate its new value and reassign the value obtained from the calculation to the same variable. Examples of compound assignment operators are: (Example: + =, - =, * =, / =,% =, & =, ^ =) Here in this example, adding 5 to the x ...

  10. Compound assignment operators

    The compound assignment operators consist of a binary operator and the simple assignment operator. They perform the operation of the binary operator on both operands and store the result of that operation into the left operand, which must be a modifiable lvalue. The following table shows the operand types of compound assignment expressions:

  11. C Assignment Operators

    Code language:C++(cpp) The = assignment operator is called a simple assignment operator. It assigns the value of the left operand to the right operand. Besides the simple assignment operator, C supports compound assignment operators. A compound assignment operator performs the operation specified by the additional operator and then assigns the ...

  12. c

    (Now, in the earliest days of C, the compound assignment operator was spelled "=*", and there was ambiguity, which is why the compound assignment operators were rejiggered to their modern form.) - Steve Summit. Aug 30, 2018 at 9:27. Add a comment | 2

  13. Using Compound Assignment Operators in C: A Beginner's Guide

    The code is a C program that demonstrates the use of compound assignment operators in C. Here is an explanation of each line of the code: int a=10,b=5; declares two variables a and b of type int and assigns them the values 10 and 5 respectively. a+=b; is the compound assignment operator +=, it performs a+b and assigns the value to a. It is equivalent to a=a+b.

  14. Compound Assignment Operators in C++

    The compound assignment operators are specified in the form e1 op= e2, where e1 is a modifiable l-value not of const type and e2 is one of the following −. The e1 op= e2 form behaves as e1 = e1 op e2, but e1 is evaluated only once. The following are the compound assignment operators in C++ −. Multiply the value of the first operand by the ...

  15. Compound Assignment Operators in C

    This is a tough question without an easy, obvious answer. The expression is probably undefined, in which case it's meaningless to ask "what could be the possible output?", because it could be anything.. If it's undefined, it's because there are multiple writes (stores) to a without any intervening sequence points. The rightmost a += 2 computes 7 and prepares to store it into a.

  16. Exploring Compound Assignment Operators in C

    Section 3: Advantages of Using Compound Assignment Operators. 1. Conciseness and Readability. By combining the operation and assignment, compound operators make code more concise and easier to read. 2. Improved Efficiency. They can lead to more efficient code execution, especially in situations where the same variable is used in multiple ...

  17. Assignment Operator in C

    Here is a list of the assignment operators that you can find in the C language: Simple assignment operator (=): This is the basic assignment operator, which assigns the value on the right-hand side to the variable on the left-hand side. Example: int x = 10; // Assigns the value 10 to the variable "x". Addition assignment operator (+=): This ...

  18. C# compound assignment operator ^=

    5. It means bitwise XOR the value of the LHS expression with the value of the RHS expression, and assign it back to the LHS expression. So for example: int x = 10; int y = 3; x ^= y; // x = 10 ^ 3, i.e. 9. The LHS expression is only evaluated once, so if you have: array[GetIndex()] ^= 10; that would only call GetIndex once.

  19. Overloading compound assignment operators in C++

    foo operator+(foo a, const foo &b) { // First argument passed by value, second by reference. return a += b; } Making the compound operators class members and the plain operators freestanding is a common style suggestion; it allows the first argument to be of a different type as long as it can be converted to a foo: foo b; foo a = 1 + b; (All ...

  20. c++

    In general, the addition and assignment will do the same thing as the compound assignment, but this is not always the case. In general, when overloading the arithmetic operators ( +, - etc.) you should do them with their associated compound assignments as well ( +=, -= etc.). See the "Binary arithmetic operators" on cppreference for some ...