C के Constants
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
// 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
Define Directive
#define compilation शुरू होने से भी पहले एक simple text substitution perform करता है -- आपकी source में defined name का हर occurrence literally इसकी value से replace हो जाता है, इसलिए compiler original name को कभी देखता ही नहीं।
उदाहरण: Define Directive
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Integer Constants
Integer constants decimal (42), leading 0 के साथ octal (052), या leading 0x के साथ hexadecimal (0x2A) में लिखे जा सकते हैं -- तीनों same value represent करते हैं, और चुनाव सिर्फ इस पर है कि उस context के लिए कौन सा base सबसे clear है।
उदाहरण: Integer Constants
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Real Constants
3.14 जैसे real (floating-point) constants context के आधार पर float या double की तरह store होते हैं, और हमेशा एक decimal point या exponent शामिल करते हैं ताकि compiler इन्हें integer literals न समझ ले।
उदाहरण: Real Constants
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Character Constants
A जैसा एक character constant single quotes में लिखा जाता है (double में नहीं, जो strings के लिए है) और असल में उस character की ASCII integer value का बस एक shorthand है -- A और 65 C में interchangeable हैं।
उदाहरण: Character Constants
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- बाद में एक
constvariable बदलने की कोशिश करना, जो एक compile error है। - एक
#defineline के अंत में एक semicolon लगाना, जो replacement text का हिस्सा बन जाता है। #define PI = 3.14लिखना, जहाँ=replacement का हिस्सा बन जाता है।
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