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

C के Constants

एक constant एक value है जिसे आप कभी न बदलने का वादा करते हैं, जैसे एक हफ्ते के दिनों की संख्या। C इसे lock करने के लिए const और #define देता है।
Syntax
c
const data_type CONSTANT_NAME = value;

#define CONSTANT_NAME value

Const Keyword

एक variable को const मार्क करना compiler को इसे बाद में reassign करने की किसी भी कोशिश को reject करने के लिए कहता है, ऐसी value को accidental modification को जो कभी बदलनी नहीं चाहिए एक silent runtime bug की बजाय एक compile-time error बनाते हुए।

उदाहरण: Const Keyword

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

Define Directive

#define compilation शुरू होने से भी पहले एक simple text substitution perform करता है -- आपकी source में defined name का हर occurrence literally इसकी value से replace हो जाता है, इसलिए compiler original name को कभी देखता ही नहीं।

उदाहरण: Define Directive

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro PI (replaced by the preprocessor)
#define PI 3.14
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.2f", PI);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Integer Constants

Integer constants decimal (42), leading 0 के साथ octal (052), या leading 0x के साथ hexadecimal (0x2A) में लिखे जा सकते हैं -- तीनों same value represent करते हैं, और चुनाव सिर्फ इस पर है कि उस context के लिए कौन सा base सबसे clear है।

उदाहरण: Integer Constants

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

Real Constants

3.14 जैसे real (floating-point) constants context के आधार पर float या double की तरह store होते हैं, और हमेशा एक decimal point या exponent शामिल करते हैं ताकि compiler इन्हें integer literals न समझ ले।

उदाहरण: Real Constants

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

Character Constants

A जैसा एक character constant single quotes में लिखा जाता है (double में नहीं, जो strings के लिए है) और असल में उस character की ASCII integer value का बस एक shorthand है -- A और 65 C में interchangeable हैं।

उदाहरण: Character Constants

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare letter and set it to 'A'
	char letter = 'A';
	// Print formatted text to the screen
	printf("%c is %d", letter, letter);
	// 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. बाद में एक const variable बदलने की कोशिश करना, जो एक compile error है।
  2. एक #define line के अंत में एक semicolon लगाना, जो replacement text का हिस्सा बन जाता है।
  3. #define PI = 3.14 लिखना, जहाँ = replacement का हिस्सा बन जाता है।

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.