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 −

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.
  • Trending Now
  • Foundational Courses
  • Data Science
  • Practice Problem
  • Machine Learning
  • System Design
  • DevOps Tutorial

Assignment Operators in Programming

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

Table of Content

What are Assignment Operators?

  • Types of Assignment Operators
  • Assignment Operators in C
  • Assignment Operators in C++
  • Assignment Operators in Java
  • Assignment Operators in Python
  • Assignment Operators in C#
  • Assignment Operators in JavaScript
  • Application of Assignment Operators

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 the variable on the left side.

Types of Assignment Operators:

  • Simple Assignment Operator ( = )
  • Addition Assignment Operator ( += )
  • Subtraction Assignment Operator ( -= )
  • Multiplication Assignment Operator ( *= )
  • Division Assignment Operator ( /= )
  • Modulus Assignment Operator ( %= )

Below is a table summarizing common assignment operators along with their symbols, description, and examples:

OperatorDescriptionExamples
= (Assignment)Assigns the value on the right to the variable on the left.  assigns the value 10 to the variable x.
+= (Addition Assignment)Adds the value on the right to the current value of the variable on the left and assigns the result to the variable.  is equivalent to 
-= (Subtraction Assignment)Subtracts the value on the right from the current value of the variable on the left and assigns the result to the variable.  is equivalent to 
*= (Multiplication Assignment)Multiplies the current value of the variable on the left by the value on the right and assigns the result to the variable.  is equivalent to 
/= (Division Assignment)Divides the current value of the variable on the left by the value on the right and assigns the result to the variable.  is equivalent to 
%= (Modulo Assignment)Calculates the modulo of the current value of the variable on the left and the value on the right, then assigns the result to the variable.  is equivalent to 

Assignment Operators in C:

Here are the implementation of Assignment Operator in C language:

Assignment Operators in C++:

Here are the implementation of Assignment Operator in C++ language:

Assignment Operators in Java:

Here are the implementation of Assignment Operator in java language:

Assignment Operators in Python:

Here are the implementation of Assignment Operator in python language:

Assignment Operators in C#:

Here are the implementation of Assignment Operator in C# language:

Assignment Operators in Javascript:

Here are the implementation of Assignment Operator in javascript language:

Application of Assignment Operators:

  • Variable Initialization : Setting initial values to variables during declaration.
  • Mathematical Operations : Combining arithmetic operations with assignment to update variable values.
  • Loop Control : Updating loop variables to control loop iterations.
  • Conditional Statements : Assigning different values based on conditions in conditional statements.
  • Function Return Values : Storing the return values of functions in variables.
  • Data Manipulation : Assigning values received from user input or retrieved from databases to variables.

Conclusion:

In conclusion, assignment operators in programming are essential tools for assigning values to variables and performing operations in a concise and efficient manner. They allow programmers to manipulate data and control the flow of their programs effectively. Understanding and using assignment operators correctly is fundamental to writing clear, efficient, and maintainable code in various programming languages.

Please Login to comment...

Similar reads.

  • Programming
  • How to Delete Discord Servers: Step by Step Guide
  • Google increases YouTube Premium price in India: Check our the latest plans
  • California Lawmakers Pass Bill to Limit AI Replicas
  • Best 10 IPTV Service Providers in Germany
  • Content Improvement League 2024: From Good To A Great Article

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

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

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

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
Filling Fast
Filling Fast

About Author

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

Nested loop in c++ example.

September 1, 2024

Understanding Object-Oriented Programming in C++

Sequential search arrays in c++.

© 2024 ProgrammingHouse

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

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

how to use 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.

how to use 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

how to use 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:

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

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

Learn C practically and Get Certified .

Popular Tutorials

Popular examples, reference materials, learn c interactively, c introduction.

  • Getting Started with C
  • Your First C Program

C Fundamentals

  • C Variables, Constants and Literals
  • C Data Types
  • C Input Output (I/O)

C Programming Operators

C flow control.

C if...else Statement

  • C while and do...while Loop
  • C break and continue
  • C switch Statement
  • C goto Statement
  • C Functions
  • C User-defined functions
  • Types of User-defined Functions in C Programming
  • C Recursion
  • C Storage Class

C Programming Arrays

  • C Multidimensional Arrays
  • Pass arrays to a function in C

C Programming Pointers

  • Relationship Between Arrays and Pointers
  • C Pass Addresses and Pointers
  • C Dynamic Memory Allocation
  • C Array and Pointer Examples
  • C Programming Strings
  • String Manipulations In C Programming Using Library Functions
  • String Examples in C Programming

C Structure and Union

  • C structs and Pointers
  • C Structure and Function

C Programming Files

  • C File Handling
  • C Files Examples

C Additional Topics

  • C Keywords and Identifiers

C Precedence And Associativity Of Operators

C Bitwise Operators

  • C Preprocessor and Macros
  • C Standard Library Functions

C Tutorials

Bitwise Operators in C Programming

  • Compute Quotient and Remainder
  • Find the Size of int, float, double and char
  • Make a Simple Calculator Using switch...case

An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform addition.

C has a wide range of operators to perform various operations.

C Arithmetic Operators

An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).

Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
% remainder after division (modulo division)

Example 1: Arithmetic Operators

The operators + , - and * computes addition, subtraction, and multiplication respectively as you might have expected.

In normal calculation, 9/4 = 2.25 . However, the output is 2 in the program.

It is because both the variables a and b are integers. Hence, the output is also an integer. The compiler neglects the term after the decimal point and shows answer 2 instead of 2.25 .

The modulo operator % computes the remainder. When a=9 is divided by b=4 , the remainder is 1 . The % operator can only be used with integers.

Suppose a = 5.0 , b = 2.0 , c = 5 and d = 2 . Then in C programming,

C Increment and Decrement Operators

C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1.

Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.

Example 2: Increment and Decrement Operators

Here, the operators ++ and -- are used as prefixes. These two operators can also be used as postfixes like a++ and a-- . Visit this page to learn more about how increment and decrement operators work when used as postfix .

C Assignment Operators

An assignment operator is used for assigning a value to a variable. The most common assignment operator is =

Operator Example Same as
= a = b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b

Example 3: Assignment Operators

C relational operators.

A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.

Relational operators are used in decision making and loops .

Operator Meaning of Operator Example
== Equal to is evaluated to 0
> Greater than is evaluated to 1
< Less than is evaluated to 0
!= Not equal to is evaluated to 1
>= Greater than or equal to is evaluated to 1
<= Less than or equal to is evaluated to 0

Example 4: Relational Operators

C logical operators.

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming .

Operator Meaning Example
&& Logical AND. True only if all operands are true If c = 5 and d = 2 then, expression equals to 0.
|| Logical OR. True only if either one operand is true If c = 5 and d = 2 then, expression equals to 1.
! Logical NOT. True only if the operand is 0 If c = 5 then, expression equals to 0.

Example 5: Logical Operators

Explanation of logical operator program

  • (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).
  • (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
  • (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
  • (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
  • !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
  • !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).

During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power.

Bitwise operators are used in C programming to perform bit-level operations.

Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right

Visit bitwise operator in C to learn more.

Other Operators

Comma operator.

Comma operators are used to link related expressions together. For example:

The sizeof operator

The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).

Example 6: sizeof Operator

Other operators such as ternary operator ?: , reference operator & , dereference operator * and member selection operator  ->  will be discussed in later tutorials.

Table of Contents

  • Arithmetic Operators
  • Increment and Decrement Operators
  • Assignment Operators
  • Relational Operators
  • Logical Operators
  • sizeof Operator

Video: Arithmetic Operators in C

Sorry about that.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks .

Learn and improve your coding skills like never before.

  • Interactive Courses
  • Certificates
  • 2000+ Challenges

Related Tutorials

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 Operators in C

C++ Course: Learn the Essentials

Operators are a fundamental part of all the computations that computers perform. Today we will learn about one of them known as Assignment Operators in C. Assignment Operators are used to assign values to variables. The most common assignment operator is = . Assignment Operators are Binary Operators.

Types of Assignment Operators in C

LHS and RHS Operands

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

  • basic assignment ( = )
  • subtraction assignment ( -= )
  • addition assignment ( += )
  • division assignment ( /= )
  • multiplication assignment ( *= )
  • modulo assignment ( %= )
  • bitwise XOR assignment ( ^= )
  • bitwise OR assignment ( |= )
  • bitwise AND assignment ( &= )
  • bitwise right shift assignment ( >>= )
  • bitwise left shift assignment ( <<= )

Working of Assignment Operators in C

This is the complete list of all assignment operators in C. To read the meaning of operator please keep in mind the above example.

OperatorMeaning Of OperatorExampleSame as
=Simple assignment operatorx=yx=y
+=Add left operand to right operand then assign result to left operandx+=yx=x+y
-=subtract right operand from left operand then assign result to left operandx-=yx=x-y
*=multiply left operand with right operand then assign result to left operandx*=yx=x*y
/=divide left operand with right operand then assign result to left operandx/=yx=x/y
%=take modulus left operand with right operand then assigned result in left operandx%=yx=x%y
<<=Left Shift Assignment Operator means the left operand is left shifted by right operand value and assigned value to left operandx<<=yx=x<<y
>>=Right shift Assignment Operator means the left operand is right shifted by right operand value and assigned value to left operandx>>=yx=x>>y
&=Bitwise AND Assignment Operator means does AND on every bit of left operand and right operand and assigned value to left operandx&=yx=x&y
|=Bitwise inclusive OR Assignment Operator means does OR on every bit of left operand and right operand and assigned value to left operandx|=yx=x|y
^=Bitwise exclusive OR Assignment Operator means does XOR on every bit of left operand and right operand and assigned value to left operandx^=yx=x^y

Example for Assignment Operators in C

Basic assignment ( = ) :

Subtraction assignment ( -= ) :

Addition assignment ( += ) :

Division assignment ( /= ) :

Multiplication assignment ( *= ) :

Modulo assignment ( %= ) :

Bitwise XOR assignment ( ^= ) :

Bitwise OR assignment ( |= ) :

Bitwise AND assignment ( &= ) :

Bitwise right shift assignment ( >>= ) :

Bitwise left shift assignment ( <<= ) :

This is the detailed explanation of all the assignment operators in C that we have. Hopefully, This is clear to you.

Practice Problems on Assignment Operators in C

1. what will be the value of a after the following code is executed.

A) 10 B) 11 C) 12 D) 15

Answer – C. 12 Explanation: a starts at 10, increases by 5 to 15, then decreases by 3 to 12. So, a is 12.

2. After executing the following code, what is the value of num ?

A) 4 B) 8 C) 16 D) 32

Answer: C) 16 Explanation: After right-shifting 8 (binary 1000) by one and then left-shifting the result by two, the value becomes 16 (binary 10000).

Q. How does the /= operator function? Is it a combination of two other operators?

A. The /= operator is a compound assignment operator in C++. It divides the left operand by the right operand and assigns the result to the left operand. It is equivalent to using the / operator and then the = operator separately.

Q. What is the most basic operator among all the assignment operators available in the C language?

A. The most basic assignment operator in the C language is the simple = operator, which is used for assigning a value to a variable.

  • Assignment operators are used to assign the result of an expression to a variable.
  • There are two types of assignment operators in C. Simple assignment operator and compound assignment operator.
  • Compound Assignment operators are easy to use and the left operand of expression needs not to write again and again.
  • They work the same way in C++ as in C.

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

=sum = 101;101 is assigned to variable sum
+=sum += 101; This is same as sum = sum + 101
-=sum -= 101; This is same as sum = sum – 101
*=sum *= 101; This is same as sum = sum * 101
/=sum /= 101; This is same as sum = sum/101
%=sum %= 101; This is same as sum = sum % 101
&=sum&=101; This is same as sum = sum & 101
^=sum ^= 101; This is same as sum = sum ^ 101

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.

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

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

Freshers.in

Igniting the Spark of Knowledge

Demystifying Assignment Operators in C Programming

C Programming @ Freshers.in

Assignment operators are fundamental in C programming, as they allow you to assign values and manipulate variables efficiently. In this article, we’ll explore the basics of C programming’s assignment operators: = (Assignment), += (Add and assign), -= (Subtract and assign), *= (Multiply and assign), /= (Divide and assign), and %= (Modulus and assign). Through real-world examples and output demonstrations, you’ll gain a solid understanding of how to use these operators effectively in your C programs. Assignment operators are vital tools in C programming, allowing you to manipulate variables and perform calculations efficiently. In this article, we will explore the basic assignment operators (=, +=, -=, *=, /=, and %=) with real-world examples and output demonstrations.

Assignment Operator (=)

The assignment operator ( = ) is used to assign a value to a variable. It’s the simplest assignment operator. Let’s illustrate this with an example:

Add and Assign (+=)

The += operator is used to add a value to the current value of a variable and assign the result back to the variable. Here’s an example:

Subtract and Assign (-=)

The -= operator is used to subtract a value from the current value of a variable and assign the result back to the variable. Let’s see it in action:

Multiply and Assign (*=)

The *= operator is used to multiply the current value of a variable by a value and assign the result back to the variable. Here’s an example:

Divide and Assign (/=)

The /= operator is used to divide the current value of a variable by a value and assign the result back to the variable. Let’s demonstrate it:

Modulus and Assign (%=)

The %= operator is used to calculate the modulus of the current value of a variable and a value and assign the result back to the variable. Here’s an example:

' src=

Related Articles

C Programming @ Freshers.in

DBT Python Numpy PySpark Hive Snowflake Redshift Airflow Aptitude

Recent Posts

Featured posts – slider widget, how partition by works in snowflake, and sql in general.

how to use assignment operator in c programming

Stash a specific file using Git

Learn Python @ Freshers.in

Prevent your computer from locking : Python to simulate mouse movements

aws logo @ Freshers.in

AWS EC2 vs Azure Virtual Machines

Production and industrial engineering, engineering technical campus placement question and answers.

Java Script @ Freshers.in

JavaScript’s reduceRight() method to iterate over an array from right to left

python @ Freshers.in

Merging Multiple Images into a Single PDF File Using Python

Nanotechnology, electronics and instrumentation, most viewed posts.

  • dbt (data build tool) interview questions
  • Python throwing as NameError: name ‘__file__’ is not defined – Solution
  • DBT command not found after intalling DBT-How to resolve.
  • BigQuery : Handle missing or null values in BigQuery
  • Airflow dags not getting refreshed/updating. How to do it manually?
  • How to delete a partition data as well from Hive external table on DROP command?
  • PySpark : Connecting and updating postgres table in spark SQL

Copyright © 2024 Freshers.in

how to use assignment operator in c programming

😶 Operators

Python operators are the symbols that allow us to perform different types of operations on variables and values. They are the building blocks of any programming language, and Python is no exception. Python provides a wide range of operators that can be used to perform arithmetic, logical, comparison, assignment, and bitwise operations.

Understanding the different types of operators is crucial to writing efficient and error-free code in Python. In this section, we will explore the different types of operators available in Python and learn how to use them effectively in our programs. So buckle up and get ready to dive into the world of Python operators!

I. Arithmetic Operators

Arithmetic operators are used in Python to perform basic arithmetic operations such as addition, subtraction, multiplication, division, and more. These operators are used on numeric data types such as integers, floats, and complex numbers.

Python provides the following arithmetic operators:

OperatorNameExampleResult

The floor division (//) operator returns the largest integer that is less than or equal to the division result.

a. Addition

Addition is one of the most basic arithmetic operations in Python. It is denoted by the + symbol and is used to add two numbers or concatenate two strings. For example, if we want to add two numbers x and y together, we can use the + operator like this:

Similarly, if we want to concatenate two strings a and b , we can use the + operator like this:

In both cases, the + operator performs the desired operation and returns a new value that we can assign to a variable or use directly.

b. Subtraction

The subtraction operator (-) is used to subtract one value from another. It takes two operands and returns the difference between them. For example, 5 - 3 will return 2, and 10.5 - 3.2 will return 7.3.

In Python, the subtraction operator can also be used with variables. For example:

Note that the subtraction operator can also be used with negative numbers. For example, 5 - (-3) will return 8.

c. Multiplication

Multiplication is a mathematical operation that is represented by the symbol * in Python. It is used to find the product of two or more values. Here's an example:

In the above example, we have two variables a and b with values 10 and 5 respectively. We multiply these two variables using the * operator and store the result in the variable c . Finally, we print the value of c which is 50 (the product of a and b ).

d. Division

In Python, the / operator is used for division. It returns the quotient (result of division) in the form of a float, even if both the operands are integers. If you want to get the quotient as an integer, you can use the // operator, which performs floor division.

Here's an example:

In the example above, we divide a by b using both the / and // operators. The result of the floating point division is stored in c , which is a float, while the result of the integer division is stored in d , which is an integer.

Modulus operator returns the remainder of the division operation between two operands. It is represented by the percentage sign % .

For example, the expression 9 % 4 returns 1 because when 9 is divided by 4, the remainder is 1.

Here is an example code snippet:

e. Exponentiation

Exponentiation is another arithmetic operator in Python represented by the double asterisk symbol (**). It raises the first operand to the power of the second operand.

Here, the base is the first operand, and the exponent is the second operand.

In the above example, 2 is raised to the power of 3, which results in 8.

f. Floor Division

Floor Division operator in Python is represented by two forward slashes // and it returns the quotient of the division operation rounding down to the nearest integer. For example, the floor division of 7 // 3 would be 2 since 3 goes into 7 two whole times with 1 left over.

Here's an example of using floor division operator:

In the above example, we have defined two variables a and b , and then used floor division operator // to divide a by b . Since a is 10 and b is 3 , the result of a // b is 3 .

II. Comparison Operators

Comparison operators, also known as relational operators, are used to compare two values or operands. In Python, comparison operators always return a boolean value - either True or False.

There are six comparison operators in Python:

Equal to (==)

Not equal to (!=)

Greater than (>)

Less than (<)

Greater than or equal to (>=)

Less than or equal to (<=)

These operators are used in conditional statements and loops to test whether a certain condition is true or false.

a. Equal to (==)

The equal to operator ( == ) is a comparison operator used to compare the equality of two operands. It returns True if the values of the two operands are equal, otherwise, it returns False .

In this example, the first comparison returns False because x is not equal to y . The second comparison returns True because x is equal to z .

b. Not equal to (!=)

In Python, the "not equal to" operator is represented by the exclamation mark followed by an equal sign (!=). It is a binary operator and is used to compare two values. The operator returns True if the values are not equal and False if they are equal.

Here's an example of using the "not equal to" operator in Python:

c. Greater than (>)

The greater than operator ( > ) is used to check if the left operand is greater than the right operand. It returns True if the left operand is greater than the right operand, otherwise, it returns False . Here is an example:

In the example above, x is greater than y , so the expression x > y returns True .

d. Less than (<)

In Python, the less than operator < is used to compare two operands. It returns True if the left operand is less than the right operand, and False otherwise.

In this example, x is less than y , so the if statement evaluates to True , and the first print statement is executed.

e. Greater than or equal to (>=)

The greater than or equal to operator (>=) is used to compare two values. It returns True if the left operand is greater than or equal to the right operand, and False otherwise.

For example:

In this example, the first print statement returns True because x (which is 5) is greater than or equal to y (which is 3). The second print statement returns False because y is less than x .

f. Less than or equal to (<=)

The "Less than or equal to" operator is represented by the symbol "<=". It is used to check if one value is less than or equal to another value.

For example, in the expression "5 <= 10", the operator "<=" checks if 5 is less than or equal to 10. Since this is true, the expression evaluates to True. However, in the expression "10 <= 5", the operator "<=" checks if 10 is less than or equal to 5. Since this is false, the expression evaluates to False.

Here's an example code snippet demonstrating the use of the "<=" operator:

This code will output "x is less than or equal to y", since 5 is indeed less than or equal to 10.

III. Logical Operators

Python Logical Operators are used to combine two or more conditions and perform logical operations on them. The following are the three logical operators in Python:

These operators are used to perform logical operations on the operands and return a Boolean value.

The 'and' operator returns True if both operands are True, otherwise, it returns False.

The 'or' operator returns True if either of the operands is True, otherwise, it returns False.

The 'not' operator returns the opposite of the operand.

Let's look at depth with some examples to understand how these operators work.

The and operator returns True if both operands are true and returns False if either one of the operands is false.

Here's the truth table for the and operator:

Operand 1Operand 2Result

Here's an example code snippet:

In this example, the and operator is used to check if x is smaller than both y and z . If this condition is true, then the statement "x is the smallest number" is printed.

The OR operator in Python is represented by or . It is a logical operator that returns True if at least one of the operands is True , and False otherwise. Here are the possible truth tables for the OR operator:

Operand 1Operand 2Result

Here's an example of using the OR operator in Python:

In this example, x is not greater than y or z , so the output will be x is not greater than y or z .

The NOT operator is a unary operator that negates the value of its operand. In Python, the NOT operator is represented by the keyword "not".

The NOT operator returns True if its operand is False, and vice versa. Here's an example:

In this example, the value of x is True. However, the NOT operator negates the value of x and returns False.

IV. Assignment Operators

Have you ever wanted to quickly assign or modify a value in Python without writing a lot of code? That's where assignment operators come in handy! They allow you to perform an operation on a variable and assign the result back to the same variable in a single step. In this section, we will explore the different types of assignment operators in Python.

a. Simple Assignment Operator

The simple assignment operator in Python is denoted by the equal sign "=" and is used to assign a value to a variable. The syntax for simple assignment is:

where variable is the name of the variable and value is the value to be assigned to the variable.

For example, the following code assigns the value 10 to the variable x :

After executing this code, the variable x will have the value 10 .

b. Arithmetic Assignment Operators

Arithmetic assignment operators are a shorthand way of performing arithmetic operations and assignment at the same time. These operators include:

+= : adds the value of the right operand to the value of the left operand and assigns the result to the left operand.

-= : subtracts the value of the right operand from the value of the left operand and assigns the result to the left operand.

*= : multiplies the value of the left operand by the value of the right operand and assigns the result to the left operand.

/= : divides the value of the left operand by the value of the right operand and assigns the result to the left operand.

%= : computes the modulus of the value of the left operand and the value of the right operand, and assigns the result to the left operand.

//= : performs floor division on the value of the left operand and the value of the right operand, and assigns the result to the left operand.

**= : raises the value of the left operand to the power of the value of the right operand, and assigns the result to the left operand.

These operators can be used with numeric values and variables of numeric types, such as integers and floating-point numbers.

In each of the above examples, the arithmetic operation and the assignment operation are performed at the same time using the shorthand arithmetic assignment operator.

c. Bitwise Assignment Operators

Bitwise assignment operators are used to perform a bitwise operation on a variable and then assign the result to the same variable. The bitwise assignment operators include:

&= : Performs a bitwise AND operation on the variable and the value on the right, then assigns the result to the variable.

|= : Performs a bitwise OR operation on the variable and the value on the right, then assigns the result to the variable.

^= : Performs a bitwise XOR operation on the variable and the value on the right, then assigns the result to the variable.

<<= : Performs a left shift operation on the variable by the number of bits specified on the right, then assigns the result to the variable.

>>= : Performs a right shift operation on the variable by the number of bits specified on the right, then assigns the result to the variable.

d. Logical Assignment Operators

There are no specific "Logical Assignment Operators" in Python, as the logical operators and , or , and not are already used for combining and negating boolean expressions. However, it is possible to use logical operators in combination with assignment operators to create compound expressions, such as x += y or z , which assigns the value of y to x if y is truthy, or the value of z otherwise.

e. Comparison Assignment Operators

There is no such thing as "Comparison Assignment Operators". The term "comparison operator" refers to operators that compare two values and return a boolean value (True or False), while "assignment operator" refers to operators that assign a value to a variable.

However, there are shorthand ways to perform a comparison and assign the result to a variable in a single line of code. For example:

x = 10 if a > b else 20 : This assigns the value 10 to x if a > b is True, otherwise it assigns the value 20.

x += 1 if a == b else 2 : This adds 1 to x if a == b is True, otherwise it adds 2.

x *= 2 if a < b else 3 : This multiplies x by 2 if a < b is True, otherwise it multiplies it by 3.

V. Bitwise Operators

Bitwise operators are used to manipulate the individual bits of binary numbers. In Python, bitwise operators can be applied to integers. The bitwise operators take two operands and operate on them bit by bit to produce a result. There are six bitwise operators in Python: AND, OR, XOR, NOT, left shift, and right shift. These operators are commonly used in low-level programming, such as device driver development and network packet processing.

a. Bitwise AND

The bitwise AND operator is represented by the & symbol in Python. It performs a logical AND operation on each corresponding bit of its operands. If both bits are 1, the resulting bit is 1. Otherwise, the resulting bit is 0.

In this example, a and b are two integers represented in binary. The & operator is used to perform a bitwise AND operation on the two numbers, resulting in the binary number 0010 , which is equivalent to the decimal number 2. The resulting value is assigned to the variable c .

b. Bitwise OR

Bitwise OR is another binary operator that operates on two integers and performs a bitwise OR operation on their binary representations. The resulting binary representation is converted back to an integer.

The syntax for the bitwise OR operator is the pipe symbol | . For example, a | b performs a bitwise OR operation on a and b .

In the above example, the binary OR operation on a and b results in 0011 1101 , which is equal to 61 in decimal representation.

c. Bitwise XOR

Bitwise XOR (exclusive OR) operator is represented by the symbol ^ in Python. The operator returns a binary number that has a 1 in each bit position where the corresponding bits of either but not both operands are 1.

For example, let's say we have two variables a = 13 and b = 17 . The binary representation of 13 is 1101 and the binary representation of 17 is 10001 . Now, let's perform the bitwise XOR operation on these two variables:

In the above example, the resulting binary number is 11000 , which is equivalent to the decimal number 24 . Therefore, the value of the variable c will be 24 .

Here is another example that demonstrates the use of bitwise XOR:

In this example, we first define a and b as binary numbers using the 0b prefix. We then perform the bitwise XOR operation on these two numbers and store the result in c . The resulting binary number is 0b110 , which is equivalent to the decimal number 6 . Therefore, the value of the variable c will be 6 .

d. Bitwise NOT

Bitwise NOT is a unary operator in Python that flips the bits of a number. It is represented by the tilde (~) symbol. When applied to a binary number, the Bitwise NOT operator returns the complement of the number.

In the above code, the value of x is 7, which is represented in binary as 0000 0111. When we apply the Bitwise NOT operator (~) to x , it flips all the bits of the number, resulting in 1111 1000. The output is in two's complement form, which is the way negative numbers are represented in binary.

VI. Membership Operators

Membership operators are used to test if a sequence is present in an object. In Python, we have two membership operators:

in : Evaluates to True if the sequence is present in the object.

not in : Evaluates to True if the sequence is not present in the object.

These operators are typically used with strings, lists, tuples, and sets to check if a certain element or a sequence of elements is present in them.

VII. Identity Operators

Identity Operators are used to compare the memory locations of two objects. There are two identity operators in Python:

is - Returns True if both variables are the same object.

is not - Returns True if both variables are not the same object.

In this example, x and y have the same values, but they are not the same object. z is assigned the same memory location as x , so x and z are the same object. The is operator returns True when comparing x and z , but False when comparing x and y . The is not operator returns False when comparing x and z , but True when comparing x and y .

Last updated 1 year ago

COMMENTS

  1. Assignment Operators in C

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

  2. Assignment Operators in C

    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.

  3. C Assignment Operators

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

  4. Assignment Operators in Programming

    What are Assignment Operators? 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 the variable on ...

  5. Assignment Operators in C Example

    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.

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

  7. Assignment Operators in C with Examples

    Assignment Operators in C with Examples Last Updated: September 7, 2022 by Chaitanya Singh | Filed Under: c-programming Assignment operators are used to assign value to a variable. The left side of an assignment operator is a variable and on the right side, there is a value, variable, or an expression.

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

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

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

  11. Assignment Operator in C

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

  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. For example, in the expression x = y + z, the sum of y and z is assigned to x.

  13. Assignment and shorthand assignment operator in C

    Assignment and shorthand assignment operator in C. 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.

  14. C Assignment Operators

    The assignment operators in C can both transform and assign values in a single operation. C provides the following assignment operators: | =. 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.

  15. Assignment Operators in C Programming

    In this C Programming Video Tutorial we will learn about assignment operators in detail. Operators are the symbol which will perform different operation on the operands.

  16. Operators in C

    An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform addition. In this tutorial, you will learn about different C operators such as arithmetic, increment, assignment, relational, logical, etc. with the help of examples.

  17. C Operators

    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:

  18. Assignment Operators in C

    This article by Scaler Topics explains what are assignment operators in C programming & helps you to learn about all kinds of assignment operators in C language using examples.

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

    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. Example -: x = 18 ...

  20. Assignment Operator in C

    Assignment Operator in C with Tutorial, C language with programming examples for beginners and professionals covering concepts, c pointers, c structures, c union, c strings etc.

  21. Assignment Operators in C

    C Programming & Data Structures: Assignment Operators in CTopics discussed:1. Introduction to Assignment Operators in C language.2. Types of Shorthand Assign...

  22. Assignment operator in c

    Assignment Operator in C Using assignment operators, we can assign value to the variables. Equality sign (=) is used as an assignment operator in C.

  23. Demystifying Assignment Operators in C Programming

    Through real-world examples and output demonstrations, you'll gain a solid understanding of how to use these operators effectively in your C programs. Assignment operators are vital tools in C programming, allowing you to manipulate variables and perform calculations efficiently.

  24. Operators

    That's where assignment operators come in handy! They allow you to perform an operation on a variable and assign the result back to the same variable in a single step. ... These operators are commonly used in low-level programming, such as device driver development and network packet processing. a. Bitwise AND . The bitwise AND operator is ...