prototyping?

Z

Zach

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
 
M

Mark Bluemel

Zach said:
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.
 
E

Ed Prochak

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
 
Z

Zach

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
 
Z

Zach

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
 
Z

Zach

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
 
J

Joachim Schmitz

Zach said:
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);
....
}
 
R

Richard Heathfield

Mark Bluemel said:

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

Mark Bluemel

Richard said:
Mark Bluemel said:



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

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
 
B

Ben Bacarisse

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

Zach

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
 
Z

Zach

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
 
D

David Thompson

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top