{"id":83177,"date":"2021-08-04T09:00:04","date_gmt":"2021-08-04T03:30:04","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=83177"},"modified":"2021-08-04T09:00:04","modified_gmt":"2021-08-04T03:30:04","slug":"error-handling-in-c","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/","title":{"rendered":"Error Handling in C"},"content":{"rendered":"<p>As we know that C programming language offers various features and functionalities to the programmers. The syntax of this language is easy to understand and easy to implement.<\/p>\n<p>C does not provide direct error handling features but you can do error handling in C through different ways. A good and skilled programmer has to avoid the errors and he also has to test the return values from the functions.<\/p>\n<h3>What is Error Handling in C?<\/h3>\n<p>As we said that you cannot do error handling in C directly. But there are ways through which you can perform error handling. Through error handling, you can check the return values from functions and should take appropriate action depending on the return value.<\/p>\n<p>In C, a lot of function calls return a -1 or NULL value in case of an error and it sets an error code <strong>errno.<\/strong> It indicates that an error has occurred during a function call.<\/p>\n<p>In <strong>Socket programming<\/strong>, the return value of functions such as socket(), listen() etc. are checked to identify if an error is occured or not.<\/p>\n<p><strong>Example:- Socket Programming<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">if ((connect = socket(AF_INET, SOCK_STREAM, 0)) == 0)\n{\n   perror(\"Connection failed!\");\n   exit();\n}\n<\/pre>\n<h3>Different ways to handle the errors in C:-<\/h3>\n<h4>1. errno in c:-<\/h4>\n<p>It is a global variable and it sets the error code. In C, when a function is called then errno is assigned a code or number automatically and this can be used to analyze which type of error is encountered. It is defined in the <strong>errno.h<\/strong> header file.<\/p>\n<p><strong>Following is the list of different errno values:-<\/strong><\/p>\n<table style=\"height: 819px\" width=\"471\">\n<tbody>\n<tr>\n<td><b>Errno Value<\/b><\/td>\n<td><b>What\u2019s the error<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">The operation is not permitted.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">2<\/span><\/td>\n<td><span style=\"font-weight: 400\">No such file or directory.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">3<\/span><\/td>\n<td><span style=\"font-weight: 400\">No such process.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">4<\/span><\/td>\n<td><span style=\"font-weight: 400\">Interrupt system call.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">5<\/span><\/td>\n<td><span style=\"font-weight: 400\">Input\/Output error.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">6<\/span><\/td>\n<td><span style=\"font-weight: 400\">No such device or address.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">7<\/span><\/td>\n<td><span style=\"font-weight: 400\">The argument list is too long.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">8<\/span><\/td>\n<td><span style=\"font-weight: 400\">Exec format error.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">9<\/span><\/td>\n<td><span style=\"font-weight: 400\">Bad file number.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">10<\/span><\/td>\n<td><span style=\"font-weight: 400\">No child process.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">11<\/span><\/td>\n<td><span style=\"font-weight: 400\">Try again.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">12<\/span><\/td>\n<td><span style=\"font-weight: 400\">Out of memory.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">13<\/span><\/td>\n<td><span style=\"font-weight: 400\">Permission denied.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Example:- errno<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n#include &lt;errno.h&gt;\nint main()\n{\n  FILE *file_name;\n  file_name = fopen(\"TechVidvan.txt\", \"r\");\n \n  printf(\"Value of errno: %d\\n \", errno);\n \n  return 0;\n}<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Value of errno: 2<\/div>\n<p>In the above example, if the file \u2018<strong>TechVidvan.txt<\/strong>\u2019 file does not exist then it will return you an error. And the value of errno will be printed which is 2 (No such file or directory).<\/p>\n<h4>2. perror() and strerror() in C:-<\/h4>\n<p>If you want to display the error description then you can make use of two functions which can be used to show a message that is associated with errno value. Below are the two functions:-<\/p>\n<p><strong>a. perror:-<\/strong> With the help of this, you can display a string which you will pass to it. It is followed by a colon, a space and the textual representation of the errno value.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void perror (char *s)\ns: a custom message to be printed before the error message.<\/pre>\n<p><strong>b. trerror:-<\/strong> It is mainly used for returning a pointer to the textual representation of the errno value.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">char *strerror (int error_number)\nerror_number: represents the error number (errno).<\/pre>\n<p><strong>Example:- strerror() and perror()<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n#include &lt;errno.h&gt;\n#include &lt;string.h&gt;\nint main ()\n{\n  FILE *file_name;\n  file_name = fopen(\"TechVidvan.txt\", \"r\");\n  printf(\"The value of errno is %d!\\n\", errno);\n  printf(\"Error message: %s\\n\", strerror(errno)); \/\/ using strerror!\n  perror(\"perror\"); \/\/ using perror!\n  return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">perror: No such file or directory<br \/>\nThe value of errno is 2!<br \/>\nError message: No such file or directory<\/div>\n<h4>3. Exit Status in C:-<\/h4>\n<p>In C, you can also make use of the exit status constants in the exit() function to indicate successful or unsuccessful termination of the program code.<\/p>\n<p>There are two constants such as <strong>EXIT_SUCCESS<\/strong> and <strong>EXIT_FAILURE.<\/strong> These are macros which are defined in the <strong>stdlib.h<\/strong> header file.<\/p>\n<p><strong>Example:- Exit Status<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n#include &lt;errno.h&gt;\n#include &lt;string.h&gt;\n#include &lt;stdlib.h&gt;\n \nint main ()\n{\n  FILE *file_name;\n  file_name = fopen (\"TechVidvan.txt\", \"rb\");\n  if (file_name == NULL)\n  {\n    \tprintf(\"The value of errno is %d!\\n\", errno);\n    \tprintf(\"The error is: %s\\n\", strerror(errno));\n    \texit(EXIT_FAILURE);\n  }\n  else\n  {\n    \tfclose(file_name);\n    \texit(EXIT_SUCCESS);\n  }\nreturn 0;\n}<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">The value of errno is 2!<br \/>\nThe error is: No such file or directory<\/div>\n<p>In the above example, if the file \u2018TechVidvan.txt\u2019 exists then it will exit with success. And if it does not exist then it will exit with failure and it will display the <strong>errno<\/strong> value and an error message saying No such file or directory.<\/p>\n<h4>4. Divide by zero errors in C:-<\/h4>\n<p>In this situation, you cannot handle the error. Division by zero will lead to undefined behaviour. All you can do is to check the value of the divisor before using it in the division.<\/p>\n<p>You can make use of the if condition. If the value of division is zero then simply display a message and return from the function.<\/p>\n<p><strong>Example:- Divide by zero error<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include&lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n \nvoid divide_zero_error(int);\n \nint main()\n{\n  int num = 0;\n  divide_zero_error(num);\n  return 0;\n}\n \nvoid divide_zero_error(int num)\n{\n  float num1;\n \n  if (num==0)\n  {\n    \tprintf(\"You are dividing a number by zero! You will face an error!\\n\");\n    \tfprintf(stderr, \"Division by zero!\\n\");\n    \texit(EXIT_FAILURE);\n  }\n  else\n  {\n    \tnum1 = 10 \/ num;\n    \tprintf(\"Value is: %f\", num1);\n  }\n}\n\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Division by zero!<br \/>\nYou are dividing a number by zero! You will face an error!<\/div>\n<p>If you run the above code then it will print the error message that you have put. Because you are dividing a number by zero.<\/p>\n<h3>Summary<\/h3>\n<p>In this tutorial, we learnt about error handling in C. We discussed what error handling is in C and different ways to handle the errors in C in detail.<\/p>\n<p>We also discussed functions such as perror() and strerror(). Then we talked over how you can prevent the error when you are dividing a number by zero.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As we know that C programming language offers various features and functionalities to the programmers. The syntax of this language is easy to understand and easy to implement. C does not provide direct error&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":83426,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3510],"tags":[3923,3924,3925,3926],"class_list":["post-83177","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-error-handling-in-c","tag-perror-in-c","tag-sterror-in-c","tag-ways-to-handle-error-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Error Handling in C - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about error handling in C. Learn different ways to handle the errors in C. See the functions such as errno, perror() and strerror().\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Error Handling in C - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about error handling in C. Learn different ways to handle the errors in C. See the functions such as errno, perror() and strerror().\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-04T03:30:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Error-Handling-in-C.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Error Handling in C - TechVidvan","description":"Learn about error handling in C. Learn different ways to handle the errors in C. See the functions such as errno, perror() and strerror().","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/","og_locale":"en_US","og_type":"article","og_title":"Error Handling in C - TechVidvan","og_description":"Learn about error handling in C. Learn different ways to handle the errors in C. See the functions such as errno, perror() and strerror().","og_url":"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-08-04T03:30:04+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Error-Handling-in-C.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Error Handling in C","datePublished":"2021-08-04T03:30:04+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/"},"wordCount":763,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Error-Handling-in-C.jpg","keywords":["Error Handling in C","perror() in C","sterror() in C","Ways to handle error in C"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/","url":"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/","name":"Error Handling in C - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Error-Handling-in-C.jpg","datePublished":"2021-08-04T03:30:04+00:00","description":"Learn about error handling in C. Learn different ways to handle the errors in C. See the functions such as errno, perror() and strerror().","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Error-Handling-in-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Error-Handling-in-C.jpg","width":1200,"height":628,"caption":"Error Handling in C"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/error-handling-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Error Handling in C"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/83177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=83177"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/83177\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/83426"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=83177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=83177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=83177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}