CProgramming Tutorial

  • C Programming Tutorial
  • C - Overview
  • C - Environment Setup
  • C - Program Structure
  • C - Basic Syntax
  • C - Data Types
  • C - Variables
  • C - Constants
  • C - Storage Classes
  • C - Operators
  • C - Decision Making
  • C - Functions
  • C - Scope Rules
  • C - Pointers
  • C - Strings
  • C - Structures
  • C - Bit Fields
  • C - Typedef
  • C - Input & Output
  • C - File I/O
  • C - Preprocessors
  • C - Header Files
  • C - Type Casting
  • C - Error Handling
  • C - Recursion
  • C - Variable Arguments
  • C - Memory Management
  • C - Command Line Arguments
  • C Programming useful 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

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

Try the following example to understand all the assignment operators available in C −

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

Kickstart Your Career

Get certified by completing the course

Learn C practically and Get Certified .

Popular Tutorials

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

  • Keywords & Identifier
  • Variables & Constants
  • C Data Types
  • C Input/Output
  • C Operators
  • C Introduction Examples

C Flow Control

  • C if...else
  • C while Loop
  • C break and continue
  • C switch...case
  • C Programming goto
  • Control Flow Examples

C Functions

  • C Programming Functions
  • C User-defined Functions
  • C Function Types
  • C Recursion
  • C Storage Class
  • C Function Examples
  • C Programming Arrays
  • C Multi-dimensional Arrays
  • C Arrays & Function
  • C Programming Pointers
  • C Pointers & Arrays
  • C Pointers And Functions
  • C Memory Allocation
  • Array & Pointer Examples

C Programming Strings

  • C Programming String
  • C String Functions
  • C String Examples

Structure And Union

  • C Structure
  • C Struct & Pointers
  • C Struct & Function
  • C struct Examples

C Programming Files

  • C Files Input/Output
  • C Files Examples

Additional Topics

  • C Enumeration
  • C Preprocessors
  • C Standard Library
  • C Programming Examples

C Precedence And Associativity Of Operators

Bitwise Operators in C Programming

  • Compute Quotient and Remainder
  • Find the Size of int, float, double and char

C if...else Statement

C Programming Operators

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

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 =

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 .

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 .

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

C Bitwise Operators

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.

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.

Related Tutorials

Codeforwin

Assignment and shorthand assignment operator in C

Quick links.

  • Shorthand assignment

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

For example consider the below assignment table.

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

Shorthand assignment operator

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

For example, consider following C statements.

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

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

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.

Introduction

Assignment Operators help us to assign the value or result of an expression to a variable and the value on the right side must be of the same data type as the variable on the left side. They have the lowest precedence level among all the operators, and it associates from right to left. The most commonly used Assignment Operator is = . Also, Assignment Operators fall under the category of Binary Operators.

For example, x = 4 ; then that means value 4 is assigned to variable x or we can say that variable x holds value 4 .

Explanation

Let me explain to you more about Assignment Operators. Don't worry after this section, you will fully understand the definition of Assignment Operators in C.

Our example is x = 4 , so what does it tell us?

  • It simply says, " hey variable x please hold a value 4 which I give you as I wrote in the definition."

So can we say that always variables are on the Left Hand Side of the assignment operator and values are always on the Right Hand Side of the operator? YES . Please take a look at the image it will help you to understand more about the above wording.

LHS and RHS Operands

The above diagram helps us to understand that the RHS value is assigned to the LHS variable.

The LHS and RHS are known as Operands.

So the operand on the LHS of the assignment operator must be a variable and operand on RHS must be a constant , variable or expression . For example:

As mentioned, precedence levels of assignment operators are lower than all the operators we have discussed so far and it associates from right to left. Now you may wonder what do you mean by it associates from right to left? Let's understand this together.

For example:

This is totally correct and means we can also assign the same value to multiple variables with one single line of code .

So what do you get from the above line of code? Simply, variable_x , variable_y and variable_z hold the same value. YES!! TRUE. But how?

The main question is here how value is assigned to them? is first variable_x get 10 or variable_y or variable_z ? What do you say? This answer is given by the line: It associates from right to left .

So that means we have to read the line from the right side to the left. Like, at first 10 is given to variable_z then variable_y gets the value present in the variable_z and after that variable_x get the value present in the variable_y . So the above wording is equivalent to the following expression.

This is the simplest explanation about assignment operator associativity.

The most basic Assignment operator is = . It requires two operands for its work. For example, = x doesn't make any sense but variable = x makes sense because it clearly says the variable variable stores the value of x . Therefore, Assignment operators are Binary operators.

Hopefully, every point in the definition is now clear to all of you.

List of All Assignment Operators in C

We have 2 types of Assignment Operators in C :

  • Simple Assignment operator (Example : = ) .
  • Compound Assignment Operators (Example : += , -= , &= ) .

Simple Assignment Operator in C

It is the operator used to assign the Right Operand to Left Operand . There is only one simple Assignment Operator and that is = . The general Syntax is like Left Operand = Right Operand .

Compound Assignment Operators in C

Any Binary Operator with a simple Assignment Operator will form Compound Assignment Operators.

The general syntax is like Left operand operation = Right operand . Here, the operation is what you want + , - , *, etc.

Let's take an example:

Here read carefully. After which, you will never forget how to read the syntax of a compound assignment operator.

So we read like this FIRST ADD 10 to variable_x , THEN WHATEVER THE RESULT, ASSIGN THAT RESULT TO variable_x . That means the above line of code is equal to

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

Example program for Assignment Operators in C

This is a simple Assignment Operator.

+= Operator

This is the Addition Assignment Operator . In which the left operand becomes equal to the addition of the right operand and left operand.

In this program, x+=y means x+y , so we assign the result of x+y to x .

-= Operator

This is the Subtraction Assignment Operator .

In which left operand becomes equal to the subtraction of right operator from left operand.

The program performs the subtraction of two numbers i.e. x-=y means x = x-y . So the output is :

*= Operator

The main purpose of this operator is that this left operand becomes equal to the product of the left and right operand. This is the Multiplication Assignment Operator .

The program performs the multiplication of two numbers and then the result of the multiplication is assigned to the variable x .

/= Operator

This one is Division Assignment Operator . In this, the left operand becomes equal to the division of the left and right operand.

This program performs a division of two numbers and the result is assigned to x variable i.e. x/=y is the same as x = x/y .

%= Operator

It is well known Modulus Assignment Operator . In this , left operand becomes equal to the modulo of left and right operand.

In this program, the user checks the remainder of two number and assign that remainder to x variable.

<<= Operator

This is called the Left Shift Assignment Operator . For example x <<= y so in this, x becomes equal to x left shifted by y .

The program basically shifts every bit of x to the left side by y places and then assigns the result to x .

>>= Operator

This is called the Right Shift Assignment Operator . For example x >>= y so , x becomes equal to x right shifted by y .

The program has defined the result of expression when x is right-shifted by y places and the result is going to store in x variable.

&= Operator

This operator is called the Bitwise AND Assignment Operator . Left operand becomes equal to the bitwise AND of left and right operand.

The program performs Bitwise AND operation on every bit of x and y . After that result is going to be stored in variable x .

|= Operator

This is called the Bitwise Inclusive OR Assignment Operator Left operand becomes equal to bitwise OR of left and right operand.

like Bitwise AND Assignment Operator , this program also performs Bitwise OR operation on every bit of x and y . And after that result is going to store in x .

^= Operator

This is called the Bitwise Exclusive OR Assignment Operator Left operand becomes equal to bitwise XOR of left and right operand.

This will perform Bitwise XOR operation on every bit of x and y . After that result is going to store in x .

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

Happy Coding folks!!!

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

C Functions

C structures, 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.

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:

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:

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:

Sizeof Operator

The memory size (in bytes) of a data type or a variable can be found with the sizeof operator:

Note that we use the %lu format specifer to print the result, instead of %d . It is because the compiler expects the sizeof operator to return a long unsigned int ( %lu ), instead of int ( %d ). On some computers it might work with %d , but it is safer to use %lu .

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

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

[email protected]

Top Tutorials

Top references, top examples, get certified.

C – Assignment Operators

  prev       next, assignment operators in c:.

In C programs, values for the variables are assigned using assignment operators.

  • For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as “sum = 10;”
  • There are 2 categories of assignment operators in C language. They are, 1. Simple assignment operator ( Example: = ) 2. Compound assignment operators ( Example: +=, -=, *=, /=, %=, &=, ^= )

Example program for C assignment operators:

  • In this program, values from 0 – 9 are summed up and total “45” is displayed as output.
  • Assignment operators such as “=” and “+=” are used in this program to assign the values and to sum up the values.

Continue on types of C operators:

Click on each operator name below for detailed description and example programs.

Prev       Next

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

  • 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

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h [email protected] , to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • Graphic Designing
  • Digital Marketing
  • On Page and Off Page SEO
  • Content Development
  • Corporate Training
  • Classroom and Online Training

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected] . Duration: 1 week to 2 week

RSS Feed

CsTutorialPoint - Computer Science Tutorials For Beginners

Assignment Operators In C [ Full Information With Examples ]

Assignment Operators In C

Assignment Operators In C

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

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

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

x = y = z = 100

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

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

  • Simple assignment operator
  • Compound assignment operators

1. Simple Assignment Operator In C

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

2. Compound Assignment Operators In C

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

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

Look at these two statements:

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

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

Syntax of Compound Assignment Operators

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

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

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

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

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

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

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

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

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

List of Assignment Operators In C

Read More -:

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

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

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

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

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

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

' src=

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

Leave a Reply Cancel reply

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

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

PrepBytes Blog

ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING

Sign in to your account

Forgot your password?

Login via OTP

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

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

Register with PrepBytes

Assignment operator in c.

' src=

Last Updated on June 23, 2023 by Prepbytes

assignment operators example program in c

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

What is Assignment Operator in C language?

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

Example of Assignment Operator in C

For example, consider the following line of code:

Types of Assignment Operators in C

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

FAQs related to Assignment Operator in C

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

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

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

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

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

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

Leave a Reply Cancel reply

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

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

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

Related Post

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

C Data Types

C operators.

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

C File Handling

  • C Cheatsheet

C Interview Questions

  • Write an Interview Experience
  • Share Your Campus Experience
  • C Programming Language Tutorial
  • C Language Introduction
  • Features of C Programming Language
  • C Programming Language Standard
  • C Hello World Program
  • Compiling a C Program: Behind the Scenes
  • Tokens in C
  • Keywords in C

C Variables and Constants

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

C Input/Output

  • Basic Input and Output in C
  • Format Specifiers in C
  • printf in C
  • Scansets in C
  • Formatted and Unformatted Input/Output functions in C with Examples

Operators in C

  • Arithmetic Operators in C
  • Unary operators in C/C++
  • Operators in C | Set 2 (Relational and Logical Operators)
  • Bitwise Operators in C/C++
  • C Logical Operators
  • Assignment Operators in C/C++
  • Increment and Decrement Operators in C
  • Conditional or Ternary Operator (?:) in C
  • sizeof operator in C
  • Operator Precedence and Associativity in C

C Control Statements Decision-Making

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

C Arrays & Strings

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

C User-Defined Data Types

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

C Storage Classes

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

C Memory Management

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

C Preprocessor

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

Miscellaneous

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

C Operators are symbols that represent operations to be performed on one or more operands. C provides a wide range of operators, which can be classified into different categories based on their functionality. Operators are used for performing operations on variables and values.

What are Operators in C?

Operators can be defined as the symbols that help us to perform specific mathematical, relational, bitwise, conditional, or logical computations on operands. In other words, we can say that an operator operates the operands. For example, ‘+’ is an operator used for addition, as shown below:  

Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’. The functionality of the C programming language is incomplete without the use of operators.

Types of Operators in C

C has many built-in operators and can be classified into 6 types:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Other Operators

The above operators have been discussed in detail: 

1. Arithmetic Operations in C

These operators are used to perform arithmetic/mathematical operations on operands. Examples: (+, -, *, /, %,++,–). Arithmetic operators are of two types: 

a) Unary Operators : 

Operators that operate or work with a single operand are unary operators. For example: Increment(++) and Decrement(–) Operators

b) Binary Operators :

Operators that operate or work with two operands are binary operators. For example: Addition(+), Subtraction(-), multiplication(*), Division(/) operators

2. Relational Operators in C

These are used for the comparison of the values of two operands. For example, checking if one operand is equal to the other operand or not, whether an operand is greater than the other operand or not, etc. Some of the relational operators are (==, >= , <= )(See this article for more reference).

3. Logical Operator in C

Logical Operators are used to combining two or more conditions/constraints or to complement the evaluation of the original condition in consideration. The result of the operation of a logical operator is a Boolean value either true or false . 

For example, the logical AND represented as the ‘&&’ operator in C returns true when both the conditions under consideration are satisfied. Otherwise, it returns false. Therefore, a && b returns true when both a and b are true (i.e. non-zero)(See this article for more reference).

4. Bitwise Operators in C  

The Bitwise operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands. Mathematical operations such as addition, subtraction, multiplication, etc. can be performed at the bit level for faster processing. For example, the bitwise AND operator represented as ‘&’ in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1(True). 

5. Assignment Operators in C

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

Different types of assignment operators are shown below: 

a) “=”

This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left.  Example:

b) “+=”

This operator is the combination of the ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.  Example:

c) “-=”  

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

d) “*=”  

This operator is a combination of the ‘*’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.  Example:

e) “/=”

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

6. Other Operators  

Apart from the above operators, there are some other operators available in C used to perform some specific tasks. Some of them are discussed here: 

i. sizeof operator

  • sizeof is much used in the C programming language.
  • It is a compile-time unary operator which can be used to compute the size of its operand.
  • The result of sizeof is of the unsigned integral type which is usually denoted by size_t.
  • Basically, the sizeof the operator is used to compute the size of the variable. 

To know more about the topic refer to this article.

ii. Comma Operator

  • The comma operator (represented by the token) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type).
  • The comma operator has the lowest precedence of any C operator.
  • Comma acts as both operator and separator. 

iii. Conditional Operator

  • The conditional operator is of the form Expression1? Expression2: Expression3
  • Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then we will execute and return the result of Expression2 otherwise if the condition(Expression1) is false then we will execute and return the result of Expression3.
  • We may replace the use of if..else statements with conditional operators. 

iv. dot (.) and arrow (->) Operators

  • Member operators are used to referencing individual members of classes, structures, and unions.
  • The dot operator is applied to the actual object. 
  • The arrow operator is used with a pointer to an object. 

to know more about dot operators refer to this article and to know more about arrow(->) operators refer to this article.

v.  Cast Operator

  • Casting operators convert one data type to another. For example, int(2.2000) would return 2.
  • A cast is a special operator that forces one data type to be converted into another. 
  • The most general cast supported by most of the C compilers is as follows −   [ (type) expression ] . 

vi.  &,* Operator

  • Pointer operator & returns the address of a variable. For example &a; will give the actual address of the variable.
  • The pointer operator * is a pointer to a variable. For example *var; will pointer to a variable var. 

C Operators with Example

Time and space complexity, precedence of operators in c.

The below table describes the precedence order and associativity of operators in C. The precedence of the operator decreases from top to bottom. 

In this article, the points we learned about the operator are as follows: Operators are symbols used for performing some kind of operation in C. The operation can be mathematical, logical, relational, bitwise, conditional, or logical. There are seven types of Unary operators, Arithmetic operator, Relational operator, Logical operator, Bitwise operator, Assignment operator, and Conditional operator. Every operator returns a numerical value except logical and conditional operator which returns a boolean value(true or false). ‘=’ and ‘==’ are not same as ‘=’ assigns the value whereas ‘==’ checks if both the values are equal or not. There is a Precedence in the operator means the priority of using one operator is greater than another operator.

Frequently Asked Questions(FAQs)

1. what are operators in c.

Operators in C are certain symbols in C used for performing certain mathematical, relational, bitwise, conditional, or logical operations for the user.

2. What are the 7 types of operators in C?

There are 7 types of operators in C as mentioned below:

  • Unary operator
  • Arithmetic operator
  • Relational operator
  • Logical operator
  • Bitwise operator
  • Assignment operator
  • Conditional operator

3. What is the difference between the ‘=’ and ‘==’ operators?

‘=’ is a type of assignment operator that places the value in right to the variable on left, Whereas ‘==’ is a type of relational operator that is used to compare two elements if the elements are equal or not.

4. What is the difference between prefix and postfix operators in C?

Prefix operations are the operations in which the value is returned prior to the operation whereas in postfix operations value is returned after updating the value in the variable.

5. What is the Modulo operator?

The Modulo operator(%) is used to find the remainder if one element is divided by another.

Please Login to comment...

  • Suyog_Sarda
  • Srinivas Nekkanti
  • susobhanakhuli
  • pujasingg43
  • anshikajain26
  • harsh_shokeen
  • naishwarya653
  • suruchikumarimfp4
  • rathoadavinash
  • pkonline2002
  • C-Operators

assignment operators example program in c

Improve your Coding Skills with Practice

The Simple Assignment Operator

  • The equals sign, =, is known as the assignment operator in C
  • The purpose of the assignment operator is to take the value from the right hand side of the operator ( the RHS value ), and store it in the variable on the left hand side ( the LHS ).
  • X = Y + 7; - Valid: The sum of Y plus 7 will be stored in the variable X.
  • X - Y = 7; - Invalid: Although this looks like a simple rearrangement of the above, the LHS is no longer a valid storage location.
  • 7 = X - Y; - Invalid: The LHS is now a single entity, but it is a constant whose value cannot be changed.
  • X = X + 7; - Valid: First the original value of X will be added to 7. Then this new total will be stored back into X, replacing the previous value.

Arithmetic Operators, +, -, *, /, %

  • Ex: X = Y + 7;
  • Ex: X = Y - 7;
  • Ex: X = - Y;
  • ( + can also be used as a unary operator, but there is no good reason to do so. )
  • Ex: X = Y * 7;
  • Ex: X = Y / 7;
  • 9 / 10 yields zero, not 0.9 or 1.
  • 17 / 5 yields 3.
  • 9.0 / 10 yields 0.9, because there are two different types involved, and so the "smaller" int of 10 is promoted to a double precision 10.0 before the division takes place.
  • int num = 9.0 / 10; stores 0. The division yields 0.9 as in the above example, but then it is truncated to the integer 0 by the assignment operator that stores the result in the int variable "num".
  • Ex: K = N % 7;
  • 17 % 5 yields 2.
  • 3 % 5 yields 3.
  • Mod only works with integers in C.
  • If N % M is equal to zero, then it means N is evenly divisible by M. ( E.g. if N % 2 is 0, then N is even. )
  • Therefore mod is often used to map a number into a given range.
  • For example, rand( ) % 52 always yields a number in the range 0 to 51 inclusive, suitable for randomly selecting a card from a deck of 52 cards.

Precedence and Associativity

  • Ex: what is the value of the expression 3 + 5 / 2 ?
  • Assignment statements have the lowest precedence of all, so all other operations are performed before the result is assigned.
  • Ex: In the expression 3 * ( 4 + 2 ), the addition is performed first, and then the multiplication.
  • Ex: What is the value of the expression 5 / 3 * 2.0 ?
  • A = B = C = 0;
  • A full table of precedence and associativity is included in any good book on C Programming. There are also many versions of this information on the web.

Abbreviated Assignment Operators

  • E.g. X = X + 7;
  • Because this is so common, there are a number of operators that combine assignment with some other ( binary ) operator.
  • Note that in these combined operators, that there is no space between the = and the other character, e.g. no space between the + and the = in +=.
  • X *= 3 + 4;
  • The above is equivalent to X = X * ( 3 + 4 );, not X = X * 3 + 4;

Auto Increment and Auto Decrement

  • Another operation that is very common in programming is to either increase or decrease an integer by exactly 1, e.g. when counting up or counting down.
  • N++; is equivalent to N += 1; which is equivalent to N = N + 1;
  • N--; is equivalent to N -= 1; which is equivalent to N = N - 1;
  • There are actually two versions of the auto increment/decrement operators, depending on whether the operator appears after the operand ( postfix, e.g. N++ ) or before the operand ( prefix, e.g. ++N ).
  • For stand-alone statement that consist of nothing but the auto increment/decrement there is no difference between the two. Common convention is to use the postfix form, but prefix would work equivalently.
  • If N is originally equal to 3, then the statement X = 2 * N++ * 3; will store 18 in X and then increment N to 4.
  • If N is originally equal to 3, then the statement X = 2 * ++N * 3; will increment N to 4 first, and then store 24 in X.
  • DANGER: If a variable is affected by an auto increment / decrement operator, never use that same variable more than once in the same statement. For example, the result of X = N++ * 3 + N; is undefined, because it is undetermined what value will be used for the second instance of N.
  • ( Compare N++ +J to N+ ++J )

Happy Codings - Programming Code Examples

C Programming Code Examples

Learn c language, assignment operators in c programming language.

This browser is no longer supported.

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

C Compound Assignment

  • 6 contributors

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

expression1 += expression2

can be understood as

expression1 = expression1 + expression2

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

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

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

C Assignment Operators

Submit and view feedback for

Additional resources

  • BYJU'S GATE
  • GATE Study Material
  • GATE Notes For CSE
  • Introduction To C Programming
  • Operators In C

Assignment Operators in C

We use this type of operator to transform as well as assign the values to any variable in an operation. In any given assignment operator, the right side is a value, and the left side is a variable. The value present on the right side of the operator must have the same data type as that of the variable present on the left side. In any other case, the compiler raises an error.

In this article, we will take a look into the Assignment Operators in C according to the GATE Syllabus for CSE (Computer Science Engineering) . Read ahead to know more.

Table of Contents

  • Working Of Assignment Operators In C
  • Example Of Assignment Operators In C
  • Practice Problems On Assignment Operators In C

Types of Assignment Operators in C

An assignment operator is basically a binary operator that helps in modifying the variable to its left with the use of the value to its right. We utilize the assignment operators to transform and assign values to any variables.

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

Here is a table that discusses, in brief, all the Assignment operators that the C language supports:

Example of Assignment Operators in C

Let us look at an example to understand how these work in a code:

#include <stdio.h>

int x = 21;

printf(“Line A – = Example of the Value of y = %d\n”, y );

printf(“Line B – -= Example of the Value of y = %d\n”, y );

printf(“Line C – += Example of the Value of c = %d\n”, c );

printf(“Line D – /= Example of the Value of y = %d\n”, y );

printf(“Line E – *= Example of the Value of y = %d\n”, y );

y <<= 2;

printf(“Line F – <<= Example of the Value of y = %d\n”, y );

printf(“Line G – %= Example of the Value of y = %d\n”, y );

y &= 2;

printf(“Line H – &= Example of the Value of y = %d\n”, y );

y >>= 2;

printf(“Line I – >>= Example of the Value of y = %d\n”, y );

printf(“Line J – |= Example of the Value of y = %d\n”, y );

printf(“Line K – ^= Example of the Value of y = %d\n”, y );

The compilation and execution of the program mentioned above will produce a result as follows:

Line A – = Example of the Value of y = 21

Line B – -= Example of the Value of y = 21

Line C – += Example of the Value of y = 42

Line D – /= Example of the Value of y = 21

Line E – *= Example of the Value of y = 441

Line F – <<= Example of the Value of y = 44

Line G – %= Example of the Value of y = 11

Line H – &= Example of the Value of y = 2

Line I – >>= Example of the Value of y = 11

Line J – |= Example of the Value of y = 2

Line K – ^= Example of the Value of y = 0

Here is another example of how the assignment operators work in the C language:

int y = 10;

printf(“z = x + y = %d \n”,z);

printf(“z += x = %d \n”,z);

printf(“z -= x = %d \n”,z);

printf(“z *= x = %d \n”,z);

printf(“z /= x = %d \n”,z);

printf(“z %= x = %d \n”,z);

c &= x ;

printf(“c &= x = %d \n”,z);

printf(“z ^= x = %d \n”,z);

printf(“z |= x = %d \n”,z);

z <<= 2 ;

printf(“z <<= 2 = %d \n”,z);

z >>= 2 ;

printf(“z >>= 2 = %d \n”,z);

The output generated here will be:

z = x + y = 15

z += x = 20

z -= x = 15

z *= x = 75

z &= x = 0

z ^= x = 10

z |= x = 10

z <<= 2 = 40

z >>= 2 = 10

z >>= 2 = 2

Practice Problems on Assignment Operators in C

1. What would be the output obtained from the program given below?

#include<stdio.h>

p += p += p += 3;

printf(“%d”,p);

Answer – A. 20

p+=p+=p+=3; it can written as p+=p+=p=p+3; p=2; Or, p+=p+=5; p=5; Or, p+=p=5+5; p=5; Or, p+=10; p=10; Or, p=p+10; p=10; Or, p=20. So, finally p=20.

2. Which of these is an invalid type of assignment operator?

D. None of these

Answer – D. None of these

All of these are valid types of assignment operators.

How does the /= operator work? Is it a combination of two other operators?

Yes, the /+ operator is a combination of the = and / operators. The / operator divides the current value of the available variable first on the left using the available value on the right. It then assigns the obtained result to the available variable on the left side.

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

The = operator is the most basic one used in the C language. We use this operator to assign the value available in the right to the value mentioned on the left side of the operator.

Keep learning and stay tuned to get the latest updates on  GATE Exam  along with  GATE Eligibility Criteria ,  GATE 2023 ,  GATE Admit Card ,  GATE Syllabus for CSE (Computer Science Engineering) ,  GATE CSE Notes ,  GATE CSE Question Paper , and more.

Also Explore,

  • Arithmetic Operators in C
  • Bitwise Operators in C
  • Increment and Decrement Operators in C
  • Logical Operators in C
  • Operators in C
  • Relational Operators in C

Leave a Comment Cancel reply

Your Mobile number and Email id will not be published. Required fields are marked *

Request OTP on Voice Call

Post My Comment

assignment operators example program in c

GATE 2024 - Your dream can come true!

Download the ultimate guide to gate preparation.

  • Share Share

Register with BYJU'S & Download Free PDFs

Register with byju's & watch live videos.

  • C Programming
  • Competitive Programming
  • LeetCode Solutions
  • Programming
  • Programming Questions
  • Development
  • Freelancing
  • Miscellaneous

Operators In C

assignment operators example program in c

Introduction

Operators are essential components of any programming language, and C is no exception. In C, operators are symbols or combinations of symbols that perform specific operations on operands. Understanding the different types of operators and how they work is crucial for writing efficient and effective C programs. In this article, we will delve into the world of operators in C, exploring their types, functions, and common use cases.

Arithmetic Operators

Arithmetic operators allow you to perform basic mathematical operations in C. They include addition, subtraction, multiplication, division, and modulus.

Addition (+)

The addition operator, denoted by the plus symbol (+), combines two operands to produce their sum. For example:

Subtraction (-)

The subtraction operator, denoted by the minus symbol (-), subtracts the second operand from the first operand. Here’s an example:

Multiplication (*)

The multiplication operator, represented by the asterisk symbol (*), multiplies two operands together. For instance:

Division (/)

The division operator, indicated by the forward slash symbol (/), divides the first operand by the second operand. Example:

Modulus (%)

The modulus operator, denoted by the percent symbol (%), calculates the remainder of the division between the first operand and the second operand. For instance:

Relational Operators

Relational operators are used to compare two values and determine their relationship. They return a Boolean value (true or false) based on the comparison.

Equal to (==)

The equal to operator checks whether the values of two operands are equal. If they are, it returns true; otherwise, it returns false. Example:

Not equal to (!=)

The not equal to operator verifies if the values of two operands are not equal. It returns true if they are different and false if they are equal. Here’s an example:

Greater than (>)

The greater than operator compares whether the value of the left operand is greater than the value of the right operand. If true, it returns true; otherwise, false. Example

Less than (<)

The less than operator checks if the value of the left operand is less than the value of the right operand. If true, it returns true; otherwise, false. For instance:

Greater than or equal to (>=)

The greater than or equal to operator compares whether the value of the left operand is greater than or equal to the value of the right operand. If true, it returns true; otherwise, false. Example:

Less than or equal to (<=)

The less than or equal to operator checks if the value of the left operand is less than or equal to the value of the right operand. If true, it returns true; otherwise, false. For example:

Logical Operators

Logical operators perform logical operations on Boolean values and return a Boolean result.

Logical AND (&&)

The logical AND operator returns true if both the left and right operands are true. If either or both operands are false, it returns false. Example:

Logical OR (||)

The logical OR operator returns true if either the left or right operand is true. It returns false only if both operands are false. Here’s an example:

Logical NOT (!)

The logical NOT operator reverses the logical state of its operand. If the operand is true, it returns false; if the operand is false, it returns true. Example:

Assignment Operators

Assignment operators are used to assign values to variables.

Simple Assignment (=)

The simple assignment operator assigns the value of the right operand to the left operand. Example:

Compound Assignment (+=, -=, *=, /=, %=)

Compound assignment operators perform an operation and assign the result to the left operand. They combine the arithmetic operator with the assignment operator. For example:

Similarly, the compound assignment operators -= (subtraction), *= (multiplication), /= (division), and %= (modulus) can be used.

Bitwise Operators

Bitwise operators manipulate individual bits of data.

Bitwise AND (&)

The bitwise AND operator performs a bitwise AND operation between the bits of two operands. It returns a result with bits set only where both operands have bits set. Example:

Bitwise OR (|)

The bitwise OR operator performs a bitwise OR operation between the bits of two operands. It returns a result with bits set where at least one of the operands has bits set. Here’s an example:

Bitwise XOR (^)

The bitwise XOR operator performs a bitwise exclusive OR operation between the bits of two operands. It returns a result with bits set where only one of the operands has bits set. Example:

Bitwise NOT (~)

The bitwise NOT operator performs a bitwise negation operation on its operand, flipping all the bits. Example:

Conditional Operator (?:)

The conditional operator, also known as the ternary operator, is a shorthand for the if-else statement. It provides a concise way to write conditional expressions.

Bitwise Shift Operators

Bitwise shift operators shift the bits of a value to the left or right.

Left Shift (<<)

The left shift operator shifts the bits of the left operand to the left by the number of positions specified by the right operand. Example:

Right Shift (>>)

The right shift operator shifts the bits of the left operand to the right by the number of positions specified by the right operand. Here’s an example:

The arithmetic operators in C include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).

Relational operators in C compare two values and return a Boolean result (true or false) based on the comparison. They include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).

Logical operators in C are used to perform logical operations on Boolean values. They include logical AND (&&), logical OR (||), and logical NOT (!).

Assignment operators in C are used to assign values to variables. The simple assignment operator (=) assigns the value of the right operand to the left operand, while compound assignment operators (+=, -=, *=, /=, %=) perform an operation and assign the result to the left operand.

Bitwise operators in C manipulate individual bits of data. They include bitwise AND (&), bitwise OR (|), bitwise XOR (^), and bitwise NOT (~).

The conditional operator (?:) is a shorthand for the if-else statement. It provides a concise way to write conditional expressions.

Operators play a vital role in C programming, allowing you to perform various operations on operands. In this article, we explored different types of operators, including arithmetic, relational, logical, assignment, bitwise, conditional, and bitwise shift operators. Understanding how these operators work and when to use them is essential for writing efficient and expressive C programs.

Remember to use operators wisely and according to the requirements of your program. By harnessing the power of operators, you can manipulate data, make decisions, and perform complex computations in your C programs.

You might also like

Bubble sort in c: a beginner’s guide to sorting algorithms, linear search in c: a beginner’s guide, related posts, priority scheduling program in c: a comprehensive guide, while loop in c programming, recommended, how to convert golang int to string: a step-by-step guide, top javascript frameworks worth using in 2022.

Where Growing Never Stops!

  • Terms of Use

cppreference.com

Assignment operators.

Assignment operators modify the value of the object.

[ edit ] Explanation

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

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

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

[ edit ] Builtin direct assignment

The direct assignment expressions have the form

For the built-in operator, lhs may have any non-const scalar type and rhs must be implicitly convertible to the type of lhs .

The direct assignment operator expects a modifiable lvalue as its left operand and an rvalue expression or a braced-init-list (since C++11) as its right operand, and returns an lvalue identifying the left operand after modification. The result is a bit-field if the left operand is a bit-field.

For non-class types, the right operand is first implicitly converted to the cv-unqualified type of the left operand, and then its value is copied into the object identified by left operand.

When the left operand has reference type, the assignment operator modifies the referred-to object.

If the left and the right operands identify overlapping objects, the behavior is undefined (unless the overlap is exact and the type is the same).

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

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

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

[ edit ] Example

Possible output:

[ edit ] Builtin compound assignment

The compound assignment expressions have the form

The behavior of every builtin compound-assignment expression E1 op = E2 (where E1 is a modifiable lvalue expression and E2 is an rvalue expression or a braced-init-list (since C++11) ) is exactly the same as the behavior of the expression E1 = E1 op E2 , except that the expression E1 is evaluated only once and that it behaves as a single operation with respect to indeterminately-sequenced function calls (e.g. in f ( a + = b, g ( ) ) , the += is either not started at all or is completed as seen from inside g ( ) ).

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

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

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

[ edit ] Defect reports

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

[ edit ] See also

Operator precedence

Operator overloading

  • Todo no example
  • 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 9 July 2023, at 06:09.
  • This page has been accessed 386,947 times.
  • Privacy policy
  • About cppreference.com
  • Disclaimers

Powered by MediaWiki

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

Summary of Operators

The following quick reference summarizes the operators supported by the Java programming language.

Simple Assignment Operator

Arithmetic operators, unary operators, equality and relational operators, conditional operators, type comparison operator, bitwise and bit shift operators.

About Oracle | Contact Us | Legal Notices | Terms of Use | Your Privacy Rights

Copyright © 1995, 2022 Oracle and/or its affiliates. All rights reserved.

IMAGES

  1. C Programming Tutorial

    assignment operators example program in c

  2. C# Assignment Operator

    assignment operators example program in c

  3. Assignment Operators in C Example

    assignment operators example program in c

  4. Operators in C Programming

    assignment operators example program in c

  5. Arithmetic Operators in C Programming

    assignment operators example program in c

  6. Programming in C

    assignment operators example program in c

VIDEO

  1. Operators in C language

  2. C Programs || Operators || Assignment(==) Operator #cprogramming #youtubeshorts

  3. Instructions in C

  4. C++

  5. Assignment Operator in C Programming

  6. The Assignment Operator in c programming

COMMENTS

  1. Assignment Operators in C/C++

    (a += b) can be written as (a = a + b) If initially value stored in a is 5. Then (a += 6) = 11. "-="This operator is combination of '-' and '=' operators.This operator first subtracts the current value of the variable on left from the value on the right and then assigns the result to the variable on the left.

  2. 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. For example: int i = 10; The below table displays all the assignment operators present in C Programming with an example.

  3. Assignment Operators in C with Examples

    Assignment Operators in C with Examples 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.

  4. Assignment Operators in C

    Assignment Operators in C The following table lists the assignment operators supported by the C language − Example Try the following example to understand all the assignment operators available in C − Live Demo

  5. Operators in C

    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. CoursesTutorials Examples Try Programiz PRO Course Index

  6. Assignment and shorthand assignment operator in C

    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.

  7. Assignment Operators in C

    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.

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

  9. C Operators

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

  10. Assignment operators in C

    1. Simple assignment operator ( Example: = ) 2. Compound assignment operators ( Example: +=, -=, *=, /=, %=, &=, ^= ) Example program for C assignment operators: In this program, values from 0 - 9 are summed up and total "45" is displayed as output.

  11. Assignment Operator in C

    Assignment Operator in C. 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 %=.

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

    2 1. Simple Assignment Operator In C 2.1 Syntax 2.2 Example -: 3 2. Compound Assignment Operators In C 3.1 Syntax of Compound Assignment Operators 3.2 Example -: 4 List of Assignment Operators In C 5 Conclusion Assignment Operators In C

  13. Assignment Operator in C

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

  14. Operators in C

    5. Assignment Operators in C. Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise ...

  15. C Programming Course Notes

    Introduction to C Programming Operators The Simple Assignment Operator. The equals sign, =, is known as the assignment operator in C; The purpose of the assignment operator is to take the value from the right hand side of the operator ( the RHS value ), and store it in the variable on the left hand side ( the LHS ). Note the following examples:

  16. Learn C

    The following table lists the assignment operators supported by the C language: =. 10 Best C Programming Courses For Beginners [2023] Simple assignment operator. Assigns values from right side operands to left side operand. +=. Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.

  17. C Compound Assignment

    The result of a compound-assignment operation has the value and type of the left operand. #define MASK 0xff00 n &= MASK; In this example, a bitwise-inclusive-AND operation is performed on n and MASK, and the result is assigned to n. The manifest constant MASK is defined with a #define preprocessor directive. See also. C Assignment Operators

  18. Assignment Operators in C

    GATE GATE Study Material GATE Notes For CSE Introduction To C Programming Operators In C Assignment Operators in C Assignment Operators in C We use this type of operator to transform as well as assign the values to any variable in an operation. In any given assignment operator, the right side is a value, and the left side is a variable.

  19. Different List of Assignment Operators in C

    Examples of Assignment Operators are given below: Example #1 Program to implement the use of = operator: Code: #include<stdio.h> #include<conio.h> int main() { int X, Y, total; printf("Enter the value of X: "); scanf("%d",& X); printf("Enter the value of Y: "); scanf("%d",& Y); total = X + Y; printf("%d", total); return 0; } Output: Example #2

  20. Assignment operators

    Simple assignment. 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).

  21. Operators In C

    They combine the arithmetic operator with the assignment operator. For example: int a = 5; a += 3; // equivalent to a = a + 3; a becomes 8 C. ... Operators play a vital role in C programming, allowing you to perform various operations on operands. In this article, we explored different types of operators, including arithmetic, relational ...

  22. Assignment Operator in C Programming

    In this video, learn Assignment Operator in C Programming | C Programming Tutorial. Find all the videos of the C Programming Complete Course in this playlis...

  23. Assignment operators

    copy assignment operator replaces the contents of the object a with a copy of the contents of b ( b is not modified). For class types, this is a special member function, described in copy assignment operator . move assignment operator replaces the contents of the object a with the contents of b, avoiding copying if possible ( b may be modified).

  24. Summary of Operators (The Java™ Tutorials > Learning the Java Language

    Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. ... The following quick reference summarizes the operators supported by the Java programming language. Simple Assignment Operator = Simple assignment operator Arithmetic Operators