prototyping?

Discussion in 'C Programming' started by Zach, Jan 25, 2008.

  1. Zach

    Zach Guest

    Here is my test program (just swaps 2 ints):

    #include <stdio.h>
    #include <stdlib.h>




    int main(void)
    {

    int s1, s2;
    s1 = 3;
    s2 = 4;

    printf("s1 = %d \n", s1);
    printf("s2 = %d \n", s2);

    swap(s1,s2);

    exit(EXIT_SUCCESS);

    }

    swap(int a, int b)
    {
    int temp;

    temp = a;
    a = b;
    b = temp;

    printf("s1 = %d \n", a);
    printf("s2 = %d \n", b);

    return 0;

    }

    When I compile there are no errors and it runs with expected results:

    zu22@netrek:~$ ./foop
    s1 = 3
    s2 = 4
    s1 = 4
    s2 = 3

    But why is it not complaining about no function prototype for
    swap() ?

    Oddly when I do add a function prototype I get an error:
    On line 5 I added:
    swap(int, int);

    When I compile it says:
    swap-foo.c:5: warning: data definition has no type or storage class

    What does this mean?

    I tried using the program protoize but it doesn't do anything:

    zu22@netrek:~$ protoize swap-foo.c
    protoize: compiling 'swap-foo.c'
    protoize: converting file 'swap-foo.c'
    zu22@netrek:~$ diff swap-foo.c swap-foo.c.save
    zu22@netrek:~$

    Zach
    Zach, Jan 25, 2008
    #1
    1. Advertising

  2. Zach

    Mark Bluemel Guest

    Zach wrote:
    > Here is my test program (just swaps 2 ints):
    >
    > #include <stdio.h>
    > #include <stdlib.h>


    > int main(void)
    > {
    >
    > int s1, s2;

    [snip]
    > swap(s1,s2);
    > exit(EXIT_SUCCESS);
    >
    > }
    >
    > swap(int a, int b)


    Note that this implicitly returns int.

    > {

    [Snip]
    > }
    >
    > When I compile there are no errors and it runs with expected results:


    > But why is it not complaining about no function prototype for
    > swap() ?


    In the absence of a prototype, it is assumed that the function returns
    int and default promotion rules are applied to the parameters.

    When I compiled your code with gcc adding -ansi -pedantic -Wall -Wextra
    I got warnings about the implicit declaration of "swap" at line 17 and
    the default return type at line 24.

    > Oddly when I do add a function prototype I get an error:
    > On line 5 I added:
    > swap(int, int);


    > When I compile it says:
    > swap-foo.c:5: warning: data definition has no type or storage class
    >
    > What does this mean?


    If you supply a prototype you must specify the return type.

    > I tried using the program protoize but it doesn't do anything:


    I don't know what protoize is, so can't comment.
    Mark Bluemel, Jan 25, 2008
    #2
    1. Advertising

  3. Zach

    Ed Prochak Guest

    On Jan 25, 8:01 am, Zach <> wrote:
    > Here is my test program (just swaps 2 ints):
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void)
    > {
    >
    >   int s1, s2;
    >   s1 = 3;
    >   s2 = 4;
    >
    >   printf("s1 = %d \n", s1);
    >   printf("s2 = %d \n", s2);
    >
    >   swap(s1,s2);
    >
    >   exit(EXIT_SUCCESS);
    >
    > }
    >
    > swap(int a, int b)
    > {
    >   int temp;
    >
    >   temp = a;
    >   a = b;
    >   b = temp;
    >
    >   printf("s1 = %d \n", a);
    >   printf("s2 = %d \n", b);
    >
    >   return 0;
    >
    > }
    >
    > When I compile there are no errors and it runs with expected results:
    >
    > zu22@netrek:~$ ./foop
    > s1 = 3
    > s2 = 4
    > s1 = 4
    > s2 = 3
    >
    > But why is it not complaining about no function prototype for
    > swap() ?


    It defaults to original C rules, that functions return int and take a
    non-predefined number of parameters.
    >
    > Oddly when I do add a function prototype I get an error:
    > On line 5 I added:
    > swap(int, int);
    >
    > When I compile it says:
    > swap-foo.c:5: warning: data definition has no type or storage class
    >
    > What does this mean?


    Where is line 5 in your program????
    what return type is swap???
    Likely you want
    void swap(int, int);
    or
    int swap(int, int);
    to appear before the start of main.

    >
    > I tried using the program protoize but it doesn't do anything:
    >
    > zu22@netrek:~$ protoize swap-foo.c
    > protoize: compiling 'swap-foo.c'
    > protoize: converting file 'swap-foo.c'
    > zu22@netrek:~$ diff swap-foo.c swap-foo.c.save
    > zu22@netrek:~$
    >
    > Zach


    Try reviewing your C reference manual.

    HTH
    ed
    Ed Prochak, Jan 25, 2008
    #3
  4. Zach

    Zach Guest

    On Jan 25, 8:15 am, Mark Bluemel <> wrote:
    >
    > In the absence of a prototype, it is assumed that the function returns
    > int and default promotion rules are applied to the parameters.


    Hi Mark,

    Oh, I see.

    > When I compiled your code with gcc adding -ansi -pedantic -Wall -Wextra
    > I got warnings about the implicit declaration of "swap" at line 17 and
    > the default return type at line 24.


    Hmm when I do get warning at lines 5 and 25:
    swap-foo.c:5: warning: data definition has no type or storage class
    swap-foo.c:5: warning: type defaults to 'int' in declaration of 'swap'
    swap-foo.c:25: warning: return type defaults to 'int'

    > If you supply a prototype you must specify the return type.


    How do I do that?

    Zach
    Zach, Jan 25, 2008
    #4
  5. Zach

    Zach Guest

    On Jan 25, 8:18 am, Ed Prochak <> wrote:
    >
    > Likely you want
    > void swap(int, int);
    > or
    > int swap(int, int);
    > to appear before the start of main.


    Ah, thanks. For some reason I was thinking of tokens like "return 0"
    when Mark was talking about needing a return type! hehe.

    Zach
    Zach, Jan 25, 2008
    #5
  6. Zach

    Zach Guest

    On Jan 25, 8:01 am, Zach <> wrote:
    > Here is my test program (just swaps 2 ints):
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void)
    > {
    >
    > int s1, s2;
    > s1 = 3;
    > s2 = 4;
    >
    > printf("s1 = %d \n", s1);
    > printf("s2 = %d \n", s2);
    >
    > swap(s1,s2);
    >
    > exit(EXIT_SUCCESS);
    >
    > }
    >
    > swap(int a, int b)
    > {
    > int temp;
    >
    > temp = a;
    > a = b;
    > b = temp;
    >
    > printf("s1 = %d \n", a);
    > printf("s2 = %d \n", b);
    >
    > return 0;
    >
    > }
    >
    > When I compile there are no errors and it runs with expected results:
    >
    > zu22@netrek:~$ ./foop
    > s1 = 3
    > s2 = 4
    > s1 = 4
    > s2 = 3
    >
    > But why is it not complaining about no function prototype for
    > swap() ?
    >
    > Oddly when I do add a function prototype I get an error:
    > On line 5 I added:
    > swap(int, int);
    >
    > When I compile it says:
    > swap-foo.c:5: warning: data definition has no type or storage class
    >
    > What does this mean?
    >
    > I tried using the program protoize but it doesn't do anything:
    >
    > zu22@netrek:~$ protoize swap-foo.c
    > protoize: compiling 'swap-foo.c'
    > protoize: converting file 'swap-foo.c'
    > zu22@netrek:~$ diff swap-foo.c swap-foo.c.save
    > zu22@netrek:~$
    >
    > Zach


    Here is my new version, it passes:
    gcc -ansi -pedantic -Wall -Wextra -o foop swap-foo.c
    with no errors or warnings

    #include <stdio.h>
    #include <stdlib.h>


    int swap(int, int);


    int main(void)
    {

    int s1, s2;
    s1 = 3;
    s2 = 4;

    printf("s1 = %d \n", s1);
    printf("s2 = %d \n", s2);

    swap(s1, s2);

    exit(EXIT_SUCCESS);

    }

    int swap(int a, int b)
    {
    int temp;

    temp = a;
    a = b;
    b = temp;

    printf("s1 = %d \n", a);
    printf("s2 = %d \n", b);

    return 0;

    }

    Zach
    Zach, Jan 25, 2008
    #6
  7. Zach

    Randy Howard Guest

    On Fri, 25 Jan 2008 07:01:58 -0600, Zach wrote
    (in article
    <>):


    > But why is it not complaining about no function prototype for
    > swap() ?


    Try it with a function returning char * and see what happens.



    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
    Randy Howard, Jan 25, 2008
    #7
  8. Zach wrote:
    > On Jan 25, 8:01 am, Zach <> wrote:
    >> Here is my test program (just swaps 2 ints):
    >>
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >>
    >> int main(void)
    >> {
    >>
    >> int s1, s2;
    >> s1 = 3;
    >> s2 = 4;
    >>
    >> printf("s1 = %d \n", s1);
    >> printf("s2 = %d \n", s2);
    >>
    >> swap(s1,s2);
    >>
    >> exit(EXIT_SUCCESS);
    >>
    >> }
    >>
    >> swap(int a, int b)
    >> {
    >> int temp;
    >>
    >> temp = a;
    >> a = b;
    >> b = temp;
    >>
    >> printf("s1 = %d \n", a);
    >> printf("s2 = %d \n", b);
    >>
    >> return 0;
    >>
    >> }
    >>
    >> When I compile there are no errors and it runs with expected results:
    >>
    >> zu22@netrek:~$ ./foop
    >> s1 = 3
    >> s2 = 4
    >> s1 = 4
    >> s2 = 3
    >>
    >> But why is it not complaining about no function prototype for
    >> swap() ?
    >>
    >> Oddly when I do add a function prototype I get an error:
    >> On line 5 I added:
    >> swap(int, int);
    >>
    >> When I compile it says:
    >> swap-foo.c:5: warning: data definition has no type or storage class
    >>
    >> What does this mean?
    >>
    >> I tried using the program protoize but it doesn't do anything:
    >>
    >> zu22@netrek:~$ protoize swap-foo.c
    >> protoize: compiling 'swap-foo.c'
    >> protoize: converting file 'swap-foo.c'
    >> zu22@netrek:~$ diff swap-foo.c swap-foo.c.save
    >> zu22@netrek:~$
    >>
    >> Zach

    >
    > Here is my new version, it passes:
    > gcc -ansi -pedantic -Wall -Wextra -o foop swap-foo.c
    > with no errors or warnings
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    >
    > int swap(int, int);
    >
    >
    > int main(void)
    > {
    >
    > int s1, s2;
    > s1 = 3;
    > s2 = 4;
    >
    > printf("s1 = %d \n", s1);
    > printf("s2 = %d \n", s2);
    >
    > swap(s1, s2);
    >
    > exit(EXIT_SUCCESS);
    >
    > }
    >
    > int swap(int a, int b)
    > {
    > int temp;
    >
    > temp = a;
    > a = b;
    > b = temp;
    >
    > printf("s1 = %d \n", a);
    > printf("s2 = %d \n", b);
    >
    > return 0;
    >
    > }

    It still doesn't swap them, it only doe so while inside swap(). Look at s1
    and s1 after the call to swap()

    If you want to realy swap them, swap() needs to take pointers to them (and
    be called with their addresses)

    int swap(int *s2, int s2)
    {
    ....
    }

    int main(void)
    {
    ....
    swap(&s1, &s2);
    ....
    }
    Joachim Schmitz, Jan 25, 2008
    #8
  9. Mark Bluemel said:

    <snip>

    > If you supply a prototype you must specify the return type.


    C&V, please. (Bear in mind that his program used implicit int, and
    therefore C99 doesn't apply - this is a C90 program.)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Jan 25, 2008
    #9
  10. Zach

    Mark Bluemel Guest

    Richard Heathfield wrote:
    > Mark Bluemel said:
    >
    > <snip>
    >
    >> If you supply a prototype you must specify the return type.

    >
    > C&V, please. (Bear in mind that his program used implicit int, and
    > therefore C99 doesn't apply - this is a C90 program.)
    >


    The OP asked
    >> Oddly when I do add a function prototype I get an error:
    >> On line 5 I added:
    >> swap(int, int);


    >> When I compile it says:
    >> swap-foo.c:5: warning: data definition has no type or storage class

    >
    >> What does this mean?


    My comment was my interpretation of the compiler warning. I don't have
    (perhaps I should get) a copy of the C90 standard to hand, so couldn't
    check against it.

    I note that gcc also believes this is the case, as when I added that
    prototype (at line 8) it reported :-

    swap.c:8: warning: type defaults to `int' in declaration of `swap'
    swap.c:8: error: ISO C forbids data definition with no type or
    storage class
    Mark Bluemel, Jan 25, 2008
    #10
  11. "Joachim Schmitz" <> writes:

    > Zach wrote:
    >>> swap(int a, int b)
    >>> {

    <snip>
    >>> }

    <snip>
    > It still doesn't swap them, it only doe so while inside swap(). Look at s1
    > and s1 after the call to swap()
    >
    > If you want to realy swap them, swap() needs to take pointers to them (and
    > be called with their addresses)
    >
    > int swap(int *s2, int s2)


    I think you mean int swap(int *s1, int *s2).

    To the OP: that fact that int if the implied return type when you
    don't give one (in C90) does not mean you have to make it return int!
    There is a type for a function that "does something" and need not
    return a value: void.

    --
    Ben.
    Ben Bacarisse, Jan 25, 2008
    #11
  12. Ben Bacarisse wrote:
    > "Joachim Schmitz" <> writes:
    >
    >> Zach wrote:
    >>>> swap(int a, int b)
    >>>> {

    > <snip>
    >>>> }

    > <snip>
    >> It still doesn't swap them, it only doe so while inside swap(). Look
    >> at s1 and s1 after the call to swap()
    >>
    >> If you want to realy swap them, swap() needs to take pointers to
    >> them (and be called with their addresses)
    >>
    >> int swap(int *s2, int s2)

    >
    > I think you mean int swap(int *s1, int *s2).

    Indeed, thanks for the correction

    Bye, Jojo
    Joachim Schmitz, Jan 25, 2008
    #12
  13. Zach

    Zach Guest

    On Jan 25, 8:40 am, "Joachim Schmitz" <>
    wrote:
    >
    >
    > It still doesn't swap them, it only doe so while inside swap(). Look at s1
    > and s1 after the call to swap()
    >
    > If you want to realy swap them, swap() needs to take pointers to them (and
    > be called with their addresses)
    >
    > int swap(int *s2, int s2)
    > {
    > ...
    >
    > }
    >
    > int main(void)
    > {
    > ...
    > swap(&s1, &s2);
    > ...
    >
    > }


    Ja, I am planning on doing that. I am incrementally improving the
    program :)

    Zach
    Zach, Jan 25, 2008
    #13
  14. Zach

    Zach Guest

    On Jan 25, 9:32 am, Ben Bacarisse <> wrote:
    >
    >
    > To the OP: that fact that int if the implied return type when you
    > don't give one (in C90) does not mean you have to make it return int!
    > There is a type for a function that "does something" and need not
    > return a value: void.


    Doh :)

    Zach
    Zach, Jan 25, 2008
    #14
  15. On Fri, 25 Jan 2008 13:15:22 +0000, Mark Bluemel
    <> wrote:

    > Zach wrote:


    > > int main(void)
    > > {
    > >
    > > int s1, s2;

    > [snip]
    > > swap(s1,s2);


    Although it is legal to (forward-)declare a function within another
    function, it is almost always better to do it outside (or formally, at
    file scope). However, it is not legal to have a declaration without
    any declaration-specifiers; in order to omit the type int (<=C90 only)
    you must specify the otherwise-redundant storage-class 'extern'.

    > > Oddly when I do add a function prototype I get an error:
    > > On line 5 I added:
    > > swap(int, int);

    >
    > > When I compile it says:
    > > swap-foo.c:5: warning: data definition has no type or storage class
    > >
    > > What does this mean?

    >

    Although the wording of this diagnostic is a bit misleading; the
    requirement for a type or class _or qualifier_ applies to all
    declarations, whether they are function declarations, data
    declarations, or data definitions.

    > If you supply a prototype you must specify the return type.
    >

    That's irrelevant. In <=C90 you can have a function declaration (and
    also definition) with or without a prototype (for the parameters) and
    independently with or without a return type. In C99 you must always
    have the return type but still can have prototyped parameters or not.

    It's _best style_ to always use prototypes AND specify return type.

    - formerly david.thompson1 || achar(64) || worldnet.att.net
    David Thompson, Feb 4, 2008
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Allison
    Replies:
    37
    Views:
    774
    Warren W. Gay VE3WWG
    Mar 4, 2004
  2. Ludovic Brenta
    Replies:
    86
    Views:
    1,344
    Jan C. =?iso-8859-1?Q?Vorbr=FCggen?=
    Feb 19, 2004
  3. rs
    Replies:
    13
    Views:
    492
    Mark Gordon
    Dec 1, 2003
  4. Dave Allison

    Announcing new scripting/prototyping language

    Dave Allison, Feb 5, 2004, in forum: C Programming
    Replies:
    35
    Views:
    860
    Warren W. Gay VE3WWG
    Mar 4, 2004
  5. Ludovic Brenta
    Replies:
    77
    Views:
    1,439
    Hyman Rosen
    Feb 26, 2004
Loading...

Share This Page