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)
for Assignment operators |
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
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:
Here is a table that discusses, in brief, all the Assignment operators that the C language supports:
basic assignment | = | p becomes equal to q | N/A | p = q |
addition assignment | += | The addition of p and q becomes equal to p | p = p + q | p += q |
subtraction assignment | -= | The subtraction of q from p becomes equal to p | p = p – q | p -= q |
multiplication assignment | *= | The product of p and q becomes equal to p | p = p * q | p *= q |
division assignment | /= | The division of p by q becomes equal to p | p = p / q | p /= q |
modulo assignment | %= | The remainder of p divided by q becomes equal to p | p = p % q | p %= q |
bitwise AND assignment | &= | The bitwise AND of p and q becomes equal to p | p = p & q | p &= q |
bitwise OR assignment | |= | The bitwise OR of p and q becomes equal to p | p = p | q | p |= q |
bitwise XOR assignment | ^= | The bitwise XOR of p and q becomes equal to p | p = p ^ q | p ^= q |
bitwise left shift assignment | <<= | p left shifted by q becomes equal to p | p = p << q | p <<= q |
bitwise right shift assignment | >>= | p right shifted by q becomes equal to p | p = p >> q | p >>= q |
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
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.
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.
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,
GATE Related Links | |
Your Mobile number and Email id will not be published. Required fields are marked *
Request OTP on Voice Call
Post My Comment
Download the ultimate guide to gate preparation, register with byju's & download free pdfs, register with byju's & watch live videos.
Using assignment operators, we can assign value to the variables.
Equality sign (=) is used as an assignment operator in C.
Here, value 5 has assigned to the variable var.
Here, value of a has assigned to the variable b . Now, both a and b will hold value 10 .
Basically, the value of right-side operand will be assigned to the left side operand.
Operator | Meaning | Example |
---|---|---|
| L=L+R | same as a=a+b |
| L=L-R | same as a=a-b |
| L=L*R | same as a=a*b |
| L=L/R | same as a=a/b |
| L=L%R | same as a=a%b |
C functions, c dynamic memory, c structure union, c file handling, c preprocessor, c command line, c programming test, c interview.
There are different kinds of the operators, such as arithmetic, relational, bitwise, assignment, etc., in the C programming language. The assignment operator is used to assign the value, variable and function to another variable. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=.
It is the operator used to assign the right side operand or variable to the left side variable.
Let's create a program to use the simple assignment operator in C.
The operator is used to add the left side operand to the left operand and then assign results to the left operand.
Let's create a program to use the Plus and assign operator in C.
The operator is used to subtract the left operand with the right operand and then assigns the result to the left operand.
Let's create a program to use the Subtract and Assign (-=) operator in C.
The operator is used to multiply the left operand with the right operand and then assign result to the left operand.
Let's create a program to use the multiply and assign operator (*=) in C.
An operator is used between the left and right operands, which divides the first number by the second number to return the result in the left operand.
Let's create a program to use the divide and assign operator (/=) in C.
An operator used between the left operand and the right operand divides the first number (n1) by the second number (n2) and returns the remainder in the left operand.
Let's create a program to use the divide and assign operator (%=) in C.
|
Transact-SQL
Reinforcement Learning
R Programming
React Native
Python Design Patterns
Python Pillow
Python Turtle
Verbal Ability
Interview Questions
Company Questions
Artificial Intelligence
Cloud Computing
Data Science
Machine Learning
Data Structures
Operating System
Computer Network
Compiler Design
Computer Organization
Discrete Mathematics
Ethical Hacking
Computer Graphics
Software Engineering
Web Technology
Cyber Security
C Programming
Control System
Data Mining
Data Warehouse
prev next, assignment operators in c:.
In C programs, values for the variables are assigned using assignment operators.
main() Total=0,i; (i=0;i<10;i++) +=i; // This is same as Total = Toatal+i ("Total = %d", Total); |
Click on each operator name below for detailed description and example programs.
These are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus | |
These are used to assign the values for the variables in C programs. | |
In C++, the assignment operator forms the backbone of many algorithms and computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language that is used to assign some value to the variables in C++ or in other words, it is used to store some kind of information.
The right-hand side value will be assigned to the variable on the left-hand side. The variable and the value should be of the same data type.
The value can be a literal or another variable of the same data type.
In C++, the assignment operator can be combined into a single operator with some other operators to perform a combination of two operations in one single statement. These operators are called Compound Assignment Operators. There are 10 compound assignment operators in C++:
Lets see each of them in detail.
In C++, the addition assignment operator (+=) combines the addition operation with the variable assignment allowing you to increment the value of variable by a specified expression in a concise and efficient way.
This above expression is equivalent to the expression:
The subtraction assignment operator (-=) in C++ enables you to update the value of the variable by subtracting another value from it. This operator is especially useful when you need to perform subtraction and store the result back in the same variable.
In C++, the multiplication assignment operator (*=) is used to update the value of the variable by multiplying it with another value.
The division assignment operator divides the variable on the left by the value on the right and assigns the result to the variable on the left.
The modulus assignment operator calculates the remainder when the variable on the left is divided by the value or variable on the right and assigns the result to the variable on the left.
This operator performs a bitwise AND between the variable on the left and the value on the right and assigns the result to the variable on the left.
The bitwise OR assignment operator performs a bitwise OR between the variable on the left and the value or variable on the right and assigns the result to the variable on the left.
The bitwise XOR assignment operator performs a bitwise XOR between the variable on the left and the value or variable on the right and assigns the result to the variable on the left.
The left shift assignment operator shifts the bits of the variable on the left to left by the number of positions specified on the right and assigns the result to the variable on the left.
The right shift assignment operator shifts the bits of the variable on the left to the right by a number of positions specified on the right and assigns the result to the variable on the left.
Also, it is important to note that all of the above operators can be overloaded for custom operations with user-defined data types to perform the operations we want.
Similar reads.
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 |
If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected]
If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected]
Top references, top examples, get certified.
IMAGES
VIDEO
COMMENTS
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. This operator first adds the current ...
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. ... Example = Simple assignment operator. Assigns values from right side operands to left side operand: C = A + B will ...
The Assignment operators in C are some of the Programming operators that are useful for assigning the values to the declared variables. Equals (=) operator is the most commonly used assignment operator. For example: int i = 10; The below table displays all the assignment operators present in C Programming with an example. C Assignment Operators.
C supports following Assignment operators: 1. Simple Assignment = Operator Example. This is one of the simplest assignment operator, it simply assigns the right side value to the left side operand. #include <stdio.h> int main () { int n; //integer variable char ch; //character variable float f; //float variable // Simple assignment operator to ...
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.
Assignment Operators in C are used to assign values to the variables. The left side operand is called a variable and the right side operand is the value. The value on the right side of the "=" is assigned to the variable on the left side of "=". In this C tutorial, we'll understand the types of C programming assignment operators with examples.
Summary: in this tutorial, you'll learn about the C assignment operators and how to use them effectively.. Introduction to the C assignment operators. An assignment operator assigns the vale of the right-hand operand to the left-hand operand. The following example uses the assignment operator (=) to assign 1 to the counter variable:
Assignment operators are used in programming toassign valuesto variables. We use an assignment operator to store and update data within a program. They enable programmers to store data in variables and manipulate that data. The most common assignment operator is the equals sign (=), which assigns the value on the right side of the operator to ...
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.
Example of Assignment Operator in C. For example, consider the following line of code: int a = 10 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 ...
Assignment operators are used to assign values to variables and perform operations simultaneously. Here are some commonly used assignment operators: Operator Description Example = Assigns the value on the right to the variable on the left. x = 5; += Adds the value on the right to the variable on the left.
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 ...
Assignment Operators In C. Assignment operators is a binary operator which is used to assign values in a variable, with its right and left sides being a one-one operand. The operand on the left side is variable in which the value is assigned and the right side operands can contain any of the constant, variable, and expression. Example -: x = 18 ...
Assignment operators are used to assigning the result of an expression to a variable. Up to now, we have used the shorthand assignment operator "=", which assigns the result of a right-hand expression to the left-hand variable. For example, in the expression x = y + z, the sum of y and z is assigned to x.
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.
A) 4 B) 8 C) 16 D) 32. Answer: C) 16 Explanation: After right-shifting 8 (binary 1000) by one and then left-shifting the result by two, the value becomes 16 (binary 10000). FAQs. Q. How does the /= operator function? Is it a combination of two other operators? A. The /= operator is a compound assignment operator in C++. It divides the left operand by the right operand and assigns the result to ...
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 ...
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 ...
Using assignment operators, we can assign value to the variables. Equality sign (=) is used as an assignment operator in C. Here, value 5 has assigned to the variable var. int a = 10 ; int b = a; printf( "a = %d \t b = %d\n" ,a,b); return 0 ; Here, value of a has assigned to the variable b. Now, both a and b will hold value 10.
Assignment Operator in C is a tutorial that explains how to use the operator that assigns a value to a variable in C programming language. It covers the syntax, types, and examples of assignment operator in C. It also provides a quiz and interview questions to test your knowledge. Learn assignment operator in C from javatpoint, a leading online platform for learning various technologies.
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.
In C++, the addition assignment operator (+=) combines the addition operation with the variable assignment allowing you to increment the value of variable by a specified expression in a concise and efficient way. Syntax. variable += value; This above expression is equivalent to the expression: variable = variable + value; Example.
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: Example. int x = 10;