← Back to C Course | Chapter 1: Introduction & Basics | Lesson 18 of 21

C में Variable Values बदलना

एक variable की value को किसी भी समय = से reassign किया जा सकता है, compound operators से उसकी अपनी current value के आधार पर modify किया जा सकता है, या किसी function को pass किए गए pointer के जरिए indirectly बदला जा सकता है।
Syntax
c
variable_name = new_value;
variable_name += amount;  // also -= *= /=

एक Variable को Reassign करना

एक variable की value इसे declare होने के बाद किसी भी point पर बस = operator से एक नई value assign करके बदली जा सकती है, और नई value वहाँ पहले जो भी store था उसे पूरी तरह replace कर देती है।

उदाहरण: Reassigning a Variable

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	x = 10;
	// Print formatted text to the screen
	printf("%d", x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Arithmetic से Values बदलना

एक variable की value अक्सर उसकी अपनी current value के आधार पर बदली जाती है, जैसे इसमें कुछ add करना, और C += और -= जैसे compound assignment operators एक step में पढ़ने, modify करने, और वापस store करने के shorthand की तरह देता है।

उदाहरण: Changing Values with Arithmetic

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;
	// Print formatted text to the screen
	printf("%d", total);
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक Variable की Value दूसरे में Copy करना

एक variable की value को दूसरे को assign करना उस specific moment पर वह value copy करता है; फिर दोनों variables independent होते हैं, और बाद में एक बदलने का दूसरे पर कोई असर नहीं होता।

उदाहरण: Copying One Variable's Value to Another

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare a and set it to 5
	int a = 5;
	// Declare b and set it to a
	int b = a;
	b = 10;
	// Print formatted text to the screen
	printf("%d %d", a, b);
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक Function में Variable की Value बदलना

एक function सिर्फ अपने caller से belong करने वाला एक variable तभी बदल सकता है जब उसे उस variable का एक pointer मिले; pointer के बिना, function को सिर्फ एक copy मिलती है और कोई भी changes सिर्फ function तक local होते हैं।

उदाहरण: Changing a Variable's Value in a Function

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void increment(int *x) {
	*x = *x + 1;
}
// Program execution starts in main()
int main() {
	// Declare value and set it to 5
	int value = 5;
	increment(&value);
	// Print formatted text to the screen
	printf("%d", value);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Constants बदले नहीं जा सकते

const qualifier से declared एक variable एक बार initialize हो सकता है लेकिन बाद में कभी reassign नहीं हो सकता, और ऐसा करने की कोशिश एक compile-time error है, const को यह guarantee करने का एक तरीका बनाते हुए कि एक value fixed रहे।

उदाहरण: Constants Cannot Be Changed

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare MAX and set it to 100
	const int MAX = 100;
	// Print formatted text to the screen
	printf("%d", MAX);
	// 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. assign करने के लिए 5 = x; लिखना, जब variable को left side पर होना ज़रूरी है।
  2. यह उम्मीद करना कि एक function caller का variable बदल देगा जब उसे एक copy मिली थी।
  3. = (assignment) को == (comparison) से confuse करना।

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.