• Trending Now
  • Foundational Courses
  • Data Science
  • Practice Problem
  • Machine Learning
  • System Design
  • DevOps Tutorial

What are Operators in Programming?

  • Binary Operators in Programming
  • Unary Operators in Programming
  • Arithmetic Operators in Programming
  • Types of Operators in Programming
  • Assignment Operators in Programming
  • Comparison Operators in Programming
  • Ternary Operator in Programming
  • Bitwise AND operator in Programming
  • Right Shift Operator (>>) in Programming
  • What is Imperative Programming?
  • Bitwise OR Operator (|) in Programming
  • Logical AND operator in Programming
  • Logical Operators in Programming
  • Conditional Operator in Programming
  • Modulus Operator in Programming
  • Operator Associativity in Programming
  • Logical NOT Operator in Programming
  • Pre and Post Decrement Operator in Programming
  • Increment and Decrement Operators in Programming

Operators in programming are essential symbols that perform operations on variables and values , enabling tasks like arithmetic calculations, logical comparisons, and bitwise manipulations. In this article, we will learn about the basics of operators and their types.

Operators-in-Programming

Operators in Programming

Table of Content

  • Operator Precedence and Associativity in Programming
  • Frequently Asked Questions (FAQs) related to Programming Operators

Operators in programming are symbols or keywords that represent computations or actions performed on operands. Operands can be variables , constants , or values , and the combination of operators and operands form expressions. Operators play a crucial role in performing various tasks, such as arithmetic calculations, logical comparisons, bitwise operations, etc.

Operator Symbol Name Type Description Use
+ Addition Arithmetic Operators Adds two values result = num1 + num2;
Subtraction Subtracts the right operand from the left result = num1 – num2;
* Multiplication Multiplies two values result = num1 * num2;
/ Division Divides the left operand by the right result = num1 / num2;
% Modulus Returns the remainder of division result = num1 % num2;
++ Increment Unary Operators Increases the value of a variable by 1 num++; or ++num;
Decrement Decreases the value of a variable by 1 num–; or –num;
= Assignment Assignment Operators Assigns a value to a variable x = 10;
+= Add and Assign Adds the right operand to the left and assigns x += 5; (equivalent to x = x + 5;)
-= Subtract and Assign Subtracts the right operand and assigns x -= 3; (equivalent to x = x – 3;)
*= Multiply and Assign Multiplies the right operand and assigns x *= 2; (equivalent to x = x * 2;)
/= Divide and Assign Divides the left operand and assigns x /= 4; (equivalent to x = x / 4;)
%= Modulus and Assign Computes modulus and assigns x %= 3; (equivalent to x = x % 3;)
== Equal to Relational or Comparison Operators Tests if two values are equal if (a == b)
!= Not Equal to Tests if two values are not equal if (a != b)
< Less Than Tests if the left value is less than the right if (a < b)
> Greater Than Tests if the left value is greater than right if (a > b)
<= Less Than or Equal To Tests if the left value is less than or equal if (a <= b)
>= Greater Than or Equal To Tests if the left value is greater than or equal if (a >= b)
&& Logical AND Logical Operators Returns true if both operands are true if (a && b)
|| Logical OR
! Logical NOT Reverses the logical state of its operand if (!condition)
& Bitwise AND Bitwise Operators Performs bitwise AND on individual bits result = a & b;
` Bitwise OR Performs bitwise OR on individual bits
^ Bitwise XOR Performs bitwise XOR on individual bits result = a ^ b;
~ Bitwise NOT Inverts the bits of its operand result = ~a;
<< Left Shift Shifts bits to the left result = a << 2;
>> Right Shift Shifts bits to the right result = a >> 1;
?: Conditional (Ternary) Conditional Operator Evaluates a condition and returns one of two values result = (condition) ? value1 : value2;

Types of Operators in Programming:

Here are some common types of operators:

  • Arithmetic Operators: Perform basic arithmetic operations on numeric values. Examples: + (addition), – (subtraction), * (multiplication), / (division), % (modulo).
  • Comparison Operators: Compare two values and return a Boolean result (true or false). Examples: == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to).
  • Logical Operators: Perform logical operations on Boolean values. Examples: && (logical AND), || (logical OR), ! (logical NOT).
  • Assignment Operators: Assign values to variables. Examples: = (assign), += (add and assign), -=, *= (multiply and assign), /=, %=.
  • Increment and Decrement Operators: Increase or decrease the value of a variable by 1. Examples: ++ (increment), — (decrement).
  • Bitwise Operators: Perform operations on individual bits of binary representations of numbers. Examples: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), >> (right shift).

These operators provide the building blocks for creating complex expressions and performing diverse operations in programming languages. Understanding their usage is crucial for writing efficient and expressive code.

Arithmetic Operators in Programming:

Arithmetic operators in programming are fundamental components of programming languages, enabling the manipulation of numeric values for various computational tasks. Here’s an elaboration on the key arithmetic operators:

Operator Description Examples
+ (Addition) Combines two numeric values, yielding their sum. (result will be 8)
– (Subtraction) Subtracts the right operand from the left operand. (difference will be 6)
* (Multiplication) Multiplies two numeric values, producing their product. (product will be 21)
/ (Division) Divides the left operand by the right operand, producing a quotient. (quotient will be 5)
% (Modulo) Returns the remainder after the division of the left operand by the right operand. (remainder will be 1)

Comparison Operators in Programming:

Comparison operators in programming are used to compare two values or expressions and return a Boolean result indicating the relationship between them. These operators play a crucial role in decision-making and conditional statements. Here are the common comparison operators:

Operator Description Examples
== (Equal to) Checks if the values on both sides are equal. (evaluates to true)
!= (Not equal to) Checks if the values on both sides are not equal. (evaluates to true)
< (Less than) Tests if the value on the left is less than the value on the right. (evaluates to true)
> (Greater than) Tests if the value on the left is greater than the value on the right. (evaluates to true)
<= (Less than or equal to) Checks if the value on the left is less than or equal to the value on the right. (evaluates to true)
>= (Greater than or equal to) Checks if the value on the left is greater than or equal to the value on the right. (evaluates to true)

These operators are extensively used in conditional statements, loops, and decision-making constructs to control the flow of a program based on the relationship between variables or values. Understanding comparison operators is crucial for creating logical and effective algorithms in programming.

Logical Operators in Programming:

Logical operators in programming are used to perform logical operations on Boolean values . These operators are crucial for combining or manipulating conditions and controlling the flow of a program based on logical expressions. Here are the common logical operators:

Operator Description Examples
&& (Logical AND) Returns true if both operands are true; otherwise, it returns false. (evaluates to false)
(||) Logical OR Returns true if at least one of the operands is true; otherwise, it returns false

true || false; (evaluates to true)

! (Logical NOT) Returns true if the operand is false and vice versa; it negates the Boolean value. (evaluates to false)

These logical operators are frequently used in conditional statements (if, else if, else), loops, and decision-making constructs to create complex conditions based on multiple Boolean expressions. Understanding how to use logical operators is essential for designing effective and readable control flow in programming.

Assignment Operators in Programming:

Assignment operators in programming are used to assign values to variables. They are essential for storing and updating data within a program. Here are common assignment operators:

Operator Description Examples
= (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 are fundamental for updating variable values, especially in loops and mathematical computations, contributing to the dynamic nature of programming. Understanding how to use assignment operators is essential for effective variable manipulation in a program.

Increment and Decrement Operators in Programming:

Increment and decrement operators in programming are used to increase or decrease the value of a variable by 1, respectively. They are shorthand notations for common operations and are particularly useful in loops. Here are the two types:

Operator Description Examples
++ (Increment) Increases the value of a variable by 1. is equivalent to or
— (Decrement) Decreases the value of a variable by 1. is equivalent to or

These operators are frequently employed in loops, especially for iterating through arrays or performing repetitive tasks. Their concise syntax enhances code readability and expressiveness.

Bitwise Operators in Programming:

Bitwise operators in programming perform operations at the bit level , manipulating individual bits of binary representations of numbers. These operators are often used in low-level programming, such as embedded systems and device drivers. Here are the common bitwise operators:

Operator Description Examples
& (Bitwise AND) Performs a bitwise AND operation between corresponding bits of two operands. sets each bit to 1 if both corresponding bits in A and B are 1.
| (Bitwise OR) Performs a bitwise OR operation between corresponding bits of two operands. A | B sets each bit to 1 if at least one corresponding bit in A or B is 1.
^ (Bitwise XOR) Performs a bitwise XOR (exclusive OR) operation between corresponding bits of two operands. sets each bit to 1 if the corresponding bits in A and B are different.
~ (Bitwise NOT) Inverts the bits of a single operand, turning 0s to 1s and vice versa. inverts all bits of A.
<< (Left Shift) Shifts the bits of the left operand to the left by a specified number of positions. shifts the bits of A two positions to the left.
>> (Right Shift) Shifts the bits of the left operand to the right by a specified number of positions. shifts the bits of A three positions to the right.

Bitwise operators are useful in scenarios where direct manipulation of binary representations or specific bit patterns is required, such as optimizing certain algorithms or working with hardware interfaces. Understanding bitwise operations is essential for low-level programming tasks.

Operator Precedence and Associativity in Programming :

Operator Precedence is a rule that determines the order in which operators are evaluated in an expression. It defines which operators take precedence over others when they are combined in the same expression. Operators with higher precedence are evaluated before operators with lower precedence. Parentheses can be used to override the default precedence and explicitly specify the order of evaluation.

Operator Associativity is a rule that determines the grouping of operators with the same precedence in an expression when they appear consecutively. It specifies the direction in which operators of equal precedence are evaluated. The two common associativities are:

  • Left to Right (Left-Associative): Operators with left associativity are evaluated from left to right. For example, in the expression a + b + c , the addition operators have left associativity, so the expression is equivalent to (a + b) + c .
  • Right to Left (Right-Associative): Operators with right associativity are evaluated from right to left. For example, in the expression a = b = c , the assignment operator = has right associativity, so the expression is equivalent to a = (b = c) .
Precedence Operator Description Associativity
1 () Parentheses Left-to-Right
x++, x– Postfix increment, decrement
2 ++x, –x Prefix increment, decrement Right-to-Left
‘+’ , ‘-‘ Unary plus, minus
! , ~ Logical NOT, Bitwise complement
* Dereference Operator
& Addressof Operator
3 *, /, % Multiplication, division, modulus Left-to-Right
4 +, – Addition, subtraction Left-to-Right
5 << , >> Bitwise shift left, Bitwise shift right Left-to-Right
6 < , <= Relational less than, less than or equal to Left-to-Right
> , >= Relational greater than, greater than or equal to
7 == , != Relational is equal to, is not equal to Left-to-Right
8 & Bitwise AND Left-to-Right
9 ^ Bitwise XOR Left-to-Right
10 | Bitwise OR Left-to-Right
11 && Logical AND Left-to-Right
12 || Logical OR Left-to-Right
13 ?: Ternary conditional Right-to-Left
14 = Assignment Right-to-Left
+= , -= Addition, subtraction assignment
*= , /= Multiplication, division assignment
%= , &= Modulus, bitwise AND assignment
^= , |= Bitwise exclusive, inclusive OR assignment
<<=, >>= Bitwise shift left, right assignment
15 , comma (expression separator) Left-to-Right

Frequently Asked Questions (FAQs) related to Programming Operators :

Here are some frequently asked questions (FAQs) related to programming operators:

Q1: What are operators in programming?

A: Operators in programming are symbols that represent computations or actions to be performed on operands. They can manipulate data, perform calculations, and facilitate various operations in a program.

Q2: How are operators categorized?

A: Operators are categorized based on their functionality. Common categories include arithmetic operators (for mathematical operations), assignment operators (for assigning values), comparison operators (for comparing values), logical operators (for logical operations), and bitwise operators (for manipulating individual bits).

Q3: What is the difference between unary and binary operators?

A: Unary operators operate on a single operand, while binary operators operate on two operands. For example, the unary minus -x negates the value of x , while the binary plus a + b adds the values of a and b .

Q4: Can operators be overloaded in programming languages?

A: Yes, some programming languages support operator overloading, allowing developers to define custom behaviors for operators when applied to user-defined types. This is commonly seen in languages like C++.

Q5: How do logical AND ( && ) and logical OR ( || ) operators work?

A: The logical AND ( && ) operator returns true if both of its operands are true. The logical OR ( || ) operator returns true if at least one of its operands is true. These operators are often used in conditional statements and expressions.

Q6: What is the purpose of the ternary operator ( ?: )?

A: The ternary operator is a shorthand for an if-else statement. It evaluates a condition and returns one of two values based on whether the condition is true or false. It is often used for concise conditional assignments.

Q7: How does the bitwise XOR operator ( ^ ) work?

A: The bitwise XOR ( ^ ) operator performs an exclusive OR operation on individual bits. It returns 1 for bits that are different and 0 for bits that are the same. This operator is commonly used in bit manipulation tasks.

Q8: What is the difference between = and == ?

A: The = operator is an assignment operator used to assign a value to a variable. The == operator is a comparison operator used to check if two values are equal. It is important not to confuse the two, as = is used for assignment, and == is used for comparison.

Q9: How do increment ( ++ ) and decrement ( -- ) operators work?

A: The increment ( ++ ) operator adds 1 to the value of a variable, while the decrement ( -- ) operator subtracts 1. These operators can be used as prefix ( ++x ) or postfix ( x++ ), affecting the order of the increment or decrement operation.

Q10: Are operators language-specific?

A: While many operators are common across programming languages, some languages may introduce unique operators or have variations in their behavior. However, the fundamental concepts of arithmetic, logical, and bitwise operations are prevalent across various languages.

Q11: What is the modulus operator ( % ) used for?

A: The modulus operator ( % ) returns the remainder when one number is divided by another. It is often used to check divisibility or to cycle through a sequence of values. For example, a % 2 can be used to determine if a is an even or odd number.

Q12: Can the same operator have different meanings in different programming languages?

A: Yes, the same symbol may represent different operators or operations in different programming languages. For example, the + operator is used for addition in most languages, but in some languages (like JavaScript), it is also used for string concatenation.

Q13: What is short-circuit evaluation in the context of logical operators?

A: Short-circuit evaluation is a behavior where the second operand of a logical AND ( && ) or logical OR ( || ) operator is not evaluated if the outcome can be determined by the value of the first operand alone. This can lead to more efficient code execution.

Q14: How are bitwise left shift ( << ) and right shift ( >> ) operators used?

A: The bitwise left shift ( << ) operator shifts the bits of a binary number to the left, effectively multiplying the number by 2. The bitwise right shift ( >> ) operator shifts the bits to the right, effectively dividing the number by 2.

Q15: Can you provide an example of operator precedence in programming?

A: Operator precedence determines the order in which operators are evaluated. For example, in the expression a + b * c , the multiplication ( * ) has higher precedence than addition ( + ), so b * c is evaluated first.

Q16: How does the ternary operator differ from an if-else statement?

A: The ternary operator ( ?: ) is a concise way to express a conditional statement with a single line of code. It returns one of two values based on a condition. An if-else statement provides a more extensive code block for handling multiple statements based on a condition.

Q17: Are there any operators specifically designed for working with arrays or collections?

A: Some programming languages provide specific operators or methods for working with arrays or collections. For example, in Python, the in operator is used to check if an element is present in a list.

Q18: How can bitwise operators be used for efficient memory management?

A: Bitwise operators are often used for efficient memory management by manipulating individual bits. For example, bitwise AND can be used to mask specific bits, and bitwise OR can be used to set particular bits.

Q19: Can operators be overloaded in all programming languages?

A: No, not all programming languages support operator overloading. While some languages like C++ allow developers to redefine the behavior of operators for user-defined types, others, like Java, do not permit operator overloading.

Q20: How do you handle operator precedence in complex expressions?

A: Parentheses are used to explicitly specify the order of operations in complex expressions, ensuring that certain operations are performed before others. For example, (a + b) * c ensures that addition is performed before multiplication.

In conclusion, operators in programming are essential tools that enable the manipulation, comparison, and logical operations on variables and values. They form the building blocks of expressions and play a fundamental role in controlling program flow, making decisions, and performing calculations. From arithmetic and comparison operators for numerical tasks to logical operators for decision-making, and bitwise operators for low-level bit manipulation, each type serves specific purposes in diverse programming scenarios.

Please Login to comment...

Similar reads.

  • Programming

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

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.
  • This page has been accessed 58,085 times.
  • Privacy policy
  • About cppreference.com
  • Disclaimers

Powered by MediaWiki

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?

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

Submit and view feedback for

Additional resources

C++ Tutorial

C++ functions, c++ classes, c++ reference, c++ examples, c++ assignment 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 :

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

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.

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 Functions
  • 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 - 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 −

IMAGES

  1. Assignment Operators in C

    what are the different assignment operators

  2. Assignment Operators in Java with Examples

    what are the different assignment operators

  3. What are Operators in Java and its Types?

    what are the different assignment operators

  4. assignment-operators-2 » EXAMRADAR.COM

    what are the different assignment operators

  5. Operators and Types of Operators

    what are the different assignment operators

  6. Assignment Operators in C with Examples

    what are the different assignment operators

VIDEO

  1. You have a different assignment

  2. THE SAME MANTLE, DIFFERENT ASSIGNMENT "ELIJAH AND ELISHA"

  3. HE HAD A DIFFERENT ASSIGNMENT ‼️😂🤣 #goodhumor #comedy #funny #hilarious #goodrelationship

  4. The Sense Of Right Alliance Meets The Expendebles 4 Final Trailer

  5. Core

  6. Java in Practice Course

COMMENTS

  1. Assignment Operators in Programming - GeeksforGeeks

    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.

  2. Assignment Operators in C - GeeksforGeeks

    Different types of assignment operators are shown below: 1. “=”: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. Example: a = 10; b = 20; ch = 'y'; 2. “+=”: This operator is combination of ‘+’ and ‘=’ operators.

  3. What are Operators in Programming? - GeeksforGeeks

    Assignment Operators: Assign values to variables. Examples: = (assign), += (add and assign), -=, *= (multiply and assign), /=, %=. Increment and Decrement Operators: Increase or decrease the value of a variable by 1. Examples: ++ (increment), — (decrement).

  4. Assignment operators | Microsoft Learn

    There are two kinds of assignment operations: simple assignment, in which the value of the second operand is stored in the object specified by the first operand. compound assignment, in which an arithmetic, shift, or bitwise operation is performed before storing the result.

  5. Assignment operators - cppreference.com

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

  6. Assignment operators - cppreference.com

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

  7. C Assignment Operators | Microsoft Learn

    = *= /= %= += -= <<= >>= &= ^= |= The assignment operators in C can both transform and assign values in a single operation. C provides the following assignment operators: Expand table.

  8. C Operators - W3Schools

    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: Example. int x = 10; Try it Yourself » The addition assignment operator ( +=) adds a value to a variable: Example. int x = 10; x += 5; Try it Yourself »

  9. C++ Assignment Operators - W3Schools

    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:

  10. Assignment Operators in C - Online Tutorials Library

    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.