C में Variable Values बदलना
In this page:
variable_name = new_value;
variable_name += amount; // also -= *= /=
एक Variable को Reassign करना
एक variable की value इसे declare होने के बाद किसी भी point पर बस = operator से एक नई value assign करके बदली जा सकती है, और नई value वहाँ पहले जो भी store था उसे पूरी तरह replace कर देती है।
उदाहरण: Reassigning a Variable
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Arithmetic से Values बदलना
एक variable की value अक्सर उसकी अपनी current value के आधार पर बदली जाती है, जैसे इसमें कुछ add करना, और C += और -= जैसे compound assignment operators एक step में पढ़ने, modify करने, और वापस store करने के shorthand की तरह देता है।
उदाहरण: Changing Values with Arithmetic
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक Variable की Value दूसरे में Copy करना
एक variable की value को दूसरे को assign करना उस specific moment पर वह value copy करता है; फिर दोनों variables independent होते हैं, और बाद में एक बदलने का दूसरे पर कोई असर नहीं होता।
उदाहरण: Copying One Variable's Value to Another
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक Function में Variable की Value बदलना
एक function सिर्फ अपने caller से belong करने वाला एक variable तभी बदल सकता है जब उसे उस variable का एक pointer मिले; pointer के बिना, function को सिर्फ एक copy मिलती है और कोई भी changes सिर्फ function तक local होते हैं।
उदाहरण: Changing a Variable's Value in a Function
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Constants बदले नहीं जा सकते
const qualifier से declared एक variable एक बार initialize हो सकता है लेकिन बाद में कभी reassign नहीं हो सकता, और ऐसा करने की कोशिश एक compile-time error है, const को यह guarantee करने का एक तरीका बनाते हुए कि एक value fixed रहे।
उदाहरण: Constants Cannot Be Changed
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- assign करने के लिए
5 = x;लिखना, जब variable को left side पर होना ज़रूरी है। - यह उम्मीद करना कि एक function caller का variable बदल देगा जब उसे एक copy मिली थी।
=(assignment) को==(comparison) से confuse करना।
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers