Excellent video, the worst thing that can happen is when the code halts at run time and we dont know what have happened. In this video we know what went wrong. Thank you Kevin.
It's best practice to save errno right after the function call. Many of the functions called in the example also set errno. The function fopen is easy because the return value tells you that you have an error. In many functions the return value says you might have an error and you need to check errno, e.g. strtol. In these cases you want to set errno to zero before the call.
Thanks for the feedback! This is of course just a basic demo to introduce the concept, a best practices video would be a lot longer and would involve more ideas than what you’ve covered in your comment. For example fopen() might be “easy” because the return value tells us that the function fails, but we use ferror() to also get an error code that tells us why something failed so we can handle that specific reason differently. Again this is of course just a basic intro video so we don’t get into that.
Error handling in C programming can be implemented by checking return values from functions, as many functions in C return -1 or NULL in case of errors. By checking these return values, a programmer can detect errors and handle them accordingly. Additionally, creating separate code to handle specific exceptions that may arise can help in recovering from errors without increasing complexity or technical debt. It is important to prevent errors in the first place and handle them appropriately to ensure smooth execution of the program.
From errno manual: On some ancient systems, was not present or did not declare errno, so that it was necessary to declare errno manually (i.e., extern int errno). Do not do this. It long ago ceased to be necessary, and it will cause problems with modern versions of the C library.
I think I may cover that in a future video. :-) They were added in C11 because strerror() isn't thread safe: en.cppreference.com/w/c/string/byte/strerror.
@@tails_the_god So according to this it's in C11, so if the compiler on a Linux machine is using C11, it should be in there: www.blinkstar.cn/posts/2022/09/talking-about-strerror/. But yeah, if your code isn't multithreaded you don't need _s versions.
How does errno know that what failed was file opening? I know you put "if (fh == NULL)" there, but how does it know that file opening failed and not something else? Who sets the errno value
Great question! :-) It is actually the fopen() function that sets errno. It can also access errno and set it to what it wants to when there is an error, and that's what it does.
Great explanation - really turned the light on for me without overloading it with all the bells and whistles - thank you!
You're very welcome, I'm really glad that you enjoyed the video style and explanation! :-)
Excellent video, the worst thing that can happen is when the code halts at run time and we dont know what have happened. In this video we know what went wrong. Thank you Kevin.
Great explanation! Clear and concise, keep up the good work! ☺
Thank you very much Milan! I'm glad to hear you enjoyed the explanation. 😀
Thanks a lot ! I was trying to figure out how to do a log file for my programm and you answered almost all my questions !
Awesome, I'm glad to hear that! 🙂
It's best practice to save errno right after the function call. Many of the functions called in the example also set errno. The function fopen is easy because the return value tells you that you have an error. In many functions the return value says you might have an error and you need to check errno, e.g. strtol. In these cases you want to set errno to zero before the call.
Thanks for the feedback! This is of course just a basic demo to introduce the concept, a best practices video would be a lot longer and would involve more ideas than what you’ve covered in your comment. For example fopen() might be “easy” because the return value tells us that the function fails, but we use ferror() to also get an error code that tells us why something failed so we can handle that specific reason differently. Again this is of course just a basic intro video so we don’t get into that.
Great explanation! Your videos are some of the best I've seen! Thanks for sharing!
You're welcome, I'm glad you enjoyed it! :-)
Error handling in C programming can be implemented by checking return values from functions, as many functions in C return -1 or NULL in case of errors. By checking these return values, a programmer can detect errors and handle them accordingly. Additionally, creating separate code to handle specific exceptions that may arise can help in recovering from errors without increasing complexity or technical debt. It is important to prevent errors in the first place and handle them appropriately to ensure smooth execution of the program.
From errno manual:
On some ancient systems, was not present or did not
declare errno, so that it was necessary to declare errno manually
(i.e., extern int errno). Do not do this. It long ago ceased to
be necessary, and it will cause problems with modern versions of
the C library.
best sources of code. thank you portfolio courses and thank you Kevin browne.
You're welcome! :-)
Hi, is "extern in errno" really needed? I do not have it in the code and it works just fine.
Thanks, nice explanation
You’re welcome John, I’m glad you enjoyed it! :-)
Can we write this same program in a linux system
What about like strerror_s?
I think I may cover that in a future video. :-) They were added in C11 because strerror() isn't thread safe: en.cppreference.com/w/c/string/byte/strerror.
@@PortfolioCourses yeah plus I wanted to know how to work around in Linux since it doesn't have strerror_s
I think that if you're using the C11 version of C, it should be available, even on Linux? But I could be incorrect about that.
@@PortfolioCourses nah it's Microsoft extensions only
I guess it won't hurt just not using the _s ones
@@tails_the_god So according to this it's in C11, so if the compiler on a Linux machine is using C11, it should be in there: www.blinkstar.cn/posts/2022/09/talking-about-strerror/. But yeah, if your code isn't multithreaded you don't need _s versions.
How does errno know that what failed was file opening? I know you put "if (fh == NULL)" there, but how does it know that file opening failed and not something else? Who sets the errno value
Great question! :-) It is actually the fopen() function that sets errno. It can also access errno and set it to what it wants to when there is an error, and that's what it does.
@@PortfolioCourses so it only works with standard libc functions isn't it?