UrbanPro
true

Learn C++ Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Importance Of Function Prototype In C

Sunil Yadav
14/07/2017 0 0

Function prototype tells compiler about number of parameters function takes, data-types of parameters and return type of function. By using this information, compiler cross checks function parameters and their data-type with function definition and function call. If we ignore function prototype, program may compile with warning, and may work properly. But some times, it will give strange output and it is very hard to find such programming mistakes. Let us see with examples:

#include
#include
 
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;
}
 
Above program checks existence of file, provided from command line, if given file is exist, then the program prints “file exist”, otherwise it prints appropriate error message. Let us provide a filename, which does not exist in file system, and check the output of program on x86_64 architecture.
 
[narendra@/media/partition/GFG]$ ./file_existence hello.c
Segmentation fault (core dumped)

Why this program crashed, instead it should show appropriate error message. This program will work fine on x86 architecture, but will crash on x86_64 architecture. Let us see what was wrong with code. Carefully go through the program, deliberately I haven’t included prototype of “strerror()” function. This function returns “pointer to character”, which will print error message which depends on errno passed to this function. Note that x86 architecture is ILP-32 model, means integer, pointers and long are 32-bit wide, that’s why program will work correctly on this architecture. But x86_64 is LP-64 model, means long and pointers are 64 bit wide. In C language, when we don’t provide prototype of function, the compiler assumes that function returns an integer. In our example, we haven’t included “string.h” header file (strerror’s prototype is declared in this file), that’s why compiler assumed that function returns integer. But its return type is pointer to character. In x86_64, pointers are 64-bit wide and integers are 32-bits wide, that’s why while returning from function, the returned address gets truncated (i.e. 32-bit wide address, which is size of integer on x86_64) which is invalid and when we try to dereference this address, the result is segmentation fault.

Now include the “string.h” header file and check the output, the program will work correctly.

[narendra@/media/partition/GFG]$ ./file_existence hello.c
No such file or directory

Consider one more example:

#include
 
int main(void)
{
    int *p = malloc(sizeof(int));
 
    if (p == NULL) {
        perror("malloc()");
        return -1;
    }
 
    *p = 10;
    free(p);
 
    return 0;
}
 
Above code will work fine on IA-32 model, but will fail on IA-64 model. Reason for failure of this code is we haven’t included prototype of malloc() function and returned value is truncated in IA-64 model.
 
source: http://www.geeksforgeeks.org/importance-of-function-prototype-in-c/
0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

C Programming Language Basics - Easy way to learn
Computer - OverviewTodays world is an information-rich world and it has become a necessity for everyone to know about computers. A computer is an electronic data processing device, which accepts and stores...

C program for Beginners
A Program to print 2 integer value. #include<stdio.h> #include<conio.h> main() int a,b,add; a=5; b=3; add=a+b; printf(“ Addition=%d”,&add); getch(); Brief description...
P

Implicit Typecasting & Explicit Typecasting
In C++ it is possible to cast child class reference variable to parent class Reference. When ever we are Converting a derived-class reference or pointer to a base-class reference or pointer is called...

Material Science and Metallurgy
Hello, I would like to show very first how to visualize the subject. For this, we should have total concentration. To avoid obstacle coming in front of us, we will follow the following steps. Close your...

How are C And C++ Different Form Each Other
1.C and C++ both have data types operator and conditional looping control statements. But the difference is that C++ has to object-oriented concepts which C programs do not have. Hence C program is like...
X

Looking for C++ Language Classes?

The best tutors for C++ Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C++ Language with the Best Tutors

The best Tutors for C++ Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more