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

C की errno.h

errno एक shared note है जहाँ C library functions कुछ गलत होने पर एक error code छोड़ते हैं, एक warning light की तरह। आप यह जानने के लिए इसे पढ़ सकते हैं कि क्यों।
Syntax
c
#include <errno.h>

errno = 0;
function_that_may_fail();
if (errno != 0) {
  perror("message");
}

errno क्या है?

errno एक global integer variable है जिसे C library functions internally कुछ गलत होने पर एक specific error code में set करते हैं, आपके program को यह पता लगाने का एक standardized तरीका देते हुए कि किस तरह की failure हुई, सिर्फ यह जानने की बजाय कि एक function ने एक error value return की।

उदाहरण: What is errno?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <errno.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	errno = 0;
	sqrt(-1.0);
	// Print formatted text to the screen
	printf("%d", errno != 0);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Math Errors Trigger करना

कुछ math.h functions आपको mathematically valid domain से बाहर एक argument pass करने पर (जैसे एक negative number का sqrt()) errno को EDOM में set करते हैं या जब correct result represent करने के लिए बहुत बड़ा या छोटा हो तो ERANGE में, आपको एक bad input को एक overflowed result से अलग करने देते हुए।

उदाहरण: Triggering Math Errors

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <errno.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	errno = 0;
	sqrt(-1.0);
	// Print formatted text to the screen
	printf("%d", errno == EDOM);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Error Messages Print करना

strerror() एक errno-style integer code लेता है और उस code का मतलब बताने वाली एक human-readable string का एक pointer return करता है, जो अकेले raw numeric error code print करने से debugging या logging के लिए कहीं ज़्यादा useful है।

उदाहरण: Printing Error Messages

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <errno.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
	errno = ENOENT;
	// Print formatted text to the screen
	printf("%s", strerror(errno));
	// Return 0 to signal that the program finished successfully
	return 0;
}

perror() इस्तेमाल करना

perror() आपकी दी एक message print करता है, उसके बाद एक colon और currently errno में stored किसी भी error का description, आपके अपने context ('failed to open file') को system की explanation ('No such file or directory') के साथ एक line में combine करते हुए।

उदाहरण: Using perror()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <errno.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("nonexistent.txt", "r");
	// Check whether fp == NULL
	if (fp == NULL) {
		perror("failed to open file");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

errno Clear करना

C library functions success पर errno को कभी वापस 0 पर reset नहीं करते, वे इसे सिर्फ failure पर set करते हैं, इसलिए पिछली call से एक stale error code बना रह सकता है और आपको mislead कर सकता है।

जिस operation को check करना है उससे तुरंत पहले errno = 0 set करना यह detect करने का reliable तरीका है कि वह specific call actually fail हुई या नहीं।

उदाहरण: Clearing errno

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <errno.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	errno = 0;
	sqrt(4.0);
	// Print formatted text to the screen
	printf("%d", errno);
	// 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. एक दूसरी function call ने इसे बदल दिया उसके बाद errno पढ़ना, ताकि code failing function से अब match न करे।
  2. errno इस्तेमाल करने से पहले #include <errno.h> भूल जाना।
  3. call से पहले errno को 0 set न करना, ताकि एक पुरानी error value को एक नई समझ लिया जाए।

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.