Why the following code is compilable? The function abc() doesn't take any
arguments, still i can call the function with arbitraty number of arguments.
Even compiler doesn't show any warning. What the standard says?
----- file1.c ------
extern unsigned abc();
int main()
{
unsigned *chip_offset;
abc(&chip_offse t, 10);
/* do something with pointer - which is errornous */
return 0;
}
----- file2.c -----
unsigned abc()
{
unsigned some_address;
some_address = 0xFFBA;
return some_address;
}
-Neo
"If you don't program yourself, life will program you!" 11 1923
Neo <ti************ ***@yahoo.com> wrote: Why the following code is compilable? The function abc() doesn't take any arguments, still i can call the function with arbitraty number of arguments. Even compiler doesn't show any warning. What the standard says?
UB
(N869)
6.5.2.2 Function calls
[#6] 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 number of
arguments does not agree with the number of parameters, the
behavior is undefined. If the function is defined with a
----- file1.c ------ extern unsigned abc();
Here above you declare abc to be a function with unspecified number
of parameters (ie. function without a prototype). The compiler
does not assume anything about the number of arguments (except
that it is constant). (note: this is unlike in C++.)
This is the declaration of abc as a function with no parameters:
extern unsigned abc(void);
int main() { unsigned *chip_offset; abc(&chip_offse t, 10); /* do something with pointer - which is errornous */ return 0; }
----- file2.c ----- unsigned abc() { unsigned some_address; some_address = 0xFFBA; return some_address; }
--
Stan Tobias
mailx `echo si***@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
S.Tobias wrote: Neo <ti************ ***@yahoo.com> wrote:
Why the following code is compilable? The function abc() doesn't
take any arguments, still i can call the function with arbitraty number of
arguments. Even compiler doesn't show any warning. What the standard says?
UB
(N869) 6.5.2.2 Function calls [#6] 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 number of arguments does not agree with the number of parameters, the behavior is undefined. If the function is defined with a
I don't think this applies here. He has a function declarator
that is not part of a definition, which signals to the
compiler that there is no information given as to the number of
arguments to the function. So the use seems to be perfectly okay
aside from the fact that it is considered obsolescent. ----- file1.c ------ extern unsigned abc();
Here above you declare abc to be a function with unspecified number of parameters (ie. function without a prototype). The compiler does not assume anything about the number of arguments (except that it is constant). (note: this is unlike in C++.)
This is the declaration of abc as a function with no parameters:
extern unsigned abc(void);
int main() { unsigned *chip_offset; abc(&chip_offse t, 10); /* do something with pointer - which is errornous */ return 0; }
----- file2.c ----- unsigned abc() { unsigned some_address; some_address = 0xFFBA; return some_address; }
-- Stan Tobias mailx `echo si***@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
--
aegis
"aegis" <ae***@mad.scie ntist.com> writes: S.Tobias wrote: Neo <ti************ ***@yahoo.com> wrote:
> Why the following code is compilable? The function abc() doesn't take any > arguments, still i can call the function with arbitraty number of arguments. > Even compiler doesn't show any warning. What the standard says?
UB
(N869) 6.5.2.2 Function calls [#6] 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 number of arguments does not agree with the number of parameters, the behavior is undefined. If the function is defined with a
I don't think this applies here. He has a function declarator that is not part of a definition, which signals to the compiler that there is no information given as to the number of arguments to the function. So the use seems to be perfectly okay aside from the fact that it is considered obsolescent.
The "extern" declaration of abc() says it takes an unspecified number
of arguments. The *definition* of abc() in file2.c says it takes no
arguments. The call passes two arguments. Undefined behavior.
--
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.
"Neo" <ti************ ***@yahoo.com> wrote in message
news:35******** *****@individua l.net... Why the following code is compilable? The function abc() doesn't take any arguments, still i can call the function with arbitraty number of
arguments. Even compiler doesn't show any warning. What the standard says?
----- file1.c ------ extern unsigned abc();
This prototype does not indicate 'no arguments'.
It indicates 'arguments not specified'. It's
also a fragile way to do things.
-Mike
Mike Wahler wrote: "Neo" <ti************ ***@yahoo.com> wrote... Why the following code is compilable? The function abc() doesn't take any arguments, still i can call the function with arbitraty number of arguments. Even compiler doesn't show any warning. What the standard says?
----- file1.c ------ extern unsigned abc(); This prototype does not indicate 'no arguments'.
It's not a prototype (in C.) It's only a function declaration.
It indicates 'arguments not specified'. It's also a fragile way to do things.
--
Peter
"Peter Nilsson" <ai***@acay.com .au> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com... Mike Wahler wrote: "Neo" <ti************ ***@yahoo.com> wrote... Why the following code is compilable? The function abc() doesn't take any arguments, still i can call the function with arbitraty number of arguments. Even compiler doesn't show any warning. What the standard says?
----- file1.c ------ extern unsigned abc();
This prototype does not indicate 'no arguments'.
It's not a prototype (in C.) It's only a function declaration.
You're right, I should have written 'declaration'.
-Mike
"Peter Nilsson" <ai***@acay.com .au> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com... Mike Wahler wrote: "Neo" <ti************ ***@yahoo.com> wrote... > Why the following code is compilable? The function abc() doesn't > take any arguments, still i can call the function with arbitraty > number of arguments. > Even compiler doesn't show any warning. What the standard says? > > ----- file1.c ------ > extern unsigned abc(); This prototype does not indicate 'no arguments'.
It's not a prototype (in C.) It's only a function declaration.
What is the difference between a function declaration and a function
prototype? It indicates 'arguments not specified'. It's also a fragile way to do things.
-- Peter
-Neo
On Thu, 20 Jan 2005 08:45:59 +0530, "Neo"
<ti************ ***@yahoo.com> wrote in comp.lang.c: "Peter Nilsson" <ai***@acay.com .au> wrote in message news:11******** *************@z 14g2000cwz.goog legroups.com... Mike Wahler wrote: "Neo" <ti************ ***@yahoo.com> wrote... > Why the following code is compilable? The function abc() doesn't > take any arguments, still i can call the function with arbitraty > number of arguments. > Even compiler doesn't show any warning. What the standard says? > > ----- file1.c ------ > extern unsigned abc();
This prototype does not indicate 'no arguments'.
It's not a prototype (in C.) It's only a function declaration.
What is the difference between a function declaration and a function prototype?
It indicates 'arguments not specified'. It's also a fragile way to do things.
-- Peter
-Neo
I already posted an extensive answer to your original post in
comp.arch.embed ded.
In the future, if you are going to post the same article to multiple
groups, after making sure that it is topical in the multiple groups,
cross-post it, that is put all the newsgroup names in a single post.
As to this question, a function prototype specifies the name of a
function, the type it returns, and the number and types of arguments:
int fred(double ricky, char *ethel);
....is a prototype, as is:
int fred(double, char *);
....because names for the arguments are optional except when you define
the body of a function.
Every line that specifies a function name, return type, and a pair of
parentheses is a function declaration. If it also includes the types
of the parameters, it is a prototype.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
In article <35************ *@individual.ne t>
Neo <ti************ ***@yahoo.com> wrote: What is the difference between a function declaration and a function prototype?
To be a prototype, you have to have at least one type-name inside
the parentheses:
int zorg(); /* declaration but not prototype */
int zorg(void); /* declaration and prototype */
The empty parentheses syntax exists for compatibility with pre-1989
C code. It is best not to use it in any code written since 1989,
because prototypes supply more information, so that compilers can
check the actual calls to the functions.
--
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Chuck Anderson |
last post by:
I have a function with 7 inputs. The last three have default values.
I want to call that function specifying the first four, skip two and
then specify the last.
I thought I could write this as :
$retval = myfunction(arg1, arg2, arg3, arg4,,,arg7);
.... but Php does not seem to want to let me do this. I get a parse
|
by: Martin Proulx |
last post by:
Hello,
I'd like to know how to correctly declare a member function so that it takes as
parameter any function object, of which the operator() takes arguments of
specific types.
The idea is to be able to build this function object using the standard binders,
or boost's bind.
Here's an example that shows what can work with function pointers, or a
|
by: |
last post by:
How to call a function with variable argument list from another function
again with variable argument list?
Example :
double average ( int num, ... );
double AFunct1 ( int num, ... );
double AFunct2 ( int num, ... );
double average ( int num, ... )
|
by: Nicolas Vigier |
last post by:
Hello,
I have in my python script a function that look like this :
def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42):
if type(arg1) is ListType:
for a in arg1:
my_function(a, arg2, opt1=opt1, opt2=opt2, opt3=opt3)
return
if type(arg2) is ListType:
|
by: Marlene Stebbins |
last post by:
I am experimenting with function pointers. Unfortunately, my C book has
nothing on function pointers as function parameters. I want to pass a
pointer to ff() to f() with the result that f() prints the return value
of ff(). The code below seems to work, but I would appreciate your
comments. Have I got it right? Does the function name "decay" to a pointer?
#include <stdio.h>
/* declares a function which takes an argument
that is a...
| |
by: Fredrik Tolf |
last post by:
If I have a variable which points to a function, can I check if certain
argument list matches what the function wants before or when calling it?
Currently, I'm trying to catch a TypeError when calling the function
(since that is what is raised when trying to call it with an illegal
list), but that has the rather undesirable side effect of also catching
any TypeErrors raised inside the function. Is there a way to avoid that?
Fredrik Tolf
|
by: Beta What |
last post by:
Hello,
I have a question about casting a function pointer. Say I want to make
a generic module (say some ADT implementation) that requires a function
pointer from the 'actual/other modules' that takes arguments of type
(void *) because the ADT must be able to deal with any type of data.
In my actual code, I will code the function to take arguments of their
real types, then when I pass this pointer through an interface
function, I...
|
by: Larax |
last post by:
Best explanation of my question will be an example, look below at this
simple function:
function SetEventHandler(element)
{
// some operations on element
element.onclick =
function(event)
{
|
by: kris |
last post by:
Hi I have written a program which prints the hostid on a linux system.
The programm uses gethostid() function call which is defined in the
library file unistd.h. But my programm gets compiled without any
warnings even if I didnot include any of the header files.
can I know how does this happen i.e how does the compiler identifies
this function gethostid.
Is there any default path from where the compiler picks up the
definition from.
|
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...
|
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: 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...
|
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: 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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |