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 |
In C language, the assignment operator stores a certain value in an already declared variable. A variable in C can be assigned the value in the form of a literal, another variable, or an expression.
The value to be assigned forms the right-hand operand, whereas the variable to be assigned should be the operand to the left of the " = " symbol, which is defined as a simple assignment operator in C.
In addition, C has several augmented assignment operators.
The following table lists the assignment operators supported by the C language −
Operator | Description | Example |
---|---|---|
= | Simple assignment operator. Assigns values from right side operands to left side operand | C = A + B will assign the value of A + B to C |
+= | Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. | C += A is equivalent to C = C + A |
-= | Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. | C -= A is equivalent to C = C - A |
*= | Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. | C *= A is equivalent to C = C * A |
/= | Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. | C /= A is equivalent to C = C / A |
%= | Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. | C %= A is equivalent to C = C % A |
<<= | Left shift AND assignment operator. | C <<= 2 is same as C = C << 2 |
>>= | Right shift AND assignment operator. | C >>= 2 is same as C = C >> 2 |
&= | Bitwise AND assignment operator. | C &= 2 is same as C = C & 2 |
^= | Bitwise exclusive OR and assignment operator. | C ^= 2 is same as C = C ^ 2 |
|= | Bitwise inclusive OR and assignment operator. | C |= 2 is same as C = C | 2 |
The = operator is one of the most frequently used operators in C. As per the ANSI C standard, all the variables must be declared in the beginning. Variable declaration after the first processing statement is not allowed.
You can declare a variable to be assigned a value later in the code, or you can initialize it at the time of declaration.
You can use a literal, another variable, or an expression in the assignment statement.
Once a variable of a certain type is declared, it cannot be assigned a value of any other type. In such a case the C compiler reports a type mismatch error.
In C, the expressions that refer to a memory location are called "lvalue" expressions. A lvalue may appear as either the left-hand or right-hand side of an assignment.
On the other hand, the term rvalue refers to a data value that is stored at some address in memory. A rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.
Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned and cannot appear on the left-hand side. Take a look at the following valid and invalid statements −
In addition to the = operator, C allows you to combine arithmetic and bitwise operators with the = symbol to form augmented or compound assignment operator. The augmented operators offer a convenient shortcut for combining arithmetic or bitwise operation with assignment.
For example, the expression "a += b" has the same effect of performing "a + b" first and then assigning the result back to the variable "a".
Run the code and check its output −
Similarly, the expression "a <<= b" has the same effect of performing "a << b" first and then assigning the result back to the variable "a".
Here is a C program that demonstrates the use of assignment operators in C −
When you compile and execute the above program, it will produce the following result −
Learn C practically and Get Certified .
Popular examples, reference materials, learn c interactively.
The best way to learn C programming is by practicing examples. The page contains examples on basic concepts of C programming. You are advised to take the references from these examples and try them on your own.
All the programs on this page are tested and should work on all platforms.
Want to learn C Programming by writing code yourself? Enroll in our Interactive C Course for FREE.
02 beginner, 03 intermediate, 04 advanced, 05 training programs, c programming assignment operators, free c programming online course with certificate, what is an assignment operator in c, types of assignment operators in c.
Example of simple assignment operator.
+= | addition assignment | It adds the right operand to the left operand and assigns the result to the left operand. |
-= | subtraction assignment | It subtracts the right operand from the left operand and assigns the result to the left operand. |
*= | multiplication assignment | It multiplies the right operand with the left operand and assigns the result to the left operand |
/= | division assignment | It divides the left operand with the right operand and assigns the result to the left operand. |
%= | modulo assignment | It takes modulus using two operands and assigns the result to the left operand. |
&= | bitwise AND assignment | It performs the bitwise AND operation on the variable with the value on the right |
|= | bitwise OR assignment | It performs the bitwise OR operation on the variable with the value on the right |
^= | bitwise XOR assignment | It performs the bitwise XOR operation on the variable with the value on the right |
<<= | bitwise left shift assignment | Shifts the bits of the variable to the left by the value on the right |
>>= | bitwise right shift assignment | Shifts the bits of the variable to the right by the value on the right |
Practice problems on assignment operators in c, 1. what will the value of "x" be after the execution of the following code, 2. after executing the following code, what is the value of the number variable, benefits of using assignment operators, best practices and tips for using the assignment operator, live classes schedule.
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast |
Assignment operators are used for assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error.
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:
2. “+=” : This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. Example:
If initially value stored in a is 5. Then (a += 6) = 11.
3. “-=” This operator is 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:
If initially value stored in a is 8. Then (a -= 6) = 2.
4. “*=” This operator is combination of ‘*’ 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:
If initially value stored in a is 5. Then (a *= 6) = 30.
5. “/=” This operator is combination of ‘/’ 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:
If initially value stored in a is 6. Then (a /= 2) = 3.
Below example illustrates the various Assignment Operators:
Similar reads.
PrepBytes Blog
ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING
Forgot your password?
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
Assignment operator in c.
Last Updated on June 23, 2023 by Prepbytes
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.
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.
For example, consider the following line of code:
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.
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.
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.
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.
Here are 1000 MCQs on C Programming (Chapterwise) .
33. What is meant by ‘a’ in the following C operation?
34. What will be the output of the following C code?
35. What will happen if the following C code is executed?
36. What will be the output of the following C code?
37. What will be the output of the following C code on a 64 bit machine?
38. What will be the output of the following C function?
39. What will be the output of the following C code?
40. Will the following C code compile without any error?
41. What will be the final value of x in the following C code?
42. What will be the output of the following C code? (Initial values: x= 7, y = 8)
43. What will be the output of the following C code considering the size of a short int is 2, char is 1 and int is 4 bytes?
44. What is the difference between the following 2 C codes?
45. What will be the output of the following C code snippet?
46. What will be the value of the following assignment expression?
47. What will be the output of the following C function?
48. What will be the final values of i and j in the following C code?
49. Comment on the following C statement.
50. How many times i value is checked in the following C program?
51. What will be the output of the following C code?
52. What will be the data type returned for the following C function?
53. What is the problem in the following C declarations?
54. Which option should be selected to work the following C expression?
55. What is the meaning of the following C statement?
56. What are the elements present in the array of the following C code?
57. What will be the output of the following C function when EOF returns?
58. Which part of the program address space is p stored in the following C code?
59. Which of the following sequences are unaccepted in C language? a)
59. Comment on the output of following C code.
60. What is the output of this C code?
61. What is the output of this C code?
62. What is the output of this C code?
63. What is the output of this C code?
The section contains C MCQs on variable names, datatypes, constants, declarations, arithmetic operators, relational and logical operators, type conversions, bitwise operators, assignment operators, increment and decrement operators, conditional expressions, evaluation order and precedence.
The section contains C Language multiple choice questions on switch statements, if-then-else statements, for and while loops, break and continue, goto and labels.
The section contains C MCQs on functions basics, external variables, variable scope, static and register variables, automatic variables, c-preprocessor, file inclusion, macro substitution and conditional inclusion.
The section contains C Programming multiple choice questions and answers on pointers and addresses, function arguments, arrays, address arithmetic, character pointers and functions, multidimensional arrays, pointer arrays initialization, command line arguments and complicated declarations.
The section contains C language MCQs on structures basics, functions, arrays of structures, pointer to structires, self referential structures, table lookup, typedefs, unions and bit fields.
The section contains C multiple choice questions and answers on standard input and output, formatted input and output, variable length argument, file access, error handling, line input and output, string operations, character class testing, ungetc, storage management, mathematical functions, random number generation, file operations, printf and scanf.
The section contains C Language MCQs on float datatype and sizeof keyword.
The section contains C multiple choice questions and answers on enums and typedef.
The section contains C MCQs on string operations, character and error handling.
The section contains C multiple choice questions and answers on mathematical functions, general utilities, diagnostics, variable argument lists, non local jumps, localization, signal handling, standard definition, implementation defined limits, date and time functions.
The section contains C MCQs on static and dynamic memory allocation, dma functions, memory leak and dangling pointers.
The section contains C multiple choice questions and answers on pragma, stringizers, conditional preprocessor directives and token concatenation.
The section contains C MCQ questions on inline, endianness, recursion and signed qualifier.
C structures, c reference.
C is a general-purpose programming language that has been widely used for over 50 years.
C is very powerful; it has been used to develop operating systems, databases, applications, etc.
Our "Try it Yourself" editor makes it easy to learn C. You can edit code and view the result in your browser:
Click on the "Try it Yourself" button to see how it works.
We recommend reading this tutorial, in the sequence listed in the left menu.
Test yourself with exercises.
Insert the missing part of the code below to output "Hello World!".
Start the Exercise
Advertisement
Learn by taking a quiz! The quiz will give you a signal of how much you know about C.
Start C Quiz
Track your progress with the free "My Learning" program here at W3Schools.
Log in to your account, and start earning points!
This is an optional feature. You can study at W3Schools without using My Learning.
Learn by examples! This tutorial supplements all explanations with clarifying examples.
See All C Examples
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.
Next: Unions , Previous: Overlaying Structures , Up: Structures [ Contents ][ Index ]
Assignment operating on a structure type copies the structure. The left and right operands must have the same type. Here is an example:
Notionally, assignment on a structure type works by copying each of the fields. Thus, if any of the fields has the const qualifier, that structure type does not allow assignment:
See Assignment Expressions .
When a structure type has a field which is an array, as here,
structure assigment such as r1 = r2 copies array fields’ contents just as it copies all the other fields.
This is the only way in C that you can operate on the whole contents of a array with one operation: when the array is contained in a struct . You can’t copy the contents of the data field as an array, because
would convert the array objects (as always) to pointers to the zeroth elements of the arrays (of type struct record * ), and the assignment would be invalid because the left operand is not an lvalue.
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Get early access and see previews of new features.
The following code gives output : 0 1 and obviously does not raise any error. Does C support the same?
No, C does not support multiple-assignment, nor has it language-level support for tuples.
The above is, considering operator precedence , equivalent to:
Which is equivalent to:
The closest C-equivalent to your Python code would be:
That would be needlessly complex and confusing though, thus prefer separating it into two statements for much cleaner code:
C does not support list assignments as Python does. You need to assign to each variable separately:
No C does not support multiple assignments like this.
Compilation passes since a , b = 0 , 1 is grouped as a, (b = 0), 1 . a and 1 are no-ops but still valid expressions; the expression is equivalent to
with a not changed.
Interestingly, you can achieve your desired notation in C++ with some contrivance and a minor change in the syntax.
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .
COMMENTS
(Note that this sequencing freedom is specific to C language, in which the result of an assignment in an rvalue. In C++ assignment evaluates to an lvalue, which requires "chained" assignments to be sequenced.) ... Because, the multiple assignment translates to: sample2 = 0; sample1 = sample2; So instead of 2 initializations you do only one and ...
But C language treats the multiple assignments like a chain, like this: In C, "X = Y = Z" means that the value of Z must be first assigned to Y, and then the value of Y must be assigned to X. Here is a sample program to see how the multiple assignments in single line work:
Using , operator you can assign a value to a variable multiple times. e.g. K = 20, K = 30; This will assign 30 to K overwriting the previous value of 20. I thought it's invalid to change a variable more than one time in one statement. Yes it leads to undefined behavior if we try to modify a variable more than once in a same C statement but here ...
Before Multiple Assignent a=0,b=0,c=100 After Multiple Assignent a=100,b=100,c=100 Assigning a value to multiple variables of same type By using such kind of expression we can easily assign a value to multiple variables of same data type, for example - if we want to assign 0 to integer variables a , b , c and d ; we can do it by following ...
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 ...
C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It was mainly developed as a system programming language to write an operating system. The main features of C language include low-level access to memory, a simple set of keywords, and a clean style, these features make C language suitable for sys
Simple assignment operator. Assigns values from right side operands to left side operand. C = A + B will assign the value of A + B to C. +=. Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C + A. -=.
Operators are symbols used for performing some kind of operation in C. There are six types of operators, Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, Assignment Operators, and Miscellaneous Operators. Operators can also be of type unary, binary, and ternary according to the number of operators they are using.
Program. C Program to Print an Integer (Entered by the User) C Program to Add Two Integers. C Program to Multiply Two Floating-Point Numbers. C Program to Find ASCII Value of a Character. C Program to Compute Quotient and Remainder. C Program to Find the Size of int, float, double and char. C Program to Demonstrate the Working of Keyword long.
Assignment Operators in C are used to assign values to the variables. They come under the category of binary operators as they require two operands to operate upon. 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 "=".
The concept of operator precedence and associativity in C helps in determining which operators will be given priority when there are multiple operators in the expression. It is very common to have multiple operators in C language and the compiler first evaluates the operater with higher precedence. It helps to maintain the ambiguity of the ...
1. "=": This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. Example: a = 10; b = 20; ch = 'y'; 2. "+=": This operator is combination of '+' and '=' operators.This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.
Solve these beginner friendly problems online to get better at C language. Courses. Learn Python 10 courses. Learn C++ 9 courses. Learn C 9 courses. Learn ... Single vs Multiple Lines Quiz: Easy: Basic Expression Printing Test: Easy: String Formatting Exercise ... Variable Assignment : Easy: What is the output: Easy: Output The Difference - MCQ ...
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 ...
C Exercises. Test Yourself With Exercises. Exercise: Fill in the missing parts to create three variables of the same type, using a comma-separated list: myNum1 = 10 myNum2 = 15 myNum3 = 25; printf("%d", myNum1 + myNum2 + myNum3); Submit Answer ». Start the Exercise. Previous Next .
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. Example: int x = 10; // Assigns the value 10 to the variable "x"
What will be the value of the following assignment expression? (x = foo())!= 1 considering foo() returns 2. a) 2 b) True c) 1 d) 0 View Answer. Answer: c ... Multiple Choice Questions on Control Flow Statements in C. The section contains C Language multiple choice questions on switch statements, if-then-else statements, for and while loops ...
Test yourself with multiple choice questions. Get Certified. Document your knowledge. ... Learn C. C is a general-purpose programming language that has been widely used for over 50 years. C is very powerful; it has been used to develop operating systems, databases, applications, etc.
15.13 Structure Assignment. Assignment operating on a structure type copies the structure. The left and right operands must have the same type. Here is an example: Notionally, assignment on a structure type works by copying each of the fields. Thus, if any of the fields has the const qualifier, that structure type does not allow assignment:
1. No C does not support multiple assignments like this. Compilation passes since a , b = 0 , 1 is grouped as a, (b = 0), 1. a and 1 are no-ops but still valid expressions; the expression is equivalent to. b = 0. with a not changed. Interestingly, you can achieve your desired notation in C++ with some contrivance and a minor change in the syntax.