C के Variables
In this page:
data_type variable_name; // declaration
variable_name = value; // assignment
data_type variable_name = value; // declaration with initialization
Variable Declaration
एक variable declare करना compiler को उस data type के लिए सही size की memory का एक block reserve करने को कहता है, और उस memory को एक नाम देता है जिसे आप reference कर सकते हैं -- लेकिन जब तक आप actually एक value assign न करें memory का content undefined रहता है।
उदाहरण: Variable Declaration
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int age;
age = 25;
// Print formatted text to the screen
printf("%d", age);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Variable Initialization
एक variable को उसी समय initialize करना जब आप इसे declare करते हैं (int age = 25;) गलती से बची हुई garbage memory पढ़ने के risk से बचाता है, जो तब होता है अगर आप कोई value assign करने से पहले किसी variable की value इस्तेमाल करने की कोशिश करें।
उदाहरण: Variable Initialization
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
// Print formatted text to the screen
printf("%d", age);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Lvalue और Rvalue
एक lvalue एक addressable memory location वाली चीज़ है (assignment का left side, जैसे एक variable), जबकि एक rvalue एक value है जिसका ज़रूरी नहीं कि कोई persistent address हो (जैसे एक literal number या एक expression का right side)।
उदाहरण: Lvalue and Rvalue
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int x;
x = 5 + 3;
// 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
कई Variables
int a, b, c; declare करना एक line में तीन अलग int variables बनाता है, थोड़ा typing बचाते हुए -- लेकिन हर एक को अभी भी अपनी explicit initialization चाहिए, क्योंकि declaration को group करना उनके बीच एक starting value share नहीं करता।
उदाहरण: Multiple Variables
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int a = 1, b = 2, c = 3;
// Print formatted text to the screen
printf("%d %d %d", a, b, c);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Variable Scope
किसी function के अंदर declared एक variable सिर्फ तब exist करता है और accessible है जब वह function चल रहा हो; हर function के बाहर declared एक variable (एक global) program की पूरी lifetime के लिए persist करता है और हर जगह visible है।
उदाहरण: Variable Scope
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Declare counter and set it to 100
int counter = 100;
void show() {
// Print formatted text to the screen
printf("%d", counter);
}
// Program execution starts in main()
int main() {
show();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक ऐसा variable इस्तेमाल करना जो declared तो था लेकिन कभी initialize नहीं हुआ, जो एक unpredictable value रखता है।
- strict C89 mode में इस्तेमाल के बाद एक variable declare करना, जहाँ declarations block की शुरुआत में आने चाहिए।
- एक
intvariable को एक string assign करना, जैसेint x = "5";, जो एक type mismatch है।
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