473,666 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2091
On 5 Feb 2006 15:28:27 -0800, in comp.lang.c , dn****@hotpop.c om
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.c om 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_Keit h) 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**********@s pamcop.net> wrote:
On 5 Feb 2006 15:28:27 -0800, in comp.lang.c , dn****@hotpop.c om
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.ne t
Feb 13 '06 #7

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

Similar topics

6
7989
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 compiler can find the definition of the function prior to encountering the use of the function it will generate the prototype itself. I don't currently use this feature, I explicitly create declarations for all functions in a header file. However, I...
1
358
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> main(argc, argv) int argc;
3
1949
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 function, it could very well be done ny including a header file which specifies the function prototype...
83
3935
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 much"; default: return "Unknown error";
17
2464
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 structure that is only needed by the implementation, so it were declared in the .cpp file, as well. Now, when converting this into a class, the class definition exists in the .h file, since it's required by the interface. The implementation...
2
1325
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 $bRetVal; } I *think* I can specify that by doing something like this:
9
1337
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 "help", "copyright", "credits" or "license" for more information.
6
1624
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 upon declaration but only once its defined. Thanks
28
8858
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 didn't find any good explanation about it. Could the experts please give me an explanation or point me to some link? Thanks! /Why Tea
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.