function call with arguments which takes no arguments

N

Neo

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_offset, 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!"
 
S

S.Tobias

Neo said:
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);
 
A

aegis

S.Tobias said:
Neo said:
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.
 
K

Keith Thompson

aegis said:
S.Tobias said:
Neo said:
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.
 
M

Mike Wahler

Neo said:
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
 
N

Neo

Peter Nilsson said:
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?

-Neo
 
C

Chris Torek

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.
 
J

Jack Klein

What is the difference between a function declaration and a function
prototype?


-Neo

I already posted an extensive answer to your original post in
comp.arch.embedded.

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.
 
N

Neo

Jack Klein said:
I already posted an extensive answer to your original post in
comp.arch.embedded.

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.

O'kay, Thanks JK.
 
S

sushant

if you dont want to pass any arguments then u have to
explicitly mention the keyword void with the function name
i.e. func( void ), otherwise it'll take any type n any num of
arguments
.. this is C89 but in C99 it'll give u the error as u were
expecting ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top