Function Prototype In C

Function Prototype In C

A C function prototype is a statement that informs the compiler about a function’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’s defined in the program but are optional if the function definition comes before the function call.

Syntax

return_type function_name(parameter_list);
  1. return_type: 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).
  2. function_name: 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.
  3. parameter_list: 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’t take any parameters.

Example

int func(int, char, char *, float);

Example

Program to demonstrate the function prototype

// C program to illustrate the function prototye
#include <stdio.h>
 
// Function prototype
float calculateRectangleArea(float length, float width);
 
int main()
{
    float length = 5.0;
    float width = 3.0;
 
    // Function call
    float area = calculateRectangleArea(length, width);
 
    printf("The area of the rectangle is: %.2f\n", area);
 
    return 0;
}
 
// Function definition
float calculateRectangleArea(float length, float width)
{
    return length * width;
}

Output

The area of the rectangle is: 15.00

What if the function prototype is not specified?

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.

// C code to check if a file exists or not.
// Prototype of strerror function is not included in the
// code.
 
#include <errno.h>
#include <stdio.h>
 
int main(int argc, char* argv[])
{
    FILE* fp;
 
    fp = fopen(argv[1], "r");
    if (fp == NULL) {
        fprintf(stderr, "%s\n", strerror(errno));
        return errno;
    }
 
    printf("file exist\n");
 
    fclose(fp);
 
    return 0;
}

Here, we provide a filename, which won’t exist in a file system, and check the output of the program on x86_64 architecture.

[narendra@/media/partition/GFG]$ ./file_existence hello.c
Segmentation fault (core dumped)

Reason For the Program to Crash

The code opens a file, checks its existence, and closes it. However, it fails to include the prototype for the strerror 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 strerror returns an integer, leading to issues with the pointer’s size. This underlines the importance of including proper function prototypes and considering data size differences across architectures.

We have to include the “string. h” header file and then, check the output, in order to know whether the program will work correctly and give the following output.

FAQ- Function Prototype In C

Q1. What is the function of the prototype?

Ans. A C function prototype is a statement that informs the compiler about a function’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.

Q2. What are the three prototypes of main?

Ans. The main has only two prototypes: int main (void); int main (int argc, char ** argv);

Q3. What is prototype examples?

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.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment