What is the diff. to write prototypes inside main() and before ??

This is a discussion on What is the diff. to write prototypes inside main() and before ?? within the C Programming forums, part of the General Programming Boards category; 1: Code: void function(); int main(void){ return 0; } void function(){} 2: Code: int main(void){ void function(); return 0; } ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    88

    Question What is the diff. to write prototypes inside main() and before ??

    1:
    Code:
    void function();
    
    int main(void){
       return 0;
    }
    
    void function(){}
    2:
    Code:
    int main(void){
       void function();
       return 0;
    }
    
    void function(){}
    i've just seen this way...

    what is the difference between 1 & 2?

    and , ask one more , what is the advantage to write prototypes for functions ?? is it just clear to see all functions easily ??

    thanks

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    To see the affects try compiling these two little programs
    Code:
    #include <stdio.h>
    
    void foo(void);
    
    int main(void)
    {
      void bar(void);
      foo();
      return 0;
    }
    
    void foo (void)
    {
      printf("Hello I'm in foo\n");
      bar();
    }
    
    void bar (void)
    {
      printf("Hello I'm in bar\n");
    }
    Code:
    #include <stdio.h>
    
    void foo(void);
    void bar(void);
    
    int main(void)
    {
      foo();
      return 0;
    }
    
    void foo (void)
    {
      printf("Hello I'm in foo\n");
      bar();
    }
    
    void bar (void)
    {
      printf("Hello I'm in bar\n");
    }

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    88
    oh ~~ i see , one is Global, one is not.

    I saw some one write in the 2nd form. (he is not an expert); hehe!

    ths

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    There are times when its not a bad idea to put the prototype inside a function to give it scope. But thats more of use for abstraction.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21