is this definition old-style or new-style

G

G Fernandes

Is the following an old-style(K&R) function definition or a new
style(C89)?

int (length, factor, multiple)
{
/* ... code ... */

return length;
}


It seems like it fits the description of either type. How does the
compiler know the difference?

Thanks
 
D

DHOLLINGSWORTH2

G Fernandes said:
Is the following an old-style(K&R) function definition or a new
style(C89)?

int (length, factor, multiple)
{
/* ... code ... */

return length;
}


It seems like it fits the description of either type. How does the
compiler know the difference?

Thanks
Niether, int is a keyword, and there is not function declaration going on
here.

int PROCEDURE_NAME(int length, int factor, int multiple)
{
/* ... code ... */
return length;
}

is a func def, and is old and new.
 
G

G Fernandes

DHOLLINGSWORTH2 said:
Niether, int is a keyword, and there is not function declaration going on
here.

What do you mean? Of course int is a keyword.
int PROCEDURE_NAME(int length, int factor, int multiple)
{
/* ... code ... */
return length;
}

is a func def, and is old and new.

No, the defintion you gave is not an old style(K&R style) definition.
It's a C89/C99 style definition.


This below definition fits the description of a C89 and K&R style
definition (not valid in C99).

foo(length, factor, multiple) {/* body */}

I was just wondering how the compiler treats this (new or old way).
 
W

Walter Roberson

:This below definition fits the description of a C89 and K&R style
:definition (not valid in C99).

:foo(length, factor, multiple) {/* body */}

Wouldn't old style be:

foo(length, factor, multiple) length, factor, multiple; { /* body */ }

Old style expects variable declarations, and the way to implicitly
declare a variable as int is to mention it's name but not a type.

Mind you, it's been long enough since I did old style...
 
C

Chris Torek

:This below definition fits the description of a C89 and K&R style
:definition (not valid in C99).
:foo(length, factor, multiple) {/* body */}

Wouldn't old style be:

foo(length, factor, multiple) length, factor, multiple; { /* body */ }

That would be allowed (with implicit int), but not required.

In K&R C, one could write:

main(argc, argv)
char **argv;
{
/* the usual code for main */
return 0;
}

for instance, to let both main() and argc default to "int", and
give argv the correct type.
 
K

Keith Thompson

:This below definition fits the description of a C89 and K&R style
:definition (not valid in C99).

:foo(length, factor, multiple) {/* body */}

Wouldn't old style be:

foo(length, factor, multiple) length, factor, multiple; { /* body */ }

Old style expects variable declarations, and the way to implicitly
declare a variable as int is to mention it's name but not a type.

No. It's:

foo(length, factor, multiple)
{
/* body */
}

or, with explicit types:

int foo(length, factor, multiple)
int length;
int factor;
int multiple;
{
/* body */
}

Any C89/C90 compiler will accept old-style function definitions; you
could try compiling your sample code to see that it's invalid.
 
J

Joe Wright

G said:
going on



What do you mean? Of course int is a keyword.




No, the defintion you gave is not an old style(K&R style) definition.
It's a C89/C99 style definition.


This below definition fits the description of a C89 and K&R style
definition (not valid in C99).

foo(length, factor, multiple) {/* body */}

I was just wondering how the compiler treats this (new or old way).

G - Your first example in not a function definition at all. It doesn't
have a function name. Old-Style definitions look like ..

int foo(len, fac, mul) int len; float fac; float mul; { /* body */ }

... while C89 and onward looks like ..

int foo(int len, float fac, float mul) { /* body */ }

This latter allows the function to be declared before use or prototyped
so that the compiler knows exactly how to call it and with which
argument value conversions. Consider ..

int foo(int, float, float);

... is a prototype. The compiler, having read this prototype, when it
finds the statement ..

foo(1.0, 2, 3);

... will convert the arguments to int, float and float before calling foo().
 
L

Lawrence Kirby

Is the following an old-style(K&R) function definition or a new
style(C89)?

int (length, factor, multiple)
{
/* ... code ... */

return length;
}


It seems like it fits the description of either type. How does the
compiler know the difference?

New style "prototype" declarations and definitions *always* have type
information between the ()'s. The example above doesn't so is a K&R style
definition. The prototype form would be

int (int length, int factor, int multiple)
{

Lawrence
 
A

Andrey Tarasevich

Walter said:
:This below definition fits the description of a C89 and K&R style
:definition (not valid in C99).

:foo(length, factor, multiple) {/* body */}

Wouldn't old style be:

foo(length, factor, multiple) length, factor, multiple; { /* body */ }
...

C89/90 allows selective declaration of function parameters in
declaration list. You can declare all of them

int foo(length, factor, multiple)
int length, factor, multiple;
{ /* body */ }

you can declare none

int foo(length, factor, multiple)
{ /* body */ }

you can declare only some of them

int foo(length, factor, multiple)
int factor;
{ /* body */ }

All these variants are fine in C89/90.

However, in C99 "old-style" function definitions are required to declare
all named parameters in declaration list, meaning that in C99 only the
first variant is valid.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top