Assignment Operators in C refer to the symbols utilized for storing values in
variables. These operators engage in fundamental value assignments and can also combine with arithmetic and bitwise operators. In this article, we will explore what an assignment operator in C entails, the various types with illustrations in C, the chaining of assignment operators, and multiple assignment operators in C involving different operations.
The assignment operator in C serves as a means to allocate a value to a variable. It takes the value on the right-hand side of the operator and assigns it to the variable on the left-hand side.
Syntax:
variable = expression;
In this case, the = symbol acts as the assignment operator, while the expression is evaluated, and its outcome is assigned to the variable on the left.
<
Types of Assignment Operators in C
In C, numerous types of assignment operators exist, consisting of combinations of arithmetic and assignment operators along with bitwise and assignment operators.
Operator Name
Symbol
Description
Syntax Example
Simple Assignment
=
Assigns the value of b to a.
a = b;
Addition and Assignment
+=
Adds b to a and stores the result in a.
a += b;
Subtraction and Assignment
-=
Subtracts b from a and saves the result in a.
a -= b;
Multiplication and Assignment
*=
Multiplies a by b and retains the result in a.
a *= b;
Division and Assignment
/=
Divides a by b and keeps the outcome in a.
a /= b;
Modulus and Assignment
%=
Stores the remainder of a divided by b in a.
a %= b;
Left Shift and Assignment
<<=
Shifts the bits of a to the left by b positions and saves the result in a.
a <<= b;
Right Shift and Assignment
>>=
Shifts the bits of a to the right by b positions and keeps the result in a.
a >>= b;
Bitwise AND and Assignment
&=
Performs bitwise AND between a and b, saving the result in a.
a &= b;
Bitwise XOR and Assignment
^=
Executes bitwise XOR between a and b and retains the result in a.
a ^= b;
Bitwise OR and Assignment
|=
Executes bitwise OR between a and b and stores the result in a.
a |= b
Let’s examine each type briefly along with examples in C:
Simple Assignment (=) Operator
The simple assignment operator (=) in C is employed to assign values to variables. It allocates the value to the variable situated on the left side.
Example:
C
Code Copied!
var isMobile = window.innerWidth
“““html
“);
editor97115.setValue(decodedContent); // Establish the default text
editor97115.clearSelection();
editor97115.setOptions({
maxLines: Infinity
});
function decodeHTML97115(input) {
var doc = new DOMParser().parseFromString(input, “text/html”);
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard97115() {
const code = editor97115.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert(“Code copied to clipboard!”);
function closeoutput97115() {
var code = editor97115.getSession().getValue();
jQuery(".maineditor97115 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn97115").addEventListener("click", copyCodeToClipboard97115);
document.getElementById("runBtn97115").addEventListener("click", runCode97115);
document.getElementById("closeoutputBtn97115").addEventListener("click", closeoutput97115);
Output:
The code illustrates how the straightforward assignment operator allocates values to the variables x and y, computes their sum into z, and then outputs the values to the console.
Addition and Assignment (+=) Operator
The addition and assignment operator (+=) in C adds the value on the right side to the variable on the left and then stores the result in that same variable.
Example:
C
Code Copied!
var isMobile = window.innerWidth ");
editor29421.setValue(decodedContent); // Establish the default text
editor29421.clearSelection();
editor29421.setOptions({
maxLines: Infinity
});
function decodeHTML29421(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard29421() {
const code = editor29421.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function closeoutput29421() {
var code = editor29421.getSession().getValue();
jQuery(".maineditor29421 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn29421").addEventListener("click", copyCodeToClipboard29421);
document.getElementById("runBtn29421").addEventListener("click", runCode29421);
document.getElementById("closeoutputBtn29421").addEventListener("click", closeoutput29421);
Output:
``````html
The snippet illustrates how the addition and assignment operator operates to increase the value 10 to the variable a and concurrently modifies its value from 5 to 15.
Subtraction and Assignment (-=) Operator
The subtraction and assignment operator (-=) in C detracts the right-hand value from the left-hand variable and subsequently saves the outcome in the same variable.
Example:
C
Code Copied!
var isMobile = window.innerWidth ");
editor93726.setValue(decodedContent); // Set the default text
editor93726.clearSelection();
editor93726.setOptions({
maxLines: Infinity
});
function decodeHTML93726(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard93726() {
const code = editor93726.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
jQuery(".maineditor93726 .copymessage").show();
setTimeout(function() {
jQuery(".maineditor93726 .copymessage").hide();
}, 2000);
}).catch(err => {
console.error("Error copying code: ", err);
});
}
function runCode93726() {
var code = editor93726.getSession().getValue();
function closeoutput93726() {
var code = editor93726.getSession().getValue();
jQuery(".maineditor93726 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn93726").addEventListener("click", copyCodeToClipboard93726);
document.getElementById("runBtn93726").addEventListener("click", runCode93726);
document.getElementById("closeoutputBtn93726").addEventListener("click", closeoutput93726);
Output:
The code demonstrates how the subtraction and assignment operator functions to remove the value 4 from the variable a, with the outcome of 16 being displayed in the console.
Multiplication and Assignment (*=) Operator
The multiplication and assignment operator (*=) in C multiplies the left-side variable by the right-side value and then retains the result in the same variable.
function hideOutput85459() {
var code = editor85459.getSession().getValue();
jQuery(".maineditor85459 .code-editor-output").hide();
}
// Bind event handlers to the buttons
document.getElementById("copyBtn85459").addEventListener("click", copyCodeToClipboard85459);
document.getElementById("runBtn85459").addEventListener("click", executeCode85459);
document.getElementById("closeoutputBtn85459").addEventListener("click", hideOutput85459);
Output:
The code demonstrates how the multiplication and assignment operator is utilized to multiply the variable a by the value 4, subsequently printing the outcome, 24, to the console.
Division and Assignment (/=) Operator
The division and assignment (/=) operator in C divides the variable on the left by the value on the right and then saves the result in the same variable.
Example:
C
Code Duplicated!
var isMobile = window.innerWidth ");
editor51590.setValue(decodedContent); // Set the initial text
editor51590.clearSelection();
editor51590.setOptions({
maxLines: Infinity
});
function decodeHTML51590(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard51590() {
const code = editor51590.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function hideOutput51590() {
var code = editor51590.getSession().getValue();
jQuery(".maineditor51590 .code-editor-output").hide();
}
// Bind event handlers to the buttons
document.getElementById("copyBtn51590").addEventListener("click", copyCodeToClipboard51590);
document.getElementById("runBtn51590").addEventListener("click", executeCode51590);
document.getElementById("closeoutputBtn51590").addEventListener("click", hideOutput51590);
Output:
The code illustrates how the division and assignment operator operates by dividing the left-side variable a by the right-side value 4, then the result 5 is printed to the console.
Modulus and Assignment (%=) Operator
The modulus and assignment operator (%=) in C computes the remainder when the left-side variable is divided by the right-hand value and assigns the result to the same variable.
Example:
C
Code Duplicated!
``````html
class="code-editor-output-icon-wrap">
var isMobile = window.innerWidth “);
editor10189.setValue(decodedContent); // Assign the default text
editor10189.clearSelection();
editor10189.setOptions({
maxLines: Infinity
});
function decodeHTML10189(input) {
var doc = new DOMParser().parseFromString(input, “text/html”);
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard10189() {
const code = editor10189.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert(“Code copied to clipboard!”);
jQuery(“.maineditor10189 .copymessage”).show();
setTimeout(function() {
jQuery(“.maineditor10189 .copymessage”).hide();
}, 2000);
}).catch(err => {
console.error(“Error copying code: “, err);
});
}
function runCode10189() {
var code = editor10189.getSession().getValue();
function closeoutput10189() {
var code = editor10189.getSession().getValue();
jQuery(".maineditor10189 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn10189").addEventListener("click", copyCodeToClipboard10189);
document.getElementById("runBtn10189").addEventListener("click", runCode10189);
document.getElementById("closeoutputBtn10189").addEventListener("click", closeoutput10189);
Result:
The code illustrates how the modulus assignment operator is utilized to keep the remainder 2 of the division in a variable a, which is then displayed in the console.
Left Shift and Assignment (<<=) Operator
The left shift and assignment operator (<<=) in C moves the bits of the left-hand variable to the left by the specified number of positions on the right-hand side and saves the outcome in the variable.
Illustration:
C
Code Copied!
var isMobile = window.innerWidth ");
editor30820.setValue(decodedContent); // Assign the default text
editor30820.clearSelection();
editor30820.setOptions({
maxLines: Infinity
});
function decodeHTML30820(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard30820() {
const code = editor30820.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
jQuery(".maineditor30820 .copymessage").show();
setTimeout(function() {
jQuery(".maineditor30820 .copymessage").hide();
}, 2000);
}).catch(err => {
console.error("Error copying code: ", err);
});
}
function runCode30820() {
var code = editor30820.getSession().getValue();
function closeoutput57908() {
var code = editor57908.getSession().getValue();
jQuery(".maineditor57908 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn57908").addEventListener("click", copyCodeToClipboard57908);
document.getElementById("runBtn57908").addEventListener("click", runCode57908);
document.getElementById("closeoutputBtn57908").addEventListener("click", closeoutput57908);
Output:
This code illustrates how the left-shift and assignment operator is utilized to shift the bits of the variable a to the left by one spot, and subsequently, the result 10 is displayed in the console.
Right Shift and Assignment (>>=) Operator
The right shift and assignment operator (>>=) in C is utilized to shift the bits of the left variable to the right by the designated number of spots on the right and saves the outcome back into the variable.
Example:
C
Code Copied!
var isMobile = window.innerWidth {
// alert("Code copied to clipboard!");
function closeoutput74188() {
var code = editor74188.getSession().getValue();
jQuery(".maineditor74188 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn74188").addEventListener("click", copyCodeToClipboard74188);
document.getElementById("runBtn74188").addEventListener("click", runCode74188);
document.getElementById("closeoutputBtn74188").addEventListener("click", closeoutput74188);
Output:
The script illustrates how the bitwise AND assignment operator is utilized to carry out a bitwise AND operation between a and b, storing the resulting value 8 in a.
Bitwise XOR and Assignment (^=) Operator
The bitwise XOR and assignment operator (^=) in C is applied to execute a bitwise XOR function between the left variable and the right value, subsequently saving the outcome in the variable.
Example:
C
Code Copied!
var isMobile = window.innerWidth ");
editor67227.setValue(decodedContent); // Initialize with text
editor67227.clearSelection();
editor67227.setOptions({
maxLines: Infinity
});
function decodeHTML67227(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to duplicate code to clipboard
function copyCodeToClipboard67227() {
const code = editor67227.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function closeoutput67227() {
var code = editor67227.getSession().getValue();
jQuery(".maineditor67227 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn67227").addEventListener("click", copyCodeToClipboard67227);
document.getElementById("runBtn67227").addEventListener("click", runCode67227);
document.getElementById("closeoutputBtn67227").addEventListener("click", closeoutput67227);
Output:
The script demonstrates how the bitwise XOR assignment operator is utilized to conduct a bitwise XOR between a and b, subsequently storing the outcome 6 in a.
Bitwise OR and Assignment (|=) Operator
The bitwise OR and assignment operator (|=) in C operates to execute a bitwise OR function between the left variable and the right value, then saving the result back into the variable.
Example:
``````html
class="main-editor-head"> C
Code Duplicated!
var isMobile = window.innerWidth ");
editor13003.setValue(decodedContent); // Set the default text
editor13003.clearSelection();
editor13003.setOptions({
maxLines: Infinity
});
function decodeHTML13003(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard13003() {
const code = editor13003.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code duplicated to clipboard!");
function closeoutput13003() {
var code = editor13003.getSession().getValue();
jQuery(".maineditor13003 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn13003").addEventListener("click", copyCodeToClipboard13003);
document.getElementById("runBtn13003").addEventListener("click", runCode13003);
document.getElementById("closeoutputBtn13003").addEventListener("click", closeoutput13003);
Outcome:
The snippet demonstrates how the bitwise OR assignment operator is utilized to perform a bitwise OR between a and b, storing the result, 14, in a.
Chaining Assignment Operator in C
A chaining assignment in C allows for assigning the same value to multiple variables in a single statement using the simple assignment operator (=). The assignment operator can be chained because it returns the value it has been assigned.
Instance:
C
Code Duplicated!
var isMobile = window.innerWidth ");
editor4620.setValue(decodedContent); // Set the default text
editor4620.clearSelection();
editor4620.setOptions({
maxLines: Infinity
});
function decodeHTML4620(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard4620() {
const code = editor4620.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code duplicated to clipboard!");
data: {
language: "c",
code: code,
cmd_line_args: "",
variablenames: "",
action:"compilerajax"
},
success:
``````javascript
function(response) {
var myArray = response.split(""");
var data = myArray[1];
jQuery(".output4620").html("
" + data + """);
jQuery(".maineditor4620 .code-editor-output").show();
jQuery("#runBtn4620 i.run-code").hide();
}
function closeoutput4620() {
var code = editor4620.getSession().getValue();
jQuery(".maineditor4620 .code-editor-output").hide();
}
// Assign event listeners to the buttons
document.getElementById("copyBtn4620").addEventListener("click", copyCodeToClipboard4620);
document.getElementById("runBtn4620").addEventListener("click", runCode4620);
document.getElementById("closeoutputBtn4620").addEventListener("click", closeoutput4620);
Output:
The code illustrates how the value 10 is allocated to the variables c, b, and a in a single expression through chaining of assignments.
Multiple Assignments in C Using Various Operations
You have the ability to conduct multiple assignments within one expression where each variable receives a unique value, allowing for diverse operations.
Example:
C
Code Copied!
var isMobile = window.innerWidth ");
editor52352.setValue(decodedContent); // Initialize with default text
editor52352.clearSelection();
editor52352.setOptions({
maxLines: Infinity
});
function decodeHTML52352(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard52352() {
const code = editor52352.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
jQuery(".maineditor52352 .copymessage").show();
setTimeout(function() {
jQuery(".maineditor52352 .copymessage").hide();
}, 2000);
}).catch(err => {
console.error("Error copying code: ", err);
});
}
function closeoutput52352() {
var code = editor52352.getSession().getValue();
jQuery(".maineditor52352 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn52352").addEventListener("click", copyCodeToClipboard52352);
document.getElementById("runBtn52352").addEventListener("click", runCode52352);
document.getElementById("closeoutputBtn52352").addEventListener("click", closeoutput52352);
Output:
The code illustrates how various assignments with different operations are computed within a single expression: a = 5. Subsequently, b is derived as a + 10, and c is derived as b * 2, with the outcomes displayed on the console.
Conclusion
From the preceding discussion, it is evident that assignment operators in C programming serve a crucial function by enabling the storage of values in variables and facilitating operations. There are simple and compound types of assignment operators which assist in optimizing the code and enhancing readability. Hence, by grasping these operators alongside C examples, you can effortlessly compose efficient code utilizing the assignment operators.
Q1. What does = signify in C?
It denotes the assignment operator, which is utilized to allocate a value to a variable.
Q2. Is = equivalent to ==?
No, they differ, as = assigns a value, and == establishes equality.
Q3. Is it possible to assign identical values to multiple variables?
Yes, you can assign the same value to multiple variables through chained assignment like a = b = c = 10;
Q4. Are assignment operators right-associative?
Yes, the assignment operators are right-associative since they evaluate from right to left.
Q5. Can I apply assignment operators across all data types?
Yes, however, bitwise and shift operators can only be applied to integers.
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.