CProgramming Tutorial

  • C Programming Tutorial
  • Basics of C
  • C - Overview
  • C - Features
  • C - History
  • C - Environment Setup
  • C - Program Structure
  • C - Hello World
  • C - Compilation Process
  • C - Comments
  • 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
  • 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 - 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 - 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 - 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 - Static Keyword
  • C - Random Number Generation
  • C - Command Line Arguments
  • C Programming Resources
  • C - Questions & Answers
  • C - Quick Guide
  • C - Cheat Sheet
  • C - Useful Resources
  • C - Discussion
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary

Assignment Operators in C

In C language, 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.

The value to be assigned forms the right-hand operand, whereas the variable to be assigned should be the operand to the left of the " = " symbol, which is defined as a simple assignment operator in C.

In addition, C has several augmented assignment operators.

The following table lists the assignment operators supported by the C language −

Operator Description Example
= Simple assignment operator. Assigns values from right side operands to left side operand C = A + B will assign the value of A + B to C
+= Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C + A
-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. C -= A is equivalent to C = C - A
*= Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. C *= A is equivalent to C = C * A
/= Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. C /= A is equivalent to C = C / A
%= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2

Simple Assignment Operator (=)

The = operator is one of the most frequently used operators in C. As per the ANSI C standard, all the variables must be declared in the beginning. Variable declaration after the first processing statement is not allowed.

You can declare a variable to be assigned a value later in the code, or you can initialize it at the time of declaration.

You can use a literal, another variable, or an expression in the assignment statement.

Once a variable of a certain type is declared, it cannot be assigned a value of any other type. In such a case the C compiler reports a type mismatch error.

In C, the expressions that refer to a memory location are called "lvalue" expressions. A lvalue may appear as either the left-hand or right-hand side of an assignment.

On the other hand, the term rvalue refers to a data value that is stored at some address in memory. A rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.

Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned and cannot appear on the left-hand side. Take a look at the following valid and invalid statements −

Augmented Assignment Operators

In addition to the = operator, C allows you to combine arithmetic and bitwise operators with the = symbol to form augmented or compound assignment operator. The augmented operators offer a convenient shortcut for combining arithmetic or bitwise operation with assignment.

For example, the expression "a += b" has the same effect of performing "a + b" first and then assigning the result back to the variable "a".

Run the code and check its output −

Similarly, the expression "a <<= b" has the same effect of performing "a << b" first and then assigning the result back to the variable "a".

Here is a C program that demonstrates the use of assignment operators in C −

When you compile and execute the above program, it will produce the following result −

Assignment Operators in C

C Assignment OperatorsExampleExplanation
=x = 25Value 25 is assigned to x
+=x += 25This is the same as x = x + 25
-=x -= 25This is the same as x = x – 25
*=y *= 25This is the same as y = y * 25
/=y /= 25This is the same as y = y / 25
%=y%= 25This is the same as y = y % 25

Assignment Operators in C Example

Home » Learn C Programming from Scratch » C Assignment Operators

C Assignment Operators

Summary : in this tutorial, you’ll learn about the C assignment operators and how to use them effectively.

Introduction to the C assignment operators

An assignment operator assigns the vale of the right-hand operand to the left-hand operand. The following example uses the assignment operator (=) to assign 1 to the counter variable:

After the assignmment, the counter variable holds the number 1.

The following example adds 1 to the counter and assign the result to the counter:

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 result to the left operand.

The following example uses a compound-assignment operator (+=):

The expression:

is equivalent to the following expression:

The following table illustrates the compound-assignment operators in C:

OperatorOperation PerformedExampleEquivalent expression
Multiplication assignmentx *= yx = x * y
Division assignmentx /= yx = x / y
Remainder assignmentx %= yx = x % y
Addition assignmentx += yx = x + y
Subtraction assignmentx -= yx = x – y
Left-shift assignmentx <<= yx = x <<=y
Right-shift assignmentx >>=yx = x >>= y
Bitwise-AND assignmentx &= yx = x & y
Bitwise-exclusive-OR assignmentx ^= yx = x ^ y
Bitwise-inclusive-OR assignmentx |= yx = x | y
  • A simple assignment operator assigns the value of the left operand to the right operand.
  • A compound assignment operator performs the operation specified by the additional operator and then assigns the result to the left operand.

01 Career Opportunities

02 beginner, 03 intermediate, 04 advanced, 05 training programs, c programming assignment operators, free c programming online course with certificate, what is an assignment operator in c, types of assignment operators in c.

1. Simple Assignment Operator (=)

Example of simple assignment operator.

2. Compound Assignment Operators

+=addition assignmentIt adds the right operand to the left operand and assigns the result to the left operand.
-=subtraction assignmentIt subtracts the right operand from the left operand and assigns the result to the left operand.
*=multiplication assignmentIt multiplies the right operand with the left operand and assigns the result to the left operand
/=division assignmentIt divides the left operand with the right operand and assigns the result to the left operand.
%=modulo assignmentIt takes modulus using two operands and assigns the result to the left operand.

Example of Augmented Arithmetic and Assignment Operators

&=bitwise AND assignmentIt performs the bitwise AND operation on the variable with the value on the right
|=bitwise OR assignmentIt performs the bitwise OR operation on the variable with the value on the right
^=bitwise XOR assignmentIt performs the bitwise XOR operation on the variable with the value on the right
<<=bitwise left shift assignmentShifts the bits of the variable to the left by the value on the right
>>=bitwise right shift assignmentShifts the bits of the variable to the right by the value on the right

Example of Augmented Bitwise and Assignment Operators

Practice problems on assignment operators in c, 1. what will the value of "x" be after the execution of the following code, 2. after executing the following code, what is the value of the number variable, benefits of using assignment operators, best practices and tips for using the assignment operator, live classes schedule.

Filling Fast
Filling Fast
Filling Fast
Filling Fast
Filling Fast
Filling Fast
Filling Fast

About Author

This browser is no longer supported.

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

C Assignment Operators

  • 6 contributors

An assignment operation assigns the value of the right-hand operand to the storage location named by the left-hand operand. Therefore, the left-hand operand of an assignment operation must be a modifiable l-value. After the assignment, an assignment expression has the value of the left operand but isn't an l-value.

assignment-expression :   conditional-expression   unary-expression assignment-operator assignment-expression

assignment-operator : one of   = *= /= %= += -= <<= >>= &= ^= |=

The assignment operators in C can both transform and assign values in a single operation. C provides the following assignment operators:

Operator Operation Performed
Simple assignment
Multiplication assignment
Division assignment
Remainder assignment
Addition assignment
Subtraction assignment
Left-shift assignment
Right-shift assignment
Bitwise-AND assignment
Bitwise-exclusive-OR assignment
Bitwise-inclusive-OR assignment

In assignment, the type of the right-hand value is converted to the type of the left-hand value, and the value is stored in the left operand after the assignment has taken place. The left operand must not be an array, a function, or a constant. The specific conversion path, which depends on the two types, is outlined in detail in Type Conversions .

  • Assignment Operators

Was this page helpful?

Additional resources

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.

OperationDescription
Assigns 10 to variable
Evaluates expression and assign result to
Evaluates and assign result to
Error, you cannot re-assign a value to a constant
Error, you cannot re-assign a value to a constant

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 assignment operatorExampleMeaning

cppreference.com

Assignment operators.

(C11)
Miscellaneous
General
(C11)
(C99)

Assignment and compound assignment operators are binary operators that modify the variable to their left using the value to their right.

Operator Operator name Example Description Equivalent of
= basic assignment a = b becomes equal to
+= addition assignment a += b becomes equal to the addition of and a = a + b
-= subtraction assignment a -= b becomes equal to the subtraction of from a = a - b
*= multiplication assignment a *= b becomes equal to the product of and a = a * b
/= division assignment a /= b becomes equal to the division of by a = a / b
%= modulo assignment a %= b becomes equal to the remainder of divided by a = a % b
&= bitwise AND assignment a &= b becomes equal to the bitwise AND of and a = a & b
|= bitwise OR assignment a |= b becomes equal to the bitwise OR of and a = a | b
^= bitwise XOR assignment a ^= b becomes equal to the bitwise XOR of and a = a ^ b
<<= bitwise left shift assignment a <<= b becomes equal to left shifted by a = a << b
>>= bitwise right shift assignment a >>= b becomes equal to right shifted by a = a >> b
Simple assignment Notes Compound assignment References See Also See also

[ edit ] Simple assignment

The simple assignment operator expressions have the form

lhs rhs
lhs - expression of any complete object type
rhs - expression of any type to lhs or with lhs

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-lvalue (so that expressions such as ( a = b ) = c are invalid).

rhs and lhs must satisfy one of the following:

  • both lhs and rhs have compatible struct or union type, or..
  • rhs must be implicitly convertible to lhs , which implies
  • both lhs and rhs have arithmetic types , in which case lhs may be volatile -qualified or atomic (since C11)
  • both lhs and rhs have pointer to compatible (ignoring qualifiers) types, or one of the pointers is a pointer to void, and the conversion would not add qualifiers to the pointed-to type. lhs may be volatile or restrict (since C99) -qualified or atomic (since C11) .
  • lhs is a (possibly qualified or atomic (since C11) ) pointer and rhs is a null pointer constant such as NULL or a nullptr_t value (since C23)
has type (possibly qualified or atomic(since C11)) _Bool and rhs is a pointer or a value(since C23) (since C99)
has type (possibly qualified or atomic) and rhs has type (since C23)

[ edit ] Notes

If rhs and lhs overlap in memory (e.g. they are members of the same union), the behavior is undefined unless the overlap is exact and the types are compatible .

Although arrays are not assignable, an array wrapped in a struct is assignable to another object of the same (or compatible) struct type.

The side effect of updating lhs is sequenced after the value computations, but not the side effects of lhs and rhs themselves and the evaluations of the operands are, as usual, unsequenced relative to each other (so the expressions such as i = ++ i ; are undefined)

Assignment strips extra range and precision from floating-point expressions (see FLT_EVAL_METHOD ).

In C++, assignment operators are lvalue expressions, not so in C.

[ edit ] Compound assignment

The compound assignment operator expressions have the form

lhs op rhs
op - one of *=, /= %=, += -=, <<=, >>=, &=, ^=, |=
lhs, rhs - expressions with (where lhs may be qualified or atomic), except when op is += or -=, which also accept pointer types with the same restrictions as + and -

The expression lhs @= rhs is exactly the same as lhs = lhs @ ( rhs ) , except that lhs is evaluated only once.

If lhs has type, the operation behaves as a single atomic read-modify-write operation with memory order .

For integer atomic types, the compound assignment @= is equivalent to:

addr = &lhs; T2 val = rhs; T1 old = *addr; T1 new; do { new = old @ val } while (! (addr, &old, new);
(since C11)

[ edit ] References

  • C17 standard (ISO/IEC 9899:2018):
  • 6.5.16 Assignment operators (p: 72-73)
  • C11 standard (ISO/IEC 9899:2011):
  • 6.5.16 Assignment operators (p: 101-104)
  • C99 standard (ISO/IEC 9899:1999):
  • 6.5.16 Assignment operators (p: 91-93)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 3.3.16 Assignment operators

[ edit ] See Also

Operator precedence

Common operators

a = b
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b

a[b]
*a
&a
a->b
a.b

a(...)
a, b
(type) a
a ? b : c
sizeof


_Alignof
(since C11)

[ edit ] See also

for Assignment operators
  • 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 19 August 2022, at 09:36.
  • Privacy policy
  • About cppreference.com
  • Disclaimers

Powered by MediaWiki

C Programming Tutorial

  • Assignment Operator in C

Last updated on July 27, 2020

We have already used the assignment operator ( = ) several times before. Let's discuss it here in detail. The assignment operator ( = ) is used to assign a value to the variable. Its general format is as follows:

The operand on the left side of the assignment operator must be a variable and operand on the right-hand side must be a constant, variable or expression. Here are some examples:

x = 18 // right operand is a constant y = x // right operand is a variable z = 1 * 12 + x // right operand is an expression

The precedence of the assignment operator is lower than all the operators we have discussed so far and it associates from right to left.

We can also assign the same value to multiple variables at once.

here x , y and z are initialized to 100 .

Since the associativity of the assignment operator ( = ) is from right to left. The above expression is equivalent to the following:

Note that expressions like:

x = 18 y = x z = 1 * 12 + x

are called assignment expression. If we put a semicolon( ; ) at the end of the expression like this:

x = 18; y = x; z = 1 * 12 + x;

then the assignment expression becomes assignment statement.

Compound Assignment Operator #

Assignment operations that use the old value of a variable to compute its new value are called Compound Assignment.

Consider the following two statements:

x = 100; x = x + 5;

Here the second statement adds 5 to the existing value of x . This value is then assigned back to x . Now, the new value of x is 105 .

To handle such operations more succinctly, C provides a special operator called Compound Assignment operator.

The general format of compound assignment operator is as follows:

where op can be any of the arithmetic operators ( + , - , * , / , % ). The above statement is functionally equivalent to the following:

Note : In addition to arithmetic operators, op can also be >> (right shift), << (left shift), | (Bitwise OR), & (Bitwise AND), ^ (Bitwise XOR). We haven't discussed these operators yet.

After evaluating the expression, the op operator is then applied to the result of the expression and the current value of the variable (on the RHS). The result of this operation is then assigned back to the variable (on the LHS). Let's take some examples: The statement:

is equivalent to x = x + 5; or x = x + (5); .

Similarly, the statement:

is equivalent to x = x * 2; or x = x * (2); .

Since, expression on the right side of op operator is evaluated first, the statement:

is equivalent to x = x * (y + 1) .

The precedence of compound assignment operators are same and they associate from right to left (see the precedence table ).

The following table lists some Compound assignment operators:

Operator Description
equivalent to
equivalent to
equivalent to
equivalent to

The following program demonstrates Compound assignment operators in action:

#include<stdio.h> int main(void) { int i = 10; char a = 'd'; printf("ASCII value of %c is %d\n", a, a); // print ASCII value of d a += 10; // increment a by 10; printf("ASCII value of %c is %d\n", a, a); // print ASCII value of n a *= 5; // multiple a by 5; printf("a = %d\n", a); a /= 4; // divide a by 4; printf("a = %d\n", a); a %= 2; // remainder of a % 2; printf("a = %d\n", a); a *= a + i; // is equivalent to a = a * (a + i) printf("a = %d\n", a); return 0; // return 0 to operating system }

Expected Output:

ASCII value of d is 100 ASCII value of n is 110 a = 38 a = 9 a = 1 a = 11

Load Comments

  • Intro to C Programming
  • Installing Code Blocks
  • Creating and Running The First C Program
  • Basic Elements of a C Program
  • Keywords and Identifiers
  • Data Types in C
  • Constants in C
  • Variables in C
  • Input and Output in C
  • Formatted Input and Output in C
  • Arithmetic Operators in C
  • Operator Precedence and Associativity in C
  • Increment and Decrement Operators in C
  • Relational Operators in C
  • Logical Operators in C
  • Conditional Operator, Comma operator and sizeof() operator in C
  • Implicit Type Conversion in C
  • Explicit Type Conversion in C
  • if-else statements in C
  • The while loop in C
  • The do while loop in C
  • The for loop in C
  • The Infinite Loop in C
  • The break and continue statement in C
  • The Switch statement in C
  • Function basics in C
  • The return statement in C
  • Actual and Formal arguments in C
  • Local, Global and Static variables in C
  • Recursive Function in C
  • One dimensional Array in C
  • One Dimensional Array and Function in C
  • Two Dimensional Array in C
  • Pointer Basics in C
  • Pointer Arithmetic in C
  • Pointers and 1-D arrays
  • Pointers and 2-D arrays
  • Call by Value and Call by Reference in C
  • Returning more than one value from function in C
  • Returning a Pointer from a Function in C
  • Passing 1-D Array to a Function in C
  • Passing 2-D Array to a Function in C
  • Array of Pointers in C
  • Void Pointers in C
  • The malloc() Function in C
  • The calloc() Function in C
  • The realloc() Function in C
  • String Basics in C
  • The strlen() Function in C
  • The strcmp() Function in C
  • The strcpy() Function in C
  • The strcat() Function in C
  • Character Array and Character Pointer in C
  • Array of Strings in C
  • Array of Pointers to Strings in C
  • The sprintf() Function in C
  • The sscanf() Function in C
  • Structure Basics in C
  • Array of Structures in C
  • Array as Member of Structure in C
  • Nested Structures in C
  • Pointer to a Structure in C
  • Pointers as Structure Member in C
  • Structures and Functions in C
  • Union Basics in C
  • typedef statement in C
  • Basics of File Handling in C
  • fputc() Function in C
  • fgetc() Function in C
  • fputs() Function in C
  • fgets() Function in C
  • fprintf() Function in C
  • fscanf() Function in C
  • fwrite() Function in C
  • fread() Function in C

Recent Posts

  • Machine Learning Experts You Should Be Following Online
  • 4 Ways to Prepare for the AP Computer Science A Exam
  • Finance Assignment Online Help for the Busy and Tired Students: Get Help from Experts
  • Top 9 Machine Learning Algorithms for Data Scientists
  • Data Science Learning Path or Steps to become a data scientist Final
  • Enable Edit Button in Shutter In Linux Mint 19 and Ubuntu 18.04
  • Python 3 time module
  • Pygments Tutorial
  • How to use Virtualenv?
  • Installing MySQL (Windows, Linux and Mac)
  • What is if __name__ == '__main__' in Python ?
  • Installing GoAccess (A Real-time web log analyzer)
  • Installing Isso

assignment operator in c programming

  • 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

assignment operator in c programming

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.

Expression with an assignment operatorDetailed expression with an assignment operator
x += y;x = x + y;
x -= y;x = x – y;
x /= y;x = x / y;
x *= y;x = x * y;
x %= y;x = x % y;
x &= y;x = x & y;
x |= y;x = x | y;
x ^= y;x = x ^ y;
x >>= y;x = x >> y;
x <<= y;x = x << y;

Expected Output:

C Programming Assignment Operators

C Programming Assignment Operators

c programming assignment operators

Key Highlights

  • Assignment operators are used to assign values to variables in C programming.
  • The simple assignment operator “=” assigns a value on the side to the variable on the left side.
  • Compound operators like “+=” and “-=” perform operation and then assign the result the variable.
  • Assignment operators have lower precedence than other operators in C.
  • Understanding assignment operators is crucial for manipulating variables and performing calculations in C.

Introduction

In C programming, assignment operators assign values to variables. Programmers use “=” for this. The left side is a variable, the right side a value or expression.

The “=” operator in C assigns the right value to the left variable. For instance, “x = 5;” sets x as 5.

C also has compound assignment operators like “+=” that combine an operation and assignment. These operators calculate using both sides and assign the result to the left variable.

Knowing assignment operators is crucial for variable manipulation in C programming. Different operators help streamline code and assign values effectively.

Exploring C Programming Assignment Operators

Assignment operators in C are used to give values to variables. These operators modify variable values in expressions. C has various assignment operators, such as “=”, “+=”, “-=”, “*=”, “/=”, “%=”, “<<=”, “>>=”, “&=”, “|=”, and “^=”. They help efficiently assign and change values. These operators work with various data types and can do arithmetic and bitwise operations during assignment.

1. Simple Assignment Operator (=)

The basic “=” operator in C programming assigns the value on the right to the variable on the left. In an example like int x = 5; , 5 goes to variable x using “=” operator. The variable x then holds the value 5. This operator suits integers, floats, characters, and custom data types but requires matching data types. If not matched, an error occurs. It’s often used to set or change values in programs.

Simple Assignment Operator

2. Addition Assignment Operator (+=)

The addition assignment operator “+=” is a compound assignment operator in C programming. It combines the addition operation with the assignment operation. It adds the value on the right side to the current value of the variable on the left side and assigns the result back to the variable on the left side.

For example:

In the above example, the current value of the variable x is 5. The addition assignment operator adds the value 3 to the current value of x, resulting in x being updated to 8.

The addition assignment operator can be used with different data types, such as integers, floats, and characters. It provides a concise way to perform addition and assignment in a single statement. It is particularly useful when updating variables in loops or incrementing/decrementing variables.

3. Subtraction Assignment Operator (-=)

The subtraction assignment operator “-=” is a compound assignment operator in C programming . It combines the subtraction operation with the assignment operation. It subtracts the value on the right side from the current value of the variable on the left side and assigns the result back to the variable on the left side.

In the above example, the current value of the variable x is 10. The subtraction assignment operator subtracts the value 3 from the current value of x, resulting in x being updated to 7.

The subtraction assignment operator is commonly used to decrement variables by a specific value. It provides a concise way to perform subtraction and assignment in a single statement.

4. Multiplication Assignment Operator (*=)

The multiplication assignment operator “*=” is a compound assignment operator in C programming . It combines the multiplication operation with the assignment operation. It multiplies the value on the right side with the current value of the variable on the left side and assigns the result back to the variable on the left side.

In the above example, the current value of the variable x is 5. The multiplication assignment operator multiplies the value 2 with the current value of x, resulting in x being updated to 10.

The multiplication assignment operator is commonly used to multiply variables by a specific value. It provides a concise way to perform multiplication and assignment in a single statement.

5. Division Assignment Operator (/=)

The division assignment operator “/=” is a compound assignment operator in C programming. It combines the division operation with the assignment operation. It divides the current value of the variable on the left side by the value on the right side and assigns the quotient back to the variable on the left side.

In the above example, the current value of the variable x is 10. The division assignment operator divides the current value of x by the value 3, resulting in x being updated to 3.

The division assignment operator performs integer division, which means it discards the remainder. If you want to obtain the remainder, you can use the modulus assignment operator (%=).

The division assignment operator is commonly used to divide variables by a specific value. It provides a concise way to perform division and assignment in a single statement.

6. Modulus Assignment Operator (%=)

The modulus assignment operator “%=” is a compound assignment operator in C programming . It combines the modulus operation with the assignment operation.

In the above example, the current value of the variable x is 10. The modulus assignment operator divides the current value of x by the value 3 and assigns the remainder (1) back to x.

The modulus assignment operator is commonly used to obtain the remainder of division. It provides a concise way to perform modulus operation and assignment in a single statement.

7. Left Shift Assignment Operator (<<=)

The left shift assignment operator “<<=” is a compound assignment operator in C . It combines the left shift operation with the assignment operation. It shifts the bits of the variable on the left side to the left by the number of positions specified by the value on the right side and assigns the result back to the variable on the left side.

In the above example, the current value of the variable x is 10. The left shift assignment operator shifts the bits of x to the left by 2 positions, resulting in x being updated to 40.

The left shift assignment operator is commonly used in bitwise operations to manipulate individual bits of a variable. It provides a concise way to perform left shift operation and assignment in a single statement.

8. Right Shift Assignment Operator (>>=)

The right shift assignment operator “>>=” is a compound assignment operator in C programming. It combines the right shift operation with the assignment operation. It shifts the bits of the variable on the left side to the right by the number of positions specified by the value on the right side and assigns the result back to the variable on the left side.

In the above example, the current value of the variable x is 10. The right shift assignment operator shifts the bits of x to the right by 2 positions, resulting in x being updated to 2.

The right shift assignment operator is commonly used in bitwise operations to manipulate individual bits of a variable. It provides a concise way to perform right shift operation and assignment in a single statement.

9. Bitwise AND Assignment Operator (&=)

The bitwise AND assignment operator “&=” is a compound assignment operator . It combines the bitwise AND operation with the assignment operation. It performs a bitwise AND operation between the variable on the left side and the value on the right side and assigns the result back to the variable on the left side.

In the above example, the current value of the variable x is 5. The bitwise AND assignment operator performs a bitwise AND operation between x and 3, resulting in x being updated to 1.

The bitwise AND assignment operator is commonly used in bitwise operations to perform logical AND operations on individual bits of a variable. It provides a concise way to perform bitwise AND operation and assignment in a single statement.

10. Bitwise OR Assignment Operator (|=)

The bitwise OR assignment operator “|=” is a compound assignment operator in C programming. It combines the bitwise OR operation with the assignment operation. It performs a bitwise OR operation between the variable on the left side and the value on the right side and assigns the result back to the variable on the left side.

In the above example, the current value of the variable x is 5. The bitwise OR assignment operator performs a bitwise OR operation between x and 3, resulting in x being updated to 7.

The bitwise OR assignment operator is commonly used in bitwise operations to perform logical OR operations on individual bits of a variable. It provides a concise way to perform bitwise OR operation and assignment in a single statement.

11. Bitwise XOR Assignment Operator (^=)

The bitwise XOR assignment operator “^=” is a compound assignment operator in C programming. It combines the bitwise XOR operation with the assignment operation. It performs a bitwise XOR operation between the variable on the left side and the value on the right side and assigns the result back to the variable on the left side.

In the above example, the current value of the variable x is 5. The bitwise XOR assignment operator performs a bitwise XOR operation between x and 3, resulting in x being updated to 6.

The bitwise XOR assignment operator is commonly used in bitwise operations to perform logical XOR operations on individual bits of a variable. It provides a concise way to perform bitwise XOR operation and assignment in a single statement.

Understanding the Use of Assignment Operators in C

Assignment operators are an important part of the C programming language. They allow programmers to assign values to variables and modify variable values efficiently. By using assignment operators, programmers can write concise and readable code. Assignment operators also play a role in improving code efficiency, as they allow for the execution of multiple operations in a single statement. Understanding the use of assignment operators in C is crucial for writing efficient and readable code.

How Assignment Operators Enhance Code Readability

Assignment operators enhance code readability in C programming. By using assignment operators, programmers can write concise and self-explanatory code. Instead of writing multiple lines of code to assign values to variables, assignment operators allow for the execution of assignment and operation in a single statement. This reduces the number of lines of code and makes the code more readable. Assignment operators also make the code more maintainable, as the intent of the code is clear and the logic is easier to understand. Overall, assignment operators improve code readability and make the code more efficient and concise.

How Assignment Operators Enhance Code Readability programminghouse.org

The Efficiency of Using Compound Assignment Operators

Using compound assignment operators in C programming can improve code efficiency. Compound assignment operators combine an operation with the assignment operation, allowing for the execution of multiple operations in a single statement. This reduces the number of lines of code and improves code efficiency. By using compound assignment operators, programmers can write more concise code and reduce the execution time of the program. Additionally, compound assignment operators make the code more readable and maintainable, as the intent of the code is clear and the logic is easier to understand. Overall, using compound assignment operators enhances code efficiency and improves the overall performance of the program.

The Efficiency of Using Compound Assignment Operators

Practical Examples of Assignment Operators in C Programming

Practical examples of assignment operators in C programming can help illustrate their usage and benefits. These examples demonstrate how assignment operators can be used to assign values to variables, perform arithmetic operations, and update variable values. By applying assignment operators in real-world scenarios, programmers can understand their practical applications and improve their understanding of C programming concepts. Practical examples also provide hands-on experience and reinforce the knowledge gained from studying the theory of assignment operators. Let’s explore some practical examples to illustrate the use of assignment operators in C programming.

Example Demonstrating Simple Assignment Operator

Here is an example that demonstrates the use of the simple assignment operator in C programming:

In this example, we declare a variable x of the type int. We then assign the value 5 to x using the simple assignment operator “=”.

The printf() function is used to display the value of x on the console. The output of this program will be “The value of x is 5”.

This example showcases how the simple assignment operator can be used to assign a value to a variable in C programming. It demonstrates the basic usage and functionality of the assignment operator.

Implementing Compound Assignment Operators in Loops

Compound assignment operators can be particularly useful when used in loops in C programming. They can simplify and optimize code by combining assignment and operation in a single statement. Here is an example demonstrating the implementation of compound assignment operators in loops:

In this example, we use the compound assignment operator “+=” to calculate the sum of numbers from 1 to 10. The loop iterates from 1 to 10, and in each iteration, the value of i is added to the variable sum using the “+=” operator.

The printf() function is used to display the sum of the numbers from 1 to 10 on the console. The output of this program will be “The sum of numbers from 1 to 10 is 55”.

This example demonstrates how compound assignment operators can simplify code and perform calculations efficiently in loops.

In essence, mastering C programming assignment operators is fundamental for code efficiency and readability. Understanding the nuances of simple (=) to complex bitwise XOR (^=) operators is key to optimizing your code. By implementing compound assignment operators effectively, you streamline your programming process and enhance code comprehension. Explore practical examples and frequently asked questions to solidify your grasp on these operators. Embrace the power of assignment operators in C programming to elevate your coding skills to new heights effortlessly.

Frequently Asked Questions

What is the difference between = and == in c.

The “=” operator is the simple assignment operator in C, used to assign a value to a variable. On the other hand, the “==” operator is the equality operator in C, used to compare two values for equality. While “=” assigns a value to a variable, “==” tests whether two values are equal. For example, “x = 5;” assigns the value 5 to the variable x, while “if (x == 5)” tests whether the value of x is equal to 5.

Leave a Comment Cancel reply

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

Programming House

Are you ready to enter the 'Programming House' of our hearts? Together, we'll debug each other's flaws, compile our dreams, and run the code of love, creating an endless loop of happiness.

Privacy Policy

Popular Posts

Types of Inheritance in C++

Types of Inheritance in C++

September 7, 2024

defining structures in c

defining structures in c

September 4, 2024

Accessing Individual Elements of a 2D Array in C++

© 2024 ProgrammingHouse

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

assignment operator in c programming

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.

Javatpoint Logo

  • Design Pattern
  • Interview Q

C Control Statements

C functions, c dynamic memory, c structure union, c file handling, c preprocessor, c command line, c programming test, c interview.

JavaTpoint

There are different kinds of the operators, such as arithmetic, relational, bitwise, assignment, etc., in the C programming language. The assignment operator is used to assign the value, variable and function to another variable. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=.


It is the operator used to assign the right side operand or variable to the left side variable.

Let's create a program to use the simple assignment operator in C.

The operator is used to add the left side operand to the left operand and then assign results to the left operand.

Let's create a program to use the Plus and assign operator in C.

The operator is used to subtract the left operand with the right operand and then assigns the result to the left operand.

Let's create a program to use the Subtract and Assign (-=) operator in C.

The operator is used to multiply the left operand with the right operand and then assign result to the left operand.

Let's create a program to use the multiply and assign operator (*=) in C.

An operator is used between the left and right operands, which divides the first number by the second number to return the result in the left operand.

Let's create a program to use the divide and assign operator (/=) in C.

An operator used between the left operand and the right operand divides the first number (n1) by the second number (n2) and returns the remainder in the left operand.

Let's create a program to use the divide and assign operator (%=) in C.





Youtube

  • Send your Feedback to [email protected]

Help Others, Please Share

facebook

Learn Latest Tutorials

Splunk tutorial

Transact-SQL

Tumblr tutorial

Reinforcement Learning

R Programming tutorial

R Programming

RxJS tutorial

React Native

Python Design Patterns

Python Design Patterns

Python Pillow tutorial

Python Pillow

Python Turtle tutorial

Python Turtle

Keras tutorial

Preparation

Aptitude

Verbal Ability

Interview Questions

Interview Questions

Company Interview Questions

Company Questions

Trending Technologies

Artificial Intelligence

Artificial Intelligence

AWS Tutorial

Cloud Computing

Hadoop tutorial

Data Science

Angular 7 Tutorial

Machine Learning

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures

DAA tutorial

Operating System

Computer Network tutorial

Computer Network

Compiler Design tutorial

Compiler Design

Computer Organization and Architecture

Computer Organization

Discrete Mathematics Tutorial

Discrete Mathematics

Ethical Hacking

Ethical Hacking

Computer Graphics Tutorial

Computer Graphics

Software Engineering

Software Engineering

html tutorial

Web Technology

Cyber Security tutorial

Cyber Security

Automata Tutorial

C Programming

C++ tutorial

Control System

Data Mining Tutorial

Data Mining

Data Warehouse Tutorial

Data Warehouse

RSS Feed

C Functions

C structures, c reference, c operators.

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable:

C divides the operators into the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

Operator Name Description Example Try it
+ Addition Adds together two values x + y
- Subtraction Subtracts one value from another x - y
* Multiplication Multiplies two values x * y
/ Division Divides one value by another x / y
% Modulus Returns the division remainder x % y
++ Increment Increases the value of a variable by 1 ++x
-- Decrement Decreases the value of a variable by 1 --x

Assignment Operators

Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator ( = ) to assign the value 10 to a variable called x :

The addition assignment operator ( += ) adds a value to a variable:

A list of all assignment operators:

Operator Example Same As Try it
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3

Comparison Operators

Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions.

The return value of a comparison is either 1 or 0 , which means true ( 1 ) or false ( 0 ). These values are known as Boolean values , and you will learn more about them in the Booleans and If..Else chapter.

Comparison operators are used to compare two values.

Note: The return value of a comparison is either true ( 1 ) or false ( 0 ).

In the following example, we use the greater than operator ( > ) to find out if 5 is greater than 3:

A list of all comparison operators:

Operator Name Example Description Try it
== Equal to x == y Returns 1 if the values are equal
!= Not equal x != y Returns 1 if the values are not equal
> Greater than x > y Returns 1 if the first value is greater than the second value
< Less than x < y Returns 1 if the first value is less than the second value
>= Greater than or equal to x >= y Returns 1 if the first value is greater than, or equal to, the second value
<= Less than or equal to x <= y Returns 1 if the first value is less than, or equal to, the second value

Logical Operators

You can also test for true or false values with logical operators.

Logical operators are used to determine the logic between variables or values, by combining multiple conditions:

Operator Name Example Description Try it
&&  AND x < 5 &&  x < 10 Returns 1 if both statements are true
||  OR x < 5 || x < 4 Returns 1 if one of the statements is true
! NOT !(x < 5 && x < 10) Reverse the result, returns 0 if the result is 1

C Exercises

Test yourself with exercises.

Fill in the blanks to multiply 10 with 5 , and print the result:

Start the Exercise

Get Certified

COLOR PICKER

colorpicker

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected]

Top Tutorials

Top references, top examples, get certified.

Assignment Operator in C

Using assignment operators, we can assign value to the variables.

Equality sign (=) is used as an assignment operator in C.

Here, value 5 has assigned to the variable var.

Here, value of a has assigned to the variable b . Now, both a and b will hold value 10 .

Basically, the value of right-side operand will be assigned to the left side operand.

Pictorial Explanation

How assignment works

Compound assignment operators

Operator

Meaning

Example
(a = 10 , b = 5)

L=L+R
add left and right operand and assign result in left

same as a=a+b
after execution will hold 15

L=L-R
subtract right operand from left operand and assign result in left

same as a=a-b
after execution will hold 5

L=L*R
multiply both right and left operand and store result in left

same as a=a*b
after execution will hold 50

L=L/R
divides left operand by right operand and store result in left

same as a=a/b
after execution will hold 2

L=L%R
After left and right operand division, the remainder will be stored in left

same as a=a%b
after execution will hold 0

Sample Program

  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
  • OverflowAI GenAI features for Teams
  • OverflowAPI Train & fine-tune LLMs
  • Labs The future of collective knowledge sharing
  • About the company Visit the blog

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Get early access and see previews of new features.

What is the difference between += and =+ C assignment operators [duplicate]

I was wondering if there was a difference between =+ and += (and other assignment operators too). I tried and both did the same thing. So is there a difference or is there a convention? Do both work because my compilers dont check for standarts?

Edit: I made a mistake. I used bad inputs during my testing which led me to thinking they are both doing the same thing. Turns out they are two different things.

+= adds rvalue to lvalue

=+ assigns rvalue to lvalue

  • assignment-operator
  • compound-assignment

Michael's user avatar

  • 16 There is no =+ operator in C. –  Eugene Sh. Commented Jan 12, 2017 at 15:07
  • 6 I works as two different operators. Assignment and unary + . –  Eugene Sh. Commented Jan 12, 2017 at 15:08
  • 9 @EugeneSh.: strictly, there is no longer a =+ operator in C. It ceased to be a part of C in the mid-70s. Note that =+ , =- , =& can both appear in modern C — even =* if the term following is a pointer. Most of the others can't. However, the meaning is of the two separate operators; the fact that they're touching is immaterial. –  Jonathan Leffler Commented Jan 12, 2017 at 15:13
  • 2 If you have: int main(void) { int i = 2, j = 3; i =+ j; printf("%d\n", i); return 0; } , do you get 3 or 5 printed? Standard C says it should be 3. Even I've never worked with a compiler that gives a different result — the change from the original =+ to += occurred years before I started coding in C. –  Jonathan Leffler Commented Jan 12, 2017 at 15:16
  • 1 @SunggukLim: There's nothing surprising about that. =+ is two operators, = and + . See my answer for details. –  Keith Thompson Commented May 31, 2018 at 17:48

In modern C, or even moderately ancient C, += is a compound assignment operator, and =+ is parsed as two separate tokens. = and + . Punctuation tokens are allowed to be adjacent.

So if you write:

it's equivalent to

except that x is only evaluated once (which can matter if it's a more complicated expression).

If you write:

then it's parsed as

and the + is a unary plus operator.

Very early versions of C (around the mid 1970s, before the publication of K&R1 in 1978) used different symbols for compound assignments. Where modern C uses += , early C used =+ . Early C had no unary + operator, but it did have a unary - operator, and the use of =- caused problems; programmers would write x=-y intending it to mean x = -y , but it was silently interpreted as x =- y . The language was changed some time between 1975 and 1978 to avoid that problem. As late as 1999, I worked with a compiler (VAXC on VMS) that would warn about an ambiguous use of =- , but would use the older meaning. That shouldn't be a concern now unless you're a hobbyist playing with some very old software and/or hardware.

(A 1975 C Reference Manual shows the old =- , =+ , et al forms of the compound assignment operators. The first edition of The C Programming Language by Kernighan and Ritchie, published in 1978, shows the modern -= , += , et al, but mentions the older forms under "Anachronisms".)

Keith Thompson's user avatar

  • 2 To this day I still put a space after the = so a = -7; is not misinterpreted as a -= 7; . –  chux - Reinstate Monica Commented Mar 28, 2020 at 22:48
  • 1 @chux-ReinstateMonica So do I, but only because I find it more legible. –  Keith Thompson Commented Mar 28, 2020 at 23:16

Not the answer you're looking for? Browse other questions tagged c assignment-operator compound-assignment or ask your own question .

  • The Overflow Blog
  • The creator of Jenkins discusses CI/CD and balancing business with open source
  • The evolution of full stack engineers
  • Featured on Meta
  • Bringing clarity to status tag usage on meta sites
  • Join Stack Overflow’s CEO and me for the first Stack IRL Community Event in...
  • Feedback requested: How do you use tag hover descriptions for curating and do...
  • Staging Ground Reviewer Motivation
  • What does a new user need in a homepage experience on Stack Overflow?

Hot Network Questions

  • The quest for a Wiki-less Game
  • Problem in solving an integral equation.
  • Getting lost on a Circular Track
  • What exactly was Teddy KGB's tell that Mike identified?
  • The Chofetz Chaim's Shocking View of Baalei Teshuva
  • Parsing and processing "resolvectl statistics" output using awk
  • How long should a wooden construct burn (and continue to take damage) until it burns out (and stops doing damage)
  • Humans are forbidden from using complex computers. But what defines a complex computer?
  • Could they free up a docking port on ISS by undocking the emergency vehicle and letting it float next to the station for a little while
  • Inspector tells me that the electrician should have removed green screw from the panel
  • Inertia Action on Kummer Sheaves
  • Is this map real?
  • What is the least number of colours Peter could use to color the 3x3 square?
  • How to delete members of a list by rule
  • Children's book about intelligent bears or maybe cats
  • Did Queen (or Freddie Mercury) really not like Star Wars?
  • Why wasn't Randall Stevens caught?
  • What are the steps to write a book?
  • Does innate sorcery apply to every attack from storm sphere?
  • I want to be a observational astronomer, but have no idea where to start
  • What's the best format or way to generate a short-lived access token?
  • Can the planet Neptune be seen from Earth with binoculars?
  • How solid is the claim that Alfred Nobel found the Nobel Prize specifically because of his invention of dynamite?
  • Electromagnetic Eigenvalue problem in FEM yielding spurious solutions

assignment operator in c programming

IncludeHelp_logo

  • Data Structure
  • Coding Problems
  • C Interview Programs
  • C++ Aptitude
  • Java Aptitude
  • C# Aptitude
  • PHP Aptitude
  • Linux Aptitude
  • DBMS Aptitude
  • Networking Aptitude
  • AI Aptitude
  • MIS Executive
  • Web Technologie MCQs
  • CS Subjects MCQs
  • Databases MCQs
  • Programming MCQs
  • Testing Software MCQs
  • Digital Mktg Subjects MCQs
  • Cloud Computing S/W MCQs
  • Engineering Subjects MCQs
  • Commerce MCQs
  • More MCQs...
  • Machine Learning/AI
  • Operating System
  • Computer Network
  • Software Engineering
  • Discrete Mathematics
  • Digital Electronics
  • Data Mining
  • Embedded Systems
  • Cryptography
  • CS Fundamental
  • More Tutorials...
  • Tech Articles
  • Code Examples
  • Programmer's Calculator
  • XML Sitemap Generator
  • Tools & Generators

IncludeHelp

  • C - Getting Started
  • C - Overview
  • C - Advantages & Disadvantages
  • C - Character Set
  • Gcc Vs. G++
  • Why We should Use C?
  • C - Basic Rules
  • C - Comments
  • C - Variable Naming Conventions
  • C - Variable Initialization
  • C - Constants
  • C - Character Constant
  • C - Octal Literals
  • C - Hexadecimal Literals
  • C - Automatic (auto) Variables
  • Local Vs. Global Variables
  • C - Access Global Variables
  • Is exit() & return Statements are Same?
  • C - Print Float Value
  • C - Print Multiple Lines Using printf()
  • C - Argument Index Specification
  • C - Value Returned by scanf()
  • C - Returned Values of printf() & scanf()
  • What do 'lvalue' & 'rvalue' Mean
  • Automatic (auto) Vs. Static Variables

C Data Types

  • C - Data Types & Operators
  • C - Basic Data Types
  • C - 'unsigned char' For Memory Optimization
  • short Vs. short int Vs. int
  • unsigned int Vs. size_t
  • C - Storage Classes Introduction
  • C - Storage Classes With Examples
  • C- Type Conversion
  • C - Type Qualifiers

C Input/Output

  • C - Read String With Spaces
  • C - Input String of Unknown Length
  • C - Disadvantages of scanf()
  • C - scanf() need '%lf' for doubles, when printf() is okay with just '%f'
  • C - Format Specifier for unsigned short int
  • C - printf() Format Specifier for bool
  • C - printf() Arguments for long
  • C - printf() Specifier for double
  • Is there a printf() converter to print in binary format?
  • C - Nested printf()
  • printf() Vs. puts()
  • printf() Vs. sprintf()
  • %d Vs. %i format Specifiers
  • C - Single Character Input & Output
  • C- Formatted Input & Output
  • C - Octal & Hex Escape Sequences
  • C - Convert Float to String
  • gets() Vs. fgets()
  • C - Input Unsigned Integer Value
  • C - Input Octal Value
  • C - Input Hex Value
  • C - Input Decimal, Octal & Hex in char Variables
  • C - Input With '%i'
  • C - Input Individual Characters
  • C - Skip characters While Reading Integers
  • C - Read Memory Address
  • C - Printing Variable's Address
  • C - printf() Examples & Variations

C Operators

  • C - Operators Precedence & Associativity
  • Operators Vs. Operands
  • C - Unary Operators
  • C - Equality Operators
  • C - Logical AND (&&) Operator
  • C - Logical OR (||) Operator
  • C - Logical NOT (!) Operator
  • C - Modulus on Negative Numbers
  • C - Expression a=b=c (Multiple Assignment) Evaluates
  • C - Expression a==b==c (Multiple Comparison) Evaluates
  • C - Complex Return Statement Using Comma Operator
  • C - Comma Operator
  • C - Bitwise Operators
  • C - Bitwise One's Compliment
  • C - Modulus of Float or Double Numbers

C Conditional Statements

  • C - If Else Statements
  • C - Switch Case
  • C - Switch Statements Features, Disadvantages
  • C - Using Range With Switch

C Control Statements

  • C - 'goto' Statement
  • C - break & continue
  • Print Numbers From 1 to N Using goto
  • C - Looping
  • C - Looping Programs
  • C - Nested Loops
  • C - Entry Controlled Vs. Exit Controlled Loops
  • C - Sentinel Vs. Counter Controlled Loops
  • C - Use for Loop as Infinite Loop
  • C - Strings in C language programming
  • C - string.h Functions
  • C - memcpy() Function
  • C - Write Your Own memcpy()
  • C - memset() Function
  • C - Write Your Own memset()

C Functions

  • C - Library & User-define Functions
  • C - Static Functions
  • C - Scope of Function Parameters
  • C - Recursion
  • C - Recursion Examples
  • More on Arrays
  • C - Properties/Characteristics of Array

C Structure and Unions

  • C Structures
  • C - Initialize a Structure in Accordance
  • C - Size of Structure With No Members
  • C -Pointer to Structure
  • C - Nested Structure Initialization
  • C - Nested Structure With Examples
  • C - Size of Structure
  • C - Copy Complete Structure in Byte Array
  • C - Pointer to Union
  • C - Pointers
  • C - Pointer Rules
  • C - Pointers Declarations
  • C - Pointer Address Operators
  • C - Accessing Variable Using Pointer
  • C - Address of (&) & Dereference (*) Operators
  • C - NULL Pointer
  • C - Pointers as Argument
  • C - Pointer Arithmetic
  • C - Pointer to an Array
  • C - Evaluation of Statement '*ptr++'
  • C - Pointer & Non-pointer Variables Declarations Together
  • C - Pointer to an Array of Integers
  • C - Pointer to Pointer
  • C - void Pointer as Function Argument
  • char s[] Vs. char *s
  • C - Copying Integer Value to Char Buffer & Vice Versa
  • C - Call by Reference Vs. Call by Value
  • C - Typedef Function Pointer

C Preprocessor Directives

  • Recommendation for defining a macro in C language
  • Macro expansion directives (#define, #undef) in C language
  • Complex macro with arguments (function like macro) in C language
  • C language #ifdef, #else, #endif Pre-processor with Example
  • C language #if, #elif, #else, #endif Pre-processor with Example
  • Parameterized Macro - we cannot use space after the Macro Name
  • Stringizing Operator (#) in C
  • Token Pasting Directive Operator (##) in C

C Command-line Arguments

  • C language Command Line Arguments

C File Handlings

  • C - Basics of File Handling
  • C - File Handling Programs
  • C - Graphics Modes
  • C - Using Colors in Text Mode
  • C - More on Graphics Modes
  • C - OUTTEXTXY() & SETTEXTSTYLE() Functions
  • C - Draw Circle & Rectangle
  • C - graphics.h Functions
  • C - More Graphics-related Interesting Functions

C Advance Topics

  • C - Process Identification (pid_t)
  • C - getpid() & getppid()
  • C - Rrules For Writing C/C++ Program
  • C - Tips For Embedded Development
  • C - Optimization Techniques
  • C Vs. Embedded C
  • C- File Management System Calls
  • C/C++ Multithreading
  • C/C++ - Sum of Array Using Multithreading

C Tips and Tricks

  • C - Copy Two Bytes Int. to Byte Buffer
  • C - Is Pre-increment Faster than Post-increment?
  • C - Create Delay Function
  • Why should We use 'f' With Float Literal in C?
  • C - Replacing a Part of String
  • C- Comparing Number of Characters of Two Strings
  • C - Safest Way to Check Value Using ==
  • C- Check EVEN or ODD W/O % Operator
  • C- Use Single Byte to Store 8 Values
  • C- Funny Trick to Use C++ in C
  • C - Trick to Print Maximum Value of an unsigned int
  • C - Print Maximum Value of an unsigned int using One's Compliment (~) Operator in C
  • Why we should use switch instead of if else?
  • How to initialize array elements with hexadecimal values in C?
  • How should we declare/define a pointer variable?

C Important Topics

  • C - Working With Hexadecimal
  • C - Working With octal
  • C - Convert ASCII String (char[]) to BYTE Array
  • C - Convert ASCII String (char[]) to Octal String
  • C - Convert ASCII String (char[]) to Hex String
  • C - Assign Binary Calue in a Variable Directly
  • C - Check Particular Bit is SET or Not
  • C- Set, Clear, & Toggle a Bit
  • C - Value of 'EOF'
  • C - Print printf("Hello world."); Using printf()
  • C - Print Text in New Line w/O Using '\n'
  • C - Return 0 From int main()
  • 'Super Loop' Architecture for Embedded C
  • C - Executing System Commands
  • C - Infix To Postfix Conversion Using Stack
  • C - Evaluation of Postfix Expressions Using Stack
  • C - Polynomial Addition Using Structure
  • C - conio.h Functions
  • SQLite with C language
  • C - SQL Table Creation, Insertion
  • C - Aptitude Questions
  • C - Interview Questions
  • C - Find Output Programs

Advertisement

Home » C programming

What is the difference between = (Assignment) and == (Equal to) operators in C?

Difference between assignment (=) vs equal to (==) operators in c.

Many times this question arises what is the difference between = and == operators in C programming language? Here we are going to tell you exactly what the differences between these two operators are.

Assignment Operator (=)

= is an Assignment Operator in C, C++ and other programming languages, It is Binary Operator which operates on two operands.

= assigns the value of right side expression’s or variable’s value to the left side variable.

Let's understand by example:

Here, When first expression evaluates value of (a+b) will be assigned into x and in second expression y=x; value of variable x will be assigned into y .

Equal To Operator (==)

== is an Equal To Operator in C and C++ only, It is Binary Operator which operates on two operands.

== compares value of left and side expressions, return 1 if they are equal other will it will return 0.

When expression x==y evaluates, it will return 1 (it means condition is TRUE ) and "TRUE" will print.

So it's cleared now, , both are not same , = is an Assignment Operator it is used to assign the value of variable or expression, while == is an Equal to Operator and it is a relation operator used for comparison (to compare value of both left and right side operands).

Comments and Discussions!

Load comments ↻

  • Marketing MCQs
  • Blockchain MCQs
  • Artificial Intelligence MCQs
  • Data Analytics & Visualization MCQs
  • Python MCQs
  • C++ Programs
  • Python Programs
  • Java Programs
  • D.S. Programs
  • Golang Programs
  • C# Programs
  • JavaScript Examples
  • jQuery Examples
  • CSS Examples
  • C++ Tutorial
  • Python Tutorial
  • ML/AI Tutorial
  • MIS Tutorial
  • Software Engineering Tutorial
  • Scala Tutorial
  • Privacy policy
  • Certificates
  • Content Writers of the Month

Copyright © 2024 www.includehelp.com. All rights reserved.

  • C - Introduction
  • C - Comments
  • C - Data Types
  • C - Type Casting
  • C - Operators
  • C - Strings
  • C - Booleans
  • C - If Else
  • C - While Loop
  • C - For Loop
  • C - goto Statement
  • C - Continue Statement
  • C - Break Statement
  • C - Functions
  • C - Scope of Variables
  • C - Pointers
  • C - Typedef
  • C - Format Specifiers
  • C Standard Library
  • C - Data Structures
  • C - Examples
  • C - Interview Questions

AlphaCodingSkills

Facebook Page

  • Programming Languages
  • Web Technologies
  • Database Technologies
  • Microsoft Technologies
  • Python Libraries
  • Data Structures
  • Interview Questions
  • PHP & MySQL
  • C++ Standard Library
  • Java Utility Library
  • Java Default Package
  • PHP Function Reference

C - Bitwise OR and assignment operator

The Bitwise OR and assignment operator (|=) assigns the first operand a value equal to the result of Bitwise OR operation of two operands.

(x |= y) is equivalent to (x = x | y)

The Bitwise OR operator (|) is a binary operator which takes two bit patterns of equal length and performs the logical OR operation on each pair of corresponding bits. It returns 1 if either or both bits at the same position are 1, else returns 0.

Bit_1Bit_2Bit_1 | Bit_2
000
101
011
111

The example below describes how bitwise OR operator works:

The code of using Bitwise OR operator (|) is given below:

The output of the above code will be:

Example: Find largest power of 2 less than or equal to given number

Consider an integer 1000. In the bit-wise format, it can be written as 1111101000. However, all bits are not written here. A complete representation will be 32 bit representation as given below:

Performing N |= (N>>i) operation, where i = 1, 2, 4, 8, 16 will change all right side bit to 1. When applied on 1000, the result in 32 bit representation is given below:

Adding one to this result and then right shifting the result by one place will give largest power of 2 less than or equal to 1000.

The below code will calculate the largest power of 2 less than or equal to given number.

The above code will give the following output:

AlphaCodingSkills Android App

  • Data Structures Tutorial
  • Algorithms Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQLi Tutorial
  • Java Tutorial
  • Scala Tutorial
  • C++ Tutorial
  • C# Tutorial
  • PHP Tutorial
  • MySQL Tutorial
  • SQL Tutorial
  • PHP Function reference
  • C++ - Standard Library
  • Java.lang Package
  • Ruby Tutorial
  • Rust Tutorial
  • Swift Tutorial
  • Perl Tutorial
  • HTML Tutorial
  • CSS Tutorial
  • AJAX Tutorial
  • XML Tutorial
  • Online Compilers
  • QuickTables
  • NumPy Tutorial
  • Pandas Tutorial
  • Matplotlib Tutorial
  • SciPy Tutorial
  • Seaborn Tutorial
  • 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.

Please Login to comment...

Similar reads.

  • Geeks Premier League
  • Geeks Premier League 2023
  • Best Twitch Extensions for 2024: Top Tools for Viewers and Streamers
  • Discord Emojis List 2024: Copy and Paste
  • Best Adblockers for Twitch TV: Enjoy Ad-Free Streaming in 2024
  • PS4 vs. PS5: Which PlayStation Should You Buy in 2024?
  • Full Stack Developer Roadmap [2024 Updated]

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

COMMENTS

  1. Assignment Operators in C

    Assignment Operators in C

  2. Assignment Operators in C

    Assignment Operators in C

  3. Assignment Operators in Programming

    Assignment operators are used in programming to assign values to variables. We use an assignment operator to store and update data within a program. They enable programmers to store data in variables and manipulate that data. The most common assignment operator is the equals sign (=), which assigns the value on the right side of the operator to ...

  4. Assignment Operators in C

    The Assignment operators in C are some of the Programming operators that are useful for assigning the values to the declared variables. Equals (=) operator is the most commonly used assignment operator. For example: int i = 10; The below table displays all the assignment operators present in C Programming with an example. C Assignment Operators.

  5. 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 ...

  6. C Programming Assignment Operators

    Assignment Operators in C are used to assign values to the variables. The left side operand is called a variable and the right side operand is the value. The value on the right side of the "=" is assigned to the variable on the left side of "=". In this C tutorial, we'll understand the types of C programming assignment operators with examples.

  7. C Assignment Operators

    C Assignment Operators

  8. Assignment and shorthand assignment operator in C

    Assignment and shorthand assignment operator in C

  9. Assignment Operators in C with Examples

    C supports following Assignment operators: 1. Simple Assignment = Operator Example. This is one of the simplest assignment operator, it simply assigns the right side value to the left side operand. #include <stdio.h> int main () { int n; //integer variable char ch; //character variable float f; //float variable // Simple assignment operator to ...

  10. Assignment operators

    Assignment operators

  11. Assignment Operator in C

    The assignment operator ( = ) is used to assign a value to the variable. Its general format is as follows: variable = right_side. The operand on the left side of the assignment operator must be a variable and operand on the right-hand side must be a constant, variable or expression. Here are some examples:

  12. 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.

  13. C Programming Assignment Operators

    In C programming, assignment operators assign values to variables. Programmers use "=" for this. The left side is a variable, the right side a value or expression. The "=" operator in C assigns the right value to the left variable. For instance, "x = 5;" sets x as 5.

  14. 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 ...

  15. Assignment Operator in C

    Assignment Operator in C

  16. C Operators

    C Operators - W3Schools ... C Operators

  17. Operators in C

    Operators in C

  18. Operators in C Language (Explained All Types With Symbols)

    Assignment Operators. Misc Operator. 1. Arithmetic Operators. ... An expression with logical operators or symbols in C programming returns a 0 or 1 result based on whether the condition is true or false. For example, when we use the logical AND operator '&&' in C language, it returns true if both conditions are satisfied; else, it returns ...

  19. Assignment Operator in C

    Using assignment operators, we can assign value to the variables. Equality sign (=) is used as an assignment operator in C. Here, value 5 has assigned to the variable var. int a = 10 ; int b = a; printf( "a = %d \t b = %d\n" ,a,b); return 0 ; Here, value of a has assigned to the variable b. Now, both a and b will hold value 10.

  20. What is the difference between += and =+ C assignment operators

    In modern C, or even moderately ancient C, += is a compound assignment operator, and =+ is parsed as two separate tokens. = and +. Punctuation tokens are allowed to be adjacent. So if you write: x += y; it's equivalent to. x = x + y; except that x is only evaluated once (which can matter if it's a more complicated expression).

  21. Difference between = (Assignment) and == (Equal to) operators in C

    = is an Assignment Operator in C, C++ and other programming languages, It is Binary Operator which operates on two operands. = assigns the value of right side expression's or variable's value to the left side variable. Let's understand by example: x = (a + b); y = x;

  22. C Bitwise OR and assignment operator

    The Bitwise OR and assignment operator (|=) assigns the first operand a value equal to the result of Bitwise OR operation of two operands. The Bitwise OR operator (|) is a binary operator which takes two bit patterns of equal length and performs the logical OR operation on each pair of corresponding bits. It returns 1 if either or both bits at ...

  23. Assignment Operators In C++

    Assignment Operators In C