C की assert.h
In this page:
#include <assert.h>
assert(condition); // aborts if condition is false
Assertion क्या है?
एक assertion उस assumption को document करता है जिस पर आपका code depend कर रहा है और इसे automatically runtime पर check करता है, अगर वह assumption गलत निकले तो तुरंत program halt कर देता है — bug को exactly उस point पर पकड़ते हुए जहाँ यह होती है, इसे आगे चुपचाप data corrupt करने देने की बजाय।
उदाहरण: What is Assertion?
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <assert.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
assert(x > 0);
// Print formatted text to the screen
printf("Assumption held: %d", x);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
assert() Macro
assert.h में defined assert() macro आपके दिए expression को evaluate करता है और, अगर वह expression false (zero) है, failing expression को file name, function, और line number के साथ stderr में print करता है और फिर program तुरंत रोकने के लिए abort() call करता है।
उदाहरण: The assert() Macro
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <assert.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
assert(age >= 0);
// 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
Assertions Disable करना (NDEBUG)
assert.h include करने से पहले NDEBUG macro define करना उस file में हर assert() call को एक no-op बना देता है, जो development और testing के दौरान उन्हें active रखते हुए एक release build से assertion checks (और उनकी runtime cost) निकालने का standard तरीका है।
उदाहरण: Disabling Assertions (NDEBUG)
// Define the macro NDEBUG (replaced by the preprocessor)
#define NDEBUG
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <assert.h>
// Program execution starts in main()
int main() {
assert(1 == 2);
// Print formatted text to the screen
printf("Assertion skipped");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointer Validity Assert करना
एक pointer dereference करने से ठीक पहले assert(ptr != NULL); रखना development के दौरान इसके होते ही एक NULL-pointer bug पकड़ने का एक तेज़, self-documenting तरीका है, कई function calls बाद होने वाले किसी mysterious crash को track करने की बजाय।
उदाहरण: Asserting Pointer Validity
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <assert.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
int *ptr = &x;
assert(ptr != NULL);
// Print formatted text to the screen
printf("%d", *ptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Value Ranges Assert करना
assert(index >= 0 && index < size); जैसी किसी चीज़ में input parameters wrap करना function boundary पर तुरंत out-of-range values पकड़ता है, function की real assumptions को code में explicit बनाते हुए इसकी बजाय कि उन्हें एक unwritten expectation छोड़ दिया जाए।
उदाहरण: Asserting Value Ranges
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <assert.h>
// Program execution starts in main()
int main() {
int arr[5] = {1,2,3,4,5};
// Declare index and set it to 2
int index = 2;
assert(index >= 0 && index < 5);
// Print formatted text to the screen
printf("%d", arr[index]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
assertके अंदर side effects वाला code रखना, जोNDEBUGdefined होने पर गायब हो जाता है।- user input check करने के लिए
assertइस्तेमाल करना, जब यह programmer errors के लिए है और compile out हो सकता है। assertmacro इस्तेमाल करने से पहले#include <assert.h>भूल जाना।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions