Thread: Why put the prototype inside the function definition?

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    Why put the prototype inside the function definition?

    The following is part of the code found on page 110 in the book "The C Programming Language" by K & R.

    Code:
    void qsort(char *v[], int left, int right)
    {
         int i, last;
         void swap(char *v[], int i, int j);
    
         /*rest of the code*/
    }

    What's the reasoning for having the prototype for swap() inside the function definition of qsort()? Ie, why isn't the prototype for swap() before main()?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    to show that swap function is called only from the qsort probably
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I would never do it that way myself. It should be in a header file that's included by your .c file.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by vart View Post
    to show that swap function is called only from the qsort probably
    Yeah, I think you're right. Here is what happens when I put the prototype printagain() inside the function definition printme1().

    Code:
    [cdalten@localhost oakland]$ more prot.c
    #include <stdio.h>
    
    void printme1(void)
    {
      char arr[] = "test";
      void printagain(char *);
      printf("This is inside printme1()\n");
    
      printagain(arr);
    }
    
    
    void printme2(void)
    {
      char arr[] = "test";
      printf("This is inside printme2()\n");
    
      printagain(arr);
    }
    
    
    void printagain(char *s) {
      printf("%s\n", s);
    }
    
    int main(void)
    {
    
      printme1();
      printme2();
    
      return 0;
    }
    [cdalten@localhost oakland]$ gcc -g -Wall prot.c -o prot
    prot.c: In function ‘printme2’:
    prot.c:18: warning: implicit declaration of function ‘printagain’
    prot.c:6: warning: previous declaration of ‘printagain’ was here
    prot.c:18: error: incompatible implicit declaration of function ‘printagain’
    prot.c:6: error: previous implicit declaration of ‘printagain’ was here
    [cdalten@localhost oakland]$

    And now here is what happens when I put the prototype for printagain() at the very top. Ie, not in the function definitions.

    Code:
    [cdalten@localhost oakland]$ more prot.c
    #include <stdio.h>
    
    void printagain(char *); /*this is no longer inside the function defintion*/
    
    void printme1(void)
    {
      char arr[] = "test";
      printf("This is inside printme1()\n");
    
      printagain(arr);
    }
    
    
    void printme2(void)
    {
      char arr[] = "test";
      printf("This is inside printme2()\n");
    
      printagain(arr);
    }
    
    
    void printagain(char *s) {
      printf("%s\n", s);
    }
    
    int main(void)
    {
    
      printme1();
      printme2();
    
      return 0;
    }
    [cdalten@localhost oakland]$ gcc -g -Wall prot.c -o prot
    [cdalten@localhost oakland]$ ./prot
    This is inside printme1()
    test
    This is inside printme2()
    test
    [cdalten@localhost oakland]$

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    I would never do it that way myself. It should be in a header file that's included by your .c file.
    Not always. Sometimes you want to hide a function so that nobody can call it, but you want to make it available in a specific place. Putting the prototype in a header file announces the function's existence and encourages people to call it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    In the 1st program in post #4, have you tried calling the function printagain from the main function or placing the function printme2 after the definition of function printagain??

    I guess in the above two cases, the errors that were listed will not appear. It all depends on how you place the code.

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by brewbuck View Post
    Not always. Sometimes you want to hide a function so that nobody can call it, but you want to make it available in a specific place. Putting the prototype in a header file announces the function's existence and encourages people to call it.

    But isn't the function made available after it's defined? So would you have includes at the bottom of your C files to include "hidden" functions? Or is there another model for using them?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by neandrake View Post
    But isn't the function made available after it's defined? So would you have includes at the bottom of your C files to include "hidden" functions? Or is there another model for using them?
    Prototypes are scoped, so putting a prototype inside a function does not make it world visible.

  9. #9
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by tabstop View Post
    Prototypes are scoped, so putting a prototype inside a function does not make it world visible.
    I understand, but the function definition puts that function at global scope, correct? That's what my question was asking about and not the prototype. If the function is placed at the bottom of the file, then no other function could see it unless it had its own prototype. But assuming you have multiple functions at the bottom, only the last one would be truly "hidden" as it knows about all the ones above it, right?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  10. #10
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    On page 297 of Prata's "C Primer" he prototypes a function twice, once in the header and then in the main function. No explanation why, confused the hell out of me.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by neandrake View Post
    I understand, but the function definition puts that function at global scope, correct?
    It puts that definition at file scope. If no prototype exists in the header that other source files include, those other source files have no way of calling the function.

  12. #12
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by tabstop View Post
    It puts that definition at file scope. If no prototype exists in the header that other source files include, those other source files have no way of calling the function.
    Then why not just put the prototypes at the top of your c file, why make them only inside the function? I'm missing the point of being able to do this. Also, prototypes have no effect on the stack/heap right; they're just notifications to the compiler?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by neandrake View Post
    Then why not just put the prototypes at the top of your c file, why make them only inside the function? I'm missing the point of being able to do this. Also, prototypes have no effect on the stack/heap right; they're just notifications to the compiler?
    That is true; prototypes make no memory demands.

    To be honest, I've always done as you described (put the helper function before the function that calls it); the only scenario I'm coming up with where it makes a difference is if function A has a variable called foo and function B needs to call a function called foo and people want to avoid confusion. I can't imagine that happens often, though.

  14. #14
    Registered User
    Join Date
    Jan 2010
    Posts
    1
    I also need help with an answer for this, How would you use the c library function to solve a programming problem? Please give three examples.
    Thank you

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Why did you bump a year old thread to ask a random (and vague) programming problem?
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM