I don't understand the difference between these two declarations
int foo(char a, char b)
{
...
}
int foo(a, b)
char a, b;
{
...
}
What is the usefulness of the second form? Is there any difference?
Secondly, I notice that if a declaration list is not included
following an identifier list, gcc warns that the types of all the
identifiers will default to int. Is there somewhere in the spec that
states a default type given to identifiers if it is not explicit? If
so, can someone please give me a pointer to the section? Or is this
just a gcc extension/assumption?
Regards,
B. 8 1742 bo*******@gmail .com writes:
I don't understand the difference between these two declarations
int foo(char a, char b)
This is a prototype.
{
...
}
int foo(a, b)
char a, b;
This is an old-style declaration, sometimes called "K&R style" because
it's used in the first edition K&R (Kernighan & Ritchie, _The C
Programming Language_.
{
...
}
What is the usefulness of the second form? Is there any difference?
K&R style function declarations are effectively obsolescent, though
they're still legal. Prior to the first ANSI C standard, in 1989,
they were the only form available, but the standardization committee
borrowed the first form (prototypes) from an early version of C++.
It's useful to recognize K&R style declarations when you encounter
them in old code, but you should never use them in new code (unless,
for some strange reason, you need to support an ancient compiler).
In this particular case, there's another difference. Due to argument
promotions, the parameters in the K&R declaration are really of type
int. The parameters in the prototype really are of type char.
Secondly, I notice that if a declaration list is not included
following an identifier list, gcc warns that the types of all the
identifiers will default to int. Is there somewhere in the spec that
states a default type given to identifiers if it is not explicit? If
so, can someone please give me a pointer to the section? Or is this
just a gcc extension/assumption?
Implicit int was removed from the language in the 1999 standard (but
few compilers fully support C99, and most or all compilers still
permit implicit int in some mode).
Don't use it. The time you save by not typing "int" isn't worth the
headache of wondering whether it's legal to leave it out.
--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
On Sep 25, 7:08 pm, boroph...@gmail .com wrote:
I don't understand the difference between these two declarations
int foo(char a, char b)
{
...
}
int foo(a, b)
char a, b;
{
...
}
What is the usefulness of the second form? Is there any difference?
The second form is outdated, and should no longer be used.
>
Secondly, I notice that if a declaration list is not included
following an identifier list, gcc warns that the types of all the
identifiers will default to int. Is there somewhere in the spec that
states a default type given to identifiers if it is not explicit? If
so, can someone please give me a pointer to the section? Or is this
just a gcc extension/assumption?
I can't remember off the top of my head, but it doesn't matter,
because you're not going to use it.
Regards,
B.
On Sep 26, 10:08 am, boroph...@gmail .com wrote:
I don't understand the difference between these two declarations
int foo(char a, char b)
{
...
}
int foo(a, b)
char a, b;
{
...
}
What is the usefulness of the second form? Is there any difference?
Secondly, I notice that if a declaration list is not included
following an identifier list, gcc warns that the types of all the
identifiers will default to int. Is there somewhere in the spec that
states a default type given to identifiers if it is not explicit? If
so, can someone please give me a pointer to the section? Or is this
just a gcc extension/assumption?
Regards,
B.
Thanks for the info, I guessed it was an old obsolescent form, and had
this confirmed after rereading it and seeing the footnote which
directed me to the following two sections:
6.11.6 Function declarators
The use of function declarators with empty parentheses (not prototype-
format parameter type declarators) is an obsolescent feature.
6.11.7 Function definitions
The use of function definitions with separate parameter identifier and
declaration lists (not prototype-format parameter type and identifier
declarators) is an obsolescent feature.
Reading 6.11.6 reads to me that function declarators with an empty
parameter list will become obsolete, except if the function declarator
is a prototype. In other words, you can have
int foo();
but not
int foo(){ ... }
which should be written as int foo(void){ ... }. Is this correct?
and if so, why do we allow parameterless prototypes? What are they
useful for? The only reason I can think is to link to some external
definition of a function whose parameters are unknown, but in what
case would you want to call a function with an unknown number and type
of parameters?
Regards,
B.
Keith Thompson wrote:
> bo*******@gmail .com writes:
I don't understand the difference between these two declarations
int foo(char a, char b)
This is a prototype.
{
...
}
int foo(a, b)
char a, b;
This is an old-style declaration, sometimes called "K&R style" because
it's used in the first edition K&R (Kernighan & Ritchie, _The C
Programming Language_.
{
...
}
What is the usefulness of the second form? Is there any difference?
K&R style function declarations are effectively obsolescent, though
they're still legal. Prior to the first ANSI C standard, in 1989,
they were the only form available, but the standardization committee
borrowed the first form (prototypes) from an early version of C++.
It's useful to recognize K&R style declarations when you encounter
them in old code, but you should never use them in new code (unless,
for some strange reason, you need to support an ancient compiler).
In this particular case, there's another difference. Due to argument
promotions, the parameters in the K&R declaration are really of type
int. The parameters in the prototype really are of type char.
What do you mean?
Argument promotions apply to arguments, not parameters.
/* BEGIN new.c */
#include <stdio.h>
int foo(a, b)
char a, b;
{
return printf("sizeof a is %u\n", (unsigned)sizeo f a);
}
int main(void)
{
foo(0,0);
return 0;
}
/* END new.c */
--
pete
pete <pf*****@mindsp ring.comwrites:
Keith Thompson wrote:
>bo*******@gmail .com writes:
I don't understand the difference between these two declarations
int foo(char a, char b)
This is a prototype.
{
...
}
int foo(a, b)
char a, b;
This is an old-style declaration, sometimes called "K&R style" because it's used in the first edition K&R (Kernighan & Ritchie, _The C Programming Language_.
{
...
}
[...]
>In this particular case, there's another difference. Due to argument promotions, the parameters in the K&R declaration are really of type int. The parameters in the prototype really are of type char.
What do you mean?
Argument promotions apply to arguments, not parameters.
/* BEGIN new.c */
#include <stdio.h>
int foo(a, b)
char a, b;
{
return printf("sizeof a is %u\n", (unsigned)sizeo f a);
}
int main(void)
{
foo(0,0);
return 0;
}
/* END new.c */
(The above program prints "sizeof a is 1".)
You're right, I was confused. I remembered that mechanically
converting old-style declarations to prototypes can cause problems for
parameters of integer types narrower than int and floating-point types
narrower than double, but I drew the wrong conclusion.
If you pass arguments of type char to foo, they'll be promoted to int
(because the parameter type information isn't available at the point
of the call), and then reconverted back to char. Because of this,
back in the Old Days, parameters of types that are subject to
promotion were generally discouraged.
With a prototype, char arguments are simply passed and stored as type
char.
My poor excuse is that I haven't had to use old-style function
declarations in a long time. I *think* I've got it right this time.
--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[comp.lang.c] Keith Thompson <ks***@mib.orgw rote:
K&R style function declarations are effectively obsolescent, though
they're still legal.
And not even deprecated, right? I suppose one day C will lose enough
of these legacy features to make the IOCCC obsolete, but I suspect
I will be long dead (and I'm 27 <g>).
--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gma il.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.or g | Google groups, due to rampant unchecked spam.
Christopher Benson-Manica <at***@otaku.fr eeshell.orgwrit es:
[comp.lang.c] Keith Thompson <ks***@mib.orgw rote:
>K&R style function declarations are effectively obsolescent, though they're still legal.
And not even deprecated, right? I suppose one day C will lose enough
of these legacy features to make the IOCCC obsolete, but I suspect
I will be long dead (and I'm 27 <g>).
C99 6.11.7:
The use of function definitions with separate parameter identifier
and declaration lists (not prototype-format parameter type and
identifier declarators) is an obsolescent feature.
The introduction to the standard says:
Certain features are obsolescent, which means that they may be
considered for withdrawal in future revisions of this
International Standard. They are retained because of their
widespread use, but their use in new implementations (for
implementation features) or new programs (for language [6.11] or
library features [7.26]) is discouraged.
--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
On Wed, 26 Sep 2007 01:40:42 -0000, bo*******@gmail .com wrote:
<snip: K&R1 format>
Thanks for the info, I guessed it was an old obsolescent form, and had
this confirmed after rereading it and seeing the footnote which
directed me to the following two sections:
6.11.6 Function declarators
The use of function declarators with empty parentheses (not prototype-
format parameter type declarators) is an obsolescent feature.
6.11.7 Function definitions
The use of function definitions with separate parameter identifier and
declaration lists (not prototype-format parameter type and identifier
declarators) is an obsolescent feature.
Reading 6.11.6 reads to me that function declarators with an empty
parameter list will become obsolete, except if the function declarator
is a prototype. In other words, you can have
int foo();
but not
int foo(){ ... }
which should be written as int foo(void){ ... }. Is this correct?
No. In C, a function declarator with empty parentheses is not a
prototype. The parenthesised(! ) clauses in 6.11.6,7 are (additional)
explanations of the statements NOT limitations on their applicability.
<OTC++ is different: K&R1 style is dropped, ALL function declarators
have prototype syntax and semantics, and empty parentheses mean no
parameters. In C++ you MAY also use (void) to mean explicitly zero
parameters, in C you MUST use it to get that meaning. </>
<snip points that depend on false assumption>
Nit: it doesn't actually say they 'will' become obsolete, or more
specifically that they will be deleted. It says they _may_, and some
people believe they _should_, but AFAICT the standards process does
not allow a binding commitment to future action. But for the reasons
already discussed, you should avoid them even if they aren't deleted.
- formerly david.thompson1 || achar(64) || worldnet.att.ne t This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Putz Ronald |
last post by:
Hy!
I am trying to convert a win Oracle application to solaris! We have use
external c functions which I have ibncluded in a shared object. See the
makefile below:
OBJECTS = definitions.o eventlog.o arc_xpi.o OracleFuncs.o
DEFS = -DOS_SOLARIS -fPIC -G
|
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...
|
by: Kobu |
last post by:
Does anyone know how old style function definitions differ in
"behaviour" to new style function definitions?
I heard the old style function definitions caused integeral promotion
and floating point promotion on the formal parameters. Is this true?
Is it okay for me to set up a new style funciton prototype when calling
old style function definitions (for legacy code)? Is this okay?
|
by: Deniz Bahar |
last post by:
Hi,
I would like to call one of my functions the exact name as an existing
C library function (for example K&R2 exercises asks me to make an atof
function). If I don't include the header with the declaration for the
C library function (stdlib.h in this case) then define/declare my own
function with the same name, am I safe? It seems to work on my
compiler, but I wonder if this is portable or even acceptable? thx
|
by: baumann |
last post by:
hi all,
typedef int (*pfunc)(int , int);
pfunc a_func;
i know it's ok,
but how can define a_func without typedef statement?
thanks .
| |
by: Xiaoshen Li |
last post by:
Dear All,
I am confused with prototypes in C. I saw the following code in a C book:
void init_array_1(int data)
{
/* some code here */
}
|
by: Olov Johansson |
last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7
and Konqueror 3.5) has support not only for standard function
definitions, function expressions (lambdas) and Function constructors
(these three I knew about), but also conditional function definitions,
as described in http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Defining_Functions
].
An example:
function fun() {
|
by: K. Jansma |
last post by:
Hi,
given the following example class
class Test:
def f(self,a, L=):
L.append(a)
return L
and the following statements
|
by: svata |
last post by:
Hello there,
after some time of pondering I come to some solution which would suit
me best. Please correct, if I am wrong.
Function has two parameters. A string array, better said a pointer to
it, char *p_buf and int size.
int read_name( char *p_buf, int size) {
char *p_item_name_1;
|
by: H9XLrv5oXVNvHiUI |
last post by:
Hi, I have a question about injecting friend functions within template
classes. My question is specific to gcc (version 3.4.5) used in
combination with mingw because this code (or at least code that gets
the same result) works as expected in visualc++. I know that this is
probably not the right behavior for a compiler but it's the kind of
behavior I'm searching for so I was hoping there was a way to do the
same thing in gcc.
As you know...
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |