C की errno.h
In this page:
#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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक दूसरी function call ने इसे बदल दिया उसके बाद
errnoपढ़ना, ताकि code failing function से अब match न करे। errnoइस्तेमाल करने से पहले#include <errno.h>भूल जाना।- call से पहले
errnoको 0 set न करना, ताकि एक पुरानी error value को एक नई समझ लिया जाए।
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