{"id":2329,"date":"2024-05-10T07:21:04","date_gmt":"2024-05-10T07:21:04","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2329"},"modified":"2024-05-10T07:21:04","modified_gmt":"2024-05-10T07:21:04","slug":"function-prototype-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/function-prototype-in-c\/","title":{"rendered":"Function Prototype In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#function-prototype-in-c\">Function Prototype In C<\/a><\/li><li ><a href=\"#example\">Example<\/a><ul><li ><a href=\"#program-to-demonstrate-the-function-prototype\">Program to demonstrate the function prototype<\/a><\/li><\/ul><\/li><li ><a href=\"#what-if-the-function-prototype-is-not-specified\">What if the function prototype is not specified?<\/a><\/li><li ><a href=\"#reason-for-the-program-to-crash\">Reason For the Program to Crash<\/a><\/li><li ><a href=\"#faq-function-prototype-in-c\">FAQ- Function Prototype In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"function-prototype-in-c\">Function Prototype In C<\/h2>\n\n\n\n<p>A C function prototype is a statement that informs the compiler about a function&#8217;s name, return type, and the number and data types of its parameters. This information allows the compiler to verify that function calls and definitions match in terms of parameters and their data types. Function prototypes are necessary when the function is called before it&#8217;s defined in the program but are optional if the function definition comes before the function call.<\/p>\n\n\n\n<p><strong>Syntax<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return_type function_name(parameter_list);\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>return_type<\/strong>: This specifies the data type of the value that the function returns. It can be any data type, such as int, float, or void (for no return value).<\/li>\n\n\n\n<li><strong>function_name<\/strong>: This is the name of the function, and it serves as its identifier. It should be chosen to reflect the purpose or functionality of the function.<\/li>\n\n\n\n<li><strong>parameter_list<\/strong>: This is the list of parameters that the function expects, enclosed in parentheses. Each parameter consists of its data type and name. You can leave the parentheses empty if the function doesn&#8217;t take any parameters.<\/li>\n<\/ol>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int func(int, char, char *, float);\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"program-to-demonstrate-the-function-prototype\">Program to demonstrate the function prototype<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate the function prototye\n#include &lt;stdio.h&gt;\n \n\/\/ Function prototype\nfloat calculateRectangleArea(float length, float width);\n \nint main()\n{\n    float length = 5.0;\n    float width = 3.0;\n \n    \/\/ Function call\n    float area = calculateRectangleArea(length, width);\n \n    printf(\"The area of the rectangle is: %.2f\\n\", area);\n \n    return 0;\n}\n \n\/\/ Function definition\nfloat calculateRectangleArea(float length, float width)\n{\n    return length * width;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The area of the rectangle is: 15.00\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-if-the-function-prototype-is-not-specified\">What if the function prototype is not specified?<\/h2>\n\n\n\n<p>Ignoring function prototypes in C can lead to different outcomes: warnings, errors, or even strange program behavior. These issues can be hard to detect and fix, making function prototypes essential for robust and maintainable code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C code to check if a file exists or not.\n\/\/ Prototype of strerror function is not included in the\n\/\/ code.\n \n#include &lt;errno.h&gt;\n#include &lt;stdio.h&gt;\n \nint main(int argc, char* argv&#91;])\n{\n    FILE* fp;\n \n    fp = fopen(argv&#91;1], \"r\");\n    if (fp == NULL) {\n        fprintf(stderr, \"%s\\n\", strerror(errno));\n        return errno;\n    }\n \n    printf(\"file exist\\n\");\n \n    fclose(fp);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Here, we provide a filename, which won&#8217;t exist in a file system, and check the output of the program on x86_64 architecture.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;narendra@\/media\/partition\/GFG]$ .\/file_existence hello.c\nSegmentation fault (core dumped)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"reason-for-the-program-to-crash\">Reason For the Program to Crash<\/h2>\n\n\n\n<p>The code opens a file, checks its existence, and closes it. However, it fails to include the prototype for the <code>strerror<\/code> function, which returns a pointer to a character. On x86 architecture, this program works because integers and pointers are both 32-bit wide. But on x86_64 architecture, which uses a 64-bit model, it crashes because the compiler assumes <code>strerror<\/code> returns an integer, leading to issues with the pointer&#8217;s size. This underlines the importance of including proper function prototypes and considering data size differences across architectures.<\/p>\n\n\n\n<p>We have to include the \u201cstring. h\u201d header file and then, check the output, in order to know whether the program will work correctly and give the following output.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-function-prototype-in-c\">FAQ- Function Prototype In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1696503641973\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is the function of the prototype?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. A C function prototype is a statement that informs the compiler about a function&#8217;s name, its return type, and the number and data types of its parameters. This information allows the compiler to verify that function calls and definitions match in terms of parameters and their data types, ensuring type safety and correctness in the program.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696503675127\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What are the three prototypes of main?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The main has only two prototypes:\u00a0<strong>int main (void);<\/strong>\u00a0<strong>int main (int argc, char ** argv);<\/strong><\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696503685855\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is prototype examples?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Examples of prototypes can include wireframes, slides, landing pages, working models, interactive frontends, and videos. These prototypes serve as visual or functional representations used to convey or test ideas, designs, or concepts before final implementation. Each type of prototype serves a specific purpose in its respective field.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Function Prototype In C A C function prototype is a statement that informs the compiler about a function&#8217;s name, return type, and the number and data types of its parameters. This information allows the compiler to verify that function calls and definitions match in terms of parameters and their data types. Function prototypes are necessary &#8230; <a title=\"Function Prototype In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/function-prototype-in-c\/\" aria-label=\"More on Function Prototype In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5349,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[420],"class_list":["post-2329","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-function-prototype-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2329"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2329\/revisions"}],"predecessor-version":[{"id":10651,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2329\/revisions\/10651"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5349"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}