How can a program be made to print the name of a source file where an error occurs? Ans. The ANSI C standard includes a predefined macro named __FILE__ that can be used to insert the current source code filename in your program. This macro, like the __LINE__ macro, can be very valuable when it comes to debugging your program and checking for logic errors. For instance, the following code, includes the filename as well as the line number when logic errors are trapped: int print_document(char* doc_name, int destination) { switch (destination) { case TO_FILE: print_to_file(doc_name); break; case TO_SCREEN: print_preview(doc_name); break; case TO_PRINTER: print_to_printer(doc_name); break; default: printf('Logic error on line number %d in the file %s!\n', __LINE__, __FILE__); exit(1); } } Now, any erroneous values for the destination parameter can be trapped, and the offending source file and line number can be printed. - Study24x7
Social learning Network
27 Mar 2023 11:43 AM study24x7 study24x7

How can a program be made to print the name of a source file where an error occurs? Ans. The ANSI C standard includes a predefined macro named __FILE__ that can be used to insert the current source code filename in your program. This macro, like the __LINE__ macro, can be very v...

See more

study24x7
Write a comment
Related Questions
500+   more Questions to answer
Most Related Articles