← Back to C Course | Chapter 3: Operators | Lesson 6 of 9

C के Assignment Operators

Assignment operators एक variable में एक value रखते हैं, और short versions इसे अपनी पुरानी value से update करते हैं, जैसे 'मेरे score में 5 जोड़ो'। ये आपको नाम दोहराने से बचाते हैं।
Syntax
c
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 (=)

c
// 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;
}

Add (+=) और Subtract (-=) Shorthands

+= और -= एक value add या subtract करने और result को तुरंत same variable में वापस store करने का shorthand हैं, इसलिए total += 5; exactly total = total + 5; के बराबर है लेकिन लिखने और पढ़ने में छोटा।

उदाहरण: Add (+=) and Subtract (-=) Shorthands

c
// 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;
}

Multiply (*=) और Divide (/=) Shorthands

*= और /= multiplication और division के लिए same shorthand तरीके से काम करते हैं -- price *= 1.1; expression के दोनों तरफ variable नाम दोहराने की ज़रूरत के बिना price पर 10% की increase apply करता है।

उदाहरण: Multiply (*=) and Divide (/=) Shorthands

c
// 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;
}

Modulo Assignment (%=)

%= variable को एक value से divide करता है और remainder को उसी variable में वापस store करता है -- एक rotating counter को एक fixed range में रखने जैसी चीज़ों के लिए useful, जैसे array के size तक पहुँचने के बाद एक index को वापस 0 पर wrap करना।

उदाहरण: Modulo Assignment (%=)

c
// 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;
}

Bitwise Assignment Shorthands

C &=, |=, ^=, <<=, और >>= भी एक bitwise operation apply करने और एक step में result reassign करने के shorthand के तौर पर देता है -- += और -= जैसा same read-modify-write pattern, बस arithmetic की बजाय bit-level operations के लिए।

उदाहरण: Bitwise Assignment Shorthands

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. x += 5 का मतलब होने पर x =+ 5 लिखना, जो x को 5 add करने की बजाय +5 assign करता है।
  2. int variables के साथ /= इस्तेमाल करना और decimals की उम्मीद करना, इसलिए x के 5 होने पर x /= 2 2 देता है।
  3. = (assignment) को == (comparison) से confuse करना, उदाहरण के लिए एक real test की बजाय if (x += 1)।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.