473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Function declaring style

Hello,

In the short time I have spent reading this newsgroup, I have seen this
sort of declaration a few times:
int
func (string, number, structure)
char* string
int number
struct some_struct structure
Now, I am vaguely familiar with it; I did not read up on it because I
have read in an apparently misinformed book that such declarations were
very old and that no one used them any more. Can someone please explain
why they are still using this style and why the book said it was
obsolete? Is what I have written any different than:
int
func (char* string, int number, struct some_struct structure) ?


Sorry if I'm being too basic about this, but it's just something that
will probably never get mentioned in regular C courses.

Thanks in advance.

Feb 5 '06 #1
6 2057
On 5 Feb 2006 15:28:27 -0800, in comp.lang.c , dn****@hotpop.com
wrote:
In the short time I have spent reading this newsgroup, I have seen this
sort of declaration a few times:
actually you mean definition.

(snip example of pre-ANSI function definition)
Now, I am vaguely familiar with it; I did not read up on it because I
have read in an apparently misinformed book that such declarations were
very old and that no one used them any more.
The book is correct. However legacy code is likely to still contain
such definitions, as will code written by people forced to use antique
compilers, or who are learning from very old books.
Is what I have written any different than:
int
func (char* string, int number, struct some_struct structure) ?


Only slightly - this ISO/ANSI definition is also a prototype, and
gives the compiler more ability to check types I believe.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 5 '06 #2
<dn****@hotpop.com> wrote
int
func (string, number, structure)
char* string
int number
struct some_struct structure


Now, I am vaguely familiar with it; I did not read up on it because I
have read in an apparently misinformed book that such declarations were
very old and that no one used them any more. Can someone please explain
why they are still using this style and why the book said it was
obsolete? Is what I have written any different than:

I'm still using Fortran 77.
I would guess that the style is borrowed from Fortran, and of course it is
messy and hard to read and the modern syntax is better.

However you should be familiar with it. I failed a job interview because I
wasn't, and was presented with some pre-ANSI code to debug. The company
still used it, for some reason.
Feb 6 '06 #3
dn****@hotpop.com writes:
In the short time I have spent reading this newsgroup, I have seen this
sort of declaration a few times:
int
func (string, number, structure)
char* string
int number
struct some_struct structure


Now, I am vaguely familiar with it; I did not read up on it because I
have read in an apparently misinformed book that such declarations were
very old and that no one used them any more. Can someone please explain
why they are still using this style and why the book said it was
obsolete? Is what I have written any different than:
int
func (char* string, int number, struct some_struct structure) ?


That old style of function definition has been basically obsolete
since the ANSI standard was approved in 1989. For several years after
that, there were still enough compilers in use that didn't support
prototypes (the superior alternative introduced by the ANSI standard)
that it was still sometimes necessary to use old-style definitions.
You'll still see a fair amount of old code that uses preprocessor
tricks to cater to pre-ANSI and ANSI compilers; there's also a tool
called "ansi2knr" that translates code using prototypes to code using
the old-style definitions. ("knr" refers to K&R, Kernighan &
Ritchie's _The C Programming Language_. The first edition describes
the pre-ANSI version of the language. The second edition describes
the newer language defined by the ANSI standard.)

The 1989 ANSI standard (or the equivalent 1990 ISO C standard) has
caught on almost universally. I'm sure there are still pre-ANSI
compilers in use somewhere, but I don't think they exist on any of the
systems I currently use. There's no longer any need to use old-style
function definitions unless you have a specific requirement to support
an ancient system.

But the old-style definitions are still supported by the newer
standards for backward compatibility.

The newer 1999 ISO C standard has not caught on as quickly, probably
because it's less of an improvement over the previous standard than
the 1989 ANSI C standard was over the (non-)standard that it replaced.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 6 '06 #4
> In the short time I have spent reading this newsgroup, I have seen this
sort of declaration a few times:
int
func (string, number, structure)
char* string
int number
struct some_struct structure

(There should be semicolons after each of the three declarations.)
Now, I am vaguely familiar with it; I did not read up on it because I
have read in an apparently misinformed book that such declarations were
very old and that no one used them any more. Can someone please explain
why they are still using this style and why the book said it was
obsolete?
There is no good reason to use the old style in new code today.
Is what I have written any different than:
int
func (char* string, int number, struct some_struct structure) ?


Yes, there is a difference: when you use the modern "prototype"
syntax, the compiler does automatic type conversions on each argument
the same way as it does in an ordinary assignment (=) expression.
With the old syntax, the types have to match or the behavior is
undefined. So these lines:

answer = func("hello", 3.1414, strucked);
answer = func("hello", 3, strucked);

are equivalent if func() was defined using a prototype, because the
double constant 3.1414 is safely converted to int. (If func() was
defined in a separate file, of course, you also need a prototyped
declaration in scope when you call it.)

If you used the old syntax, only the third form would work safely.
The others would cause undefined behavior -- in practice, what's
likely to happen is at least that some bits from the floating-point
value will be misinterpreted as an integer, and other arguments may
be misread as well. Similar issues arise if you use a long int
argument (3L) and int and long int are different sizes.

There are some other subtle differences relating certain to specific
types of arguments such as "float" and "short".
--
Mark Brader "If you design for compatibility with a
Toronto donkey cart, what you get is a donkey cart."
ms*@vex.net -- ?, quoted by Henry Spencer

My text in this article is in the public domain.
Feb 6 '06 #5

Mark Brader wrote:
In the short time I have spent reading this newsgroup, I have seen this
sort of declaration a few times:
int
func (string, number, structure)
char* string
int number
struct some_struct structure

(There should be semicolons after each of the three declarations.)


Sorry about that. And sorry for using incorrect terminology.
Now, I am vaguely familiar with it; I did not read up on it because I
have read in an apparently misinformed book that such declarations were
very old and that no one used them any more. Can someone please explain
why they are still using this style and why the book said it was
obsolete?
There is no good reason to use the old style in new code today.
Is what I have written any different than:
int
func (char* string, int number, struct some_struct structure) ?


Yes, there is a difference: when you use the modern "prototype"
syntax, the compiler does automatic type conversions on each argument
the same way as it does in an ordinary assignment (=) expression.
With the old syntax, the types have to match or the behavior is
undefined. So these lines:

answer = func("hello", 3.1414, strucked);
answer = func("hello", 3, strucked);

are equivalent if func() was defined using a prototype, because the
double constant 3.1414 is safely converted to int. (If func() was
defined in a separate file, of course, you also need a prototyped
declaration in scope when you call it.)

If you used the old syntax, only the third form would work safely.
The others would cause undefined behavior -- in practice, what's
likely to happen is at least that some bits from the floating-point
value will be misinterpreted as an integer, and other arguments may
be misread as well. Similar issues arise if you use a long int
argument (3L) and int and long int are different sizes.

There are some other subtle differences relating certain to specific
types of arguments such as "float" and "short".


Thanks ot everyone for valuable advice and insight.
--
Mark Brader "If you design for compatibility with a
Toronto donkey cart, what you get is a donkey cart."
ms*@vex.net -- ?, quoted by Henry Spencer

My text in this article is in the public domain.


Feb 6 '06 #6
On Sun, 05 Feb 2006 23:46:04 +0000, Mark McIntyre
<ma**********@spamcop.net> wrote:
On 5 Feb 2006 15:28:27 -0800, in comp.lang.c , dn****@hotpop.com
wrote:

Is [old-style function definition] any different than:
int
func (char* string, int number, struct some_struct structure) ?


Only slightly - this ISO/ANSI definition is also a prototype, and
gives the compiler more ability to check types I believe.


Almost. A prototype definition is also a (prototype) declaration and
the compiler _must_ diagnose any mismatch with calls made in the scope
of that declaration (which is the rest of the translation unit, i.e.,
intramodule calls) _and_ any mismatch with another/prior prototype
declaration (such as an interface in an #include'd .h file).

A nonprototype definition is also a nonprototype declaration and does
not require such checking*; but the definition does provide type
information that the compiler and/or linker _can_ check if they wish.
* If there is _another_ prototype declaration in scope _that_
declaration does require checking of calls -- but not matching with
the definition. This was (is?) a handy transition technique, because
it is easy to macroize a declaration to be either prototype or
oldstyle, but much harder to do this with the definition.

There is also a subtle difference for some parameter types, though not
the ones in the OP's case. If a parameter in a K&R1 definition is a
float or an integer type narrower (lower rank) than int they are
actually passed as double and (signed or unsigned) int respectively
and then 'narrowed' back to the declared types in the called body.
Thus to write a prototype declaration, and make prototyped calls, to a
K&R1-defined function with such parameter types, the prototype must
use the widened types.

- David.Thompson1 at worldnet.att.net
Feb 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
1
by: jconnort | last post by:
I'm trying to write a function to get the current system time, so I can use it when I need to output it to a log, below is the code I'm testing: #include "include.h" #include <stdlib.h> ...
3
by: sam_cit | last post by:
Hi Everyone, I have seen in some project where functions are declared as extern, what is the possible reason to do this? To my best understanding, if some other file wan't to invoke this...
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
17
by: Jason Doucette | last post by:
I am converting a C-style unit into a C++ class. I have an implementation function that was defined in the .cpp file (so it was hidden from the interface that exists in the .h file). It uses a...
2
by: Sanders Kaufman | last post by:
I've noticed that some code analyzers comment that the type of data returned by my functions is "unknown" in the following syntax: function fnFooBar($aryParameters) { $bRetVal = True; return...
9
by: =?ISO-8859-1?Q?Janne_H=E4rk=F6nen?= | last post by:
Hello, Is there a simple way to resolve declaring class of a method at runtime ? Consider this simple example: $ python Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) on cygwin Type...
6
by: Syren Baran | last post by:
Hi, is it possible to write two functions which both require a function as an argument and both being able to use the other function as an argument? Afaik the address of a function is not known...
28
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.