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

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...

My first C++ program
# include using namespace std; int main() { cout<<"Welcome to C++"; return 0; } Use IDE such as CodeBlocks to run this program. To do this, open CodeBlocks, Goto File->New->Empty File. Copy...

Memory Layout Of C Program
1. Text or Code Segment: Text segment contains machine code of the compiled program. Usually, the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs,...

C programming language basics easy way to learn
Algorithm in ProgrammingIn programming, algorithms are the set of well defined instruction in sequence to solve a program. An algorithm should always have a clear stopping point.Qualities of a good algorithmInputs...

Compiling C program on Linux machine GCC ,GDB
Linux have powerful tool called "Terminal". From "terminal" we can command the machine. For any programming language the required tools are editor, compiler and debugger. Compiler will check the syntax...
N

Narasimha Reddy

0 0
0
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