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

C के Variables

एक variable एक labeled box की तरह है जहाँ आप information रखते हैं। आप बताते हैं कि इसमें किस तरह की चीज़ जाएगी, इसे एक नाम देते हैं, और बाद में अंदर क्या है यह बदल सकते हैं।
Syntax
c
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

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

Variable Initialization

एक variable को उसी समय initialize करना जब आप इसे declare करते हैं (int age = 25;) गलती से बची हुई garbage memory पढ़ने के risk से बचाता है, जो तब होता है अगर आप कोई value assign करने से पहले किसी variable की value इस्तेमाल करने की कोशिश करें।

उदाहरण: Variable Initialization

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

Lvalue और Rvalue

एक lvalue एक addressable memory location वाली चीज़ है (assignment का left side, जैसे एक variable), जबकि एक rvalue एक value है जिसका ज़रूरी नहीं कि कोई persistent address हो (जैसे एक literal number या एक expression का right side)।

उदाहरण: Lvalue and Rvalue

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

कई Variables

int a, b, c; declare करना एक line में तीन अलग int variables बनाता है, थोड़ा typing बचाते हुए -- लेकिन हर एक को अभी भी अपनी explicit initialization चाहिए, क्योंकि declaration को group करना उनके बीच एक starting value share नहीं करता।

उदाहरण: Multiple Variables

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

Variable Scope

किसी function के अंदर declared एक variable सिर्फ तब exist करता है और accessible है जब वह function चल रहा हो; हर function के बाहर declared एक variable (एक global) program की पूरी lifetime के लिए persist करता है और हर जगह visible है।

उदाहरण: Variable Scope

c
// 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;
}
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. एक ऐसा variable इस्तेमाल करना जो declared तो था लेकिन कभी initialize नहीं हुआ, जो एक unpredictable value रखता है।
  2. strict C89 mode में इस्तेमाल के बाद एक variable declare करना, जहाँ declarations block की शुरुआत में आने चाहिए।
  3. एक int variable को एक string assign करना, जैसे int x = "5";, जो एक type mismatch है।

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.