C के Assignment Operators
In this page:
variable = value;
variable += value; // also -= *= /= %=
Simple Assignment (=)
= अपने right side की value को अपने left side के variable में store करता है -- C में सबसे basic operation, और == से अलग जो एक value assign करने की बजाय दो values compare करता है, एक mix-up जो कई subtle bugs cause करता है।
उदाहरण: Simple Assignment (=)
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int x;
x = 10;
// Print formatted text to the screen
printf("%d", x);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Add (+=) और Subtract (-=) Shorthands
+= और -= एक value add या subtract करने और result को तुरंत same variable में वापस store करने का shorthand हैं, इसलिए total += 5; exactly total = total + 5; के बराबर है लेकिन लिखने और पढ़ने में छोटा।
उदाहरण: Add (+=) and Subtract (-=) Shorthands
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare total and set it to 10
int total = 10;
total += 5;
total -= 2;
// Print formatted text to the screen
printf("%d", total);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multiply (*=) और Divide (/=) Shorthands
*= और /= multiplication और division के लिए same shorthand तरीके से काम करते हैं -- price *= 1.1; expression के दोनों तरफ variable नाम दोहराने की ज़रूरत के बिना price पर 10% की increase apply करता है।
उदाहरण: Multiply (*=) and Divide (/=) Shorthands
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare price and set it to 100
float price = 100;
price *= 1.1f;
// Print formatted text to the screen
printf("%.2f", price);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Modulo Assignment (%=)
%= variable को एक value से divide करता है और remainder को उसी variable में वापस store करता है -- एक rotating counter को एक fixed range में रखने जैसी चीज़ों के लिए useful, जैसे array के size तक पहुँचने के बाद एक index को वापस 0 पर wrap करना।
उदाहरण: Modulo Assignment (%=)
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare index and set it to 7
int index = 7;
index %= 5;
// Print formatted text to the screen
printf("%d", index);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Bitwise Assignment Shorthands
C &=, |=, ^=, <<=, और >>= भी एक bitwise operation apply करने और एक step में result reassign करने के shorthand के तौर पर देता है -- += और -= जैसा same read-modify-write pattern, बस arithmetic की बजाय bit-level operations के लिए।
उदाहरण: Bitwise Assignment Shorthands
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare flags and set it to 6
int flags = 6;
flags &= 3;
// Print formatted text to the screen
printf("%d", flags);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
x += 5का मतलब होने परx =+ 5लिखना, जोxको 5 add करने की बजाय +5 assign करता है।- int variables के साथ
/=इस्तेमाल करना और decimals की उम्मीद करना, इसलिएxके 5 होने परx /= 22 देता है। =(assignment) को==(comparison) से confuse करना, उदाहरण के लिए एक real test की बजायif (x += 1)।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: