← Back to C Course | Chapter 12: Advanced Topics | Lesson 15 of 20

C की assert.h

assert एक check है जो आप code में रखते हैं जो program को एक message के साथ रोक देता है अगर आपकी मानी गई कोई बात गलत निकले, एक smoke alarm की तरह। यह bugs जल्दी ढूंढने में मदद करता है।
Syntax
c
#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?

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

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

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

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)

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

Pointer Validity Assert करना

एक pointer dereference करने से ठीक पहले assert(ptr != NULL); रखना development के दौरान इसके होते ही एक NULL-pointer bug पकड़ने का एक तेज़, self-documenting तरीका है, कई function calls बाद होने वाले किसी mysterious crash को track करने की बजाय।

उदाहरण: Asserting Pointer Validity

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

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

c
// 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;
}
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. assert के अंदर side effects वाला code रखना, जो NDEBUG defined होने पर गायब हो जाता है।
  2. user input check करने के लिए assert इस्तेमाल करना, जब यह programmer errors के लिए है और compile out हो सकता है।
  3. assert macro इस्तेमाल करने से पहले #include <assert.h> भूल जाना।

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.