473,902 Members | 3,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

K&R-Style Function Declarations: Good or Bad?

I tend to use rather descriptive names for parameters, so the old style of
declaration appeals to me, as I can keep a declaration within 80 chars:

void * newKlElem (frame_size,num _blocks,num_fra mes,frame_locat or)
size_t frame_size;
unsigned short num_blocks;
unsigned short num_frames;
Kl_frame_locato r *locator;
{
/* code goes here */

I've found many who despise this sort of declaration, and also some who
say it's going to be deprecated in a future standard. Should I avoid
something like this, or keep doing as I please?

--
Mike's Patented Blocklist; compile with gcc:

i=0;o(a){printf ("%u",i>>8*a&25 5);if(a){printf (".");o(--a);}}
main(){do{o(3); puts("");}while (++i);}

Nov 13 '05
28 15419

On Mon, 8 Dec 2003, E. Robert Tisdale wrote:

CBFalconer wrote:
Why not have the best of both worlds and write:

void* newKlElem( /* comment */
size_t frame_size; /* comment */
unsigned short num_blocks; /* comment */
unsigned short num_frames; /* comment */
Kl_frame_locato r* locator /* comment */
) {
/* code goes here */
}


No, he didn't write *quite* that. For one thing, his
comment blocks were lined up, and I'm fairly sure he
put the ) and the { on different lines, not being an
infidel and all that.
But did you have anything to say, or were you just
practicing cut-and-paste again?

-Arthur
Nov 13 '05 #11
"Michael B." <us*****@spambl ocked.com> wrote in message news:<pa******* *************** ******@spambloc ked.com>...

I tend to use rather descriptive names for parameters, so the old style of
declaration appeals to me, as I can keep a declaration within 80 chars:

void * newKlElem (frame_size,num _blocks,num_fra mes,frame_locat or)
size_t frame_size;
unsigned short num_blocks;
unsigned short num_frames;
Kl_frame_locato r *locator;
{
/* code goes here */
How is that better than

void * newKlElem (
size_t frame_size,
unsigned short num_blocks,
unsigned short num_frames,
Kl_frame_locato r *locator )
{
/* code goes here */

which appears to give you far more space for descriptive parameter
names or long type names, while being very similar to your current
style? I find the latter far more readable since there is less
clutter.

The example you give is also invalid. Unless you have a prototype
in scope at the time of the call, it is not possible for your
function to receive parameters of type <unsigned short>. They will
have been promoted at the time of the call to be either <int> or
<unsigned int> depending on the type sizes in the implementation -
so to make your version portable, you need some ugly compile-time
duplication of the parameters in the function definition, or you
need to explicitly cast the parameters to an appropriate type in
each call.

The addition of function prototypes was probably the most valuable
change in the history of C. The advantages of checking the numbers
and types of parameters, and having explicit control of the types
that get passed, are immense - I've seen countless bugs get caught
this way.
I've found many who despise this sort of declaration, and also some who
say it's going to be deprecated in a future standard. Should I avoid
something like this, or keep doing as I please?


You can obviously do as you please, but if you did that in front
of me in an interview you wouldn't get hired - even if the error
in your example were corrected.
Nov 13 '05 #12
"J. J. Farrell" wrote:

"Michael B." <us*****@spambl ocked.com> wrote in message news:<pa******* *************** ******@spambloc ked.com>...

I tend to use rather descriptive names for parameters, so the old style of
declaration appeals to me, as I can keep a declaration within 80 chars:

void * newKlElem (frame_size,num _blocks,num_fra mes,frame_locat or)
size_t frame_size;
unsigned short num_blocks;
unsigned short num_frames;
Kl_frame_locato r *locator;
{


[...]

The example you give is also invalid. Unless you have a prototype
in scope at the time of the call, it is not possible for your
function to receive parameters of type <unsigned short>. They will
have been promoted at the time of the call to be either <int> or
<unsigned int> depending on the type sizes in the implementation -
so to make your version portable, you need some ugly compile-time
duplication of the parameters in the function definition, or you
need to explicitly cast the parameters to an appropriate type in
each call.


This is the second time this piece of misinformation has
cropped up in this thread. I can only conclude that people
have become so accustomed to prototyped functions (that's
understandable; they *are* better) that they've completely
forgotten how old-style functions worked.

For the record:

- Pre-Standard C is/was a language without function
prototypes, in which the O.P.'s style of function
definition is/was the only possible style, and

- In pre-Standard C it is/was possible to write a
function with promotable argument types like `char'
and `short' and `float', and to call such functions
even though the supplied values are/were subject to
promotion at the point of call, and

- Standard C retains the pre-Standard function style,
augmenting rather than supplanting it, and

- Pre-Standard functions and calls work in Standard C
just as they do/did in pre-Standard C.

Reference: ISO/IEC 9899:1999 (E), Section 6.5.2.2,
paragraph 6, sentences 1, 2 and 5:

If the expression that denotes the called function has
a type that does not include a prototype, the integer
promotions are performed on each argument, and arguments
that have type float are promoted to double. These are
called the default argument promotions. [...] If the
function is defined with a type that does not include a
prototype, and the types of the arguments after promotion
are not compatible with those of the parameters after
promotion, the behavior is undefined, [...]

In short, the types of the promoted arguments need not match the
types of the formal parameters, they need to match the *promoted*
types of those parameters.

People seem to have lost sight of the fact that although
Standard C was born in 1989, C itself had been around for some
dozens of weeks before that -- and yes, during that Paleozoic
time, young 'uns, it *was* possible to write C programs. Been
there, done that -- don't want to go back. Use prototypes.

--
Er*********@sun .com
Nov 13 '05 #13
"E. Robert Tisdale" wrote:
CBFalconer wrote:
Why not have the best of both worlds and write:

void* newKlElem( /* comment */
size_t frame_size; /* comment */
unsigned short num_blocks; /* comment */
unsigned short num_frames; /* comment */
Kl_frame_locato r* locator /* comment */
) {
/* code goes here */
}
**** an entirely empty message ****


Which at least contained none of the usual Trollsdale errors or
misinformation. He restricted himself to reformatting the quote
without mentioning it. He is quite sneaky, because you have to
look twice to see what he did.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #14
On Sun, 07 Dec 2003 23:13:17 -0800, in comp.lang.c , "Michael B."
<us*****@spambl ocked.com> wrote:
I tend to use rather descriptive names for parameters, so the old style of
declaration appeals to me, as I can keep a declaration within 80 chars:
your reasoning is spurious. You can equally keep a proper prototyping
declaration within 80 chars too.

(snip 20 year old declaration style.)
I've found many who despise this sort of declaration, and also some who
say it's going to be deprecated in a future standard. Should I avoid
something like this, or keep doing as I please?


Its already deprecated, don't use it. You're doing the equivalent of
using a goose quill to write letters.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #15
On Mon, 08 Dec 2003 16:57:23 GMT, in comp.lang.c , CBFalconer
<cb********@yah oo.com> wrote:
Why not have the best of both worlds and write:

void *newKlElem(size _t frame_size; /* comment */
unsigned short num_blocks; /* comment */
unsigned short num_frames; /* comment */
Kl_frame_locato r *locator) /* comment */


you may want to consider replacing those semicolons by commas tho...

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #16
[this followup is also a test of trn-4.0-test76...]

In article <news:3F******* ********@sun.co m>
Eric Sosman <Er*********@Su n.COM> writes:
In short, the types of the promoted arguments need not match the
types of the formal parameters, they need to match the *promoted*
types of those parameters.


Indeed. You may also write a prototype for a K&R-style
function, as long as you use those promoted types. For instance:

int f(c) char c; { ... } /* K&R declaration/definition */

which requires:

int f(int); /* not int f(char)! */

as its prototype. This assumes that plain char promotes to signed
int, which is true on any sensible hosted implementation -- but it
also points to the problem with writing prototypes for functions
like (to take a real example):

int chown(file, uid, gid)
char *file;
uid_t uid;
gid_t gid;
{
...
}

If uid_t is:

typedef short uid_t;

then the correct prototype uses "int", but if it is:

typedef unsigned short uid_t;

then it depends on whether USHRT_MAX > INT_MAX. If USHRT_MAX is
(say) 65535 and INT_MAX is 32767, as is true on the PDP-11, the
correct prototype uses "unsigned int" instead.

If uid_t is typedef'ed as signed or unsigned int, there is no
change in types between the K&R declaration and a prototype.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #17
Mark McIntyre wrote:
<cb********@yah oo.com> wrote:
Why not have the best of both worlds and write:

void *newKlElem(size _t frame_size; /* comment */
unsigned short num_blocks; /* comment */
unsigned short num_frames; /* comment */
Kl_frame_locato r *locator) /* comment */


you may want to consider replacing those semicolons by commas tho...


Eee-yup. That does tend to reduce the error listings :-)

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #18
In <br*********@en ews2.newsguy.co m> Chris Torek <no****@torek.n et> writes:
Indeed. You may also write a prototype for a K&R-style
function, as long as you use those promoted types. For instance:

int f(c) char c; { ... } /* K&R declaration/definition */

which requires:

int f(int); /* not int f(char)! */

as its prototype.


But that would be misleading, given that the type of c is still char:

f.c:

#include <stdio.h>

int f(c)
char c;
{
return printf("%d\n", (int)c);
}

main.c:

int f(int);
int main()
{
f(1000);
return 0;
}

I'm not sure about the behaviour of this code in K&R C, which talks about
float parameters getting widened to double, but no mention about char and
short parameters getting promoted to int.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #19
On 9 Dec 2003 05:00:53 GMT, Chris Torek <no****@torek.n et> wrote:
Indeed. You may also write a prototype for a K&R-style
function, as long as you use those promoted types. For instance:

int f(c) char c; { ... } /* K&R declaration/definition */

which requires:

int f(int); /* not int f(char)! */


Some compilers at least, in K&R mode, complain about anything other
than int f();
--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #20

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

Similar topics

9
8587
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character reference (i.e.  ) and it seems to be converting the character to its UNICODE representation. Take this source XML document: <?xml version="1.0" encoding="UTF-8"?>
1
11447
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a href="mailto:DrTebi@yahoo.com">DrTebi</a> This would work like a regular mailto link in any browser, but wouldn't be visible to spiders if they don't have a function to decode it.
0
2430
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a href="http://blah.com/?test=test&amp;amp;test2=test2">Test2&amp;amp;</a> This results in the following HTML Code:
6
47572
by: Rich Wallace | last post by:
All, I receive an xml doc with an element which contains a &amp; in the data. I am attempting to insert the data into a SQL Server database and I want to convert the &amp; to a "&" character before I perform the insert. Is the a way in VB.NET to perform this? -Rich
8
2823
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);" TextBox2.Attributes.Add("onKeyPress", jscode) You will notice that jscode contains the JavaScript Logical And operator (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
11
6459
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved and
6
2253
by: cj | last post by:
I'm receiving an xml formatted string that I pull data from by reading it into an xml document like this: Dim doc As New Xml.XmlDocument doc.LoadXml(respstr) Dim co_name As Xml.XmlNodeList = doc.GetElementsByTagName("co_name") textbox1.text = co_name(0).innertext Now I'm getting company names that have ampersands in them. I was not aware that was not allowed in xml and had no method of dealing with it.
14
5946
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only because of this, so I hope some of you gurus can enlighten me with this :) In what circumstances can the "&amp;" in the source code be involuntary changed to "&" by a browser when or other software, when editing and uploading the file to the web...
7
4641
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get BeautifulSoup to clean those up? There are various parsing options related to "&" handling, but none of them seem to do quite the right thing. If I write the BeautifulSoup parse tree back out with "prettify", the loose "&" is still in there. So...
3
4464
by: gg | last post by:
I specify the Url element as <xsd:element name="Url"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:maxLength value="512"/> <xsd:pattern value="http://+"/> </xsd:restriction> </xsd:simpleType> </xsd:element> in the xsd but When I use the following in the xml
0
9997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11279
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...
0
10872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9675
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
8047
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
5893
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4307
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3323
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.