473,382 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

printf prototype UB

Would this by any chance invoke undefined behavior?

extern int printf(const char *, ...); int main(void) {
printf("Hello\n"); return 0; }

That is, providing my own printf prototype would invoke undefined
behavior in anyway? Assuming the implementation is c89 of course.
Nov 13 '05 #1
14 5532
Mantorok Redgormor <ne*****@tokyo.com> scribbled the following:
Would this by any chance invoke undefined behavior? extern int printf(const char *, ...); int main(void) {
printf("Hello\n"); return 0; } That is, providing my own printf prototype would invoke undefined
behavior in anyway? Assuming the implementation is c89 of course.


No, I don't think it will invoke undefined behaviour. If the prototype
has the correct form, the compiler won't care who supplied it, you or
the preprocessor.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
and vow excitedly zooming."
- JIPsoft
Nov 13 '05 #2
In <41**************************@posting.google.com > ne*****@tokyo.com (Mantorok Redgormor) writes:
Would this by any chance invoke undefined behavior?

extern int printf(const char *, ...); int main(void) {
printf("Hello\n"); return 0; }

That is, providing my own printf prototype would invoke undefined
behavior in anyway? Assuming the implementation is c89 of course.


As long as your declaration is compatible with the function interface
specified by the relevant standard, everything is OK.

The main argument against doing it is that many functions have different
interfaces in the two C standards. This doesn't affect correct code that
uses the implementation-provided declaration, but it is theoretically
problematic to code providing its own declarations (even if you try to
accomodate both C89 and C99, who knows what's going to happen if C0x
ever gets released and implemented?).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
No, I don't think it will invoke undefined behaviour. If the prototype
has the correct form, the compiler won't care who supplied it, you or
the preprocessor.


Under what circumstances would supplying the prototype yourself be advisable?

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Nov 13 '05 #4
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:bl**********@chessie.cirr.com...
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
No, I don't think it will invoke undefined behaviour. If the prototype
has the correct form, the compiler won't care who supplied it, you or
the preprocessor.


Under what circumstances would supplying the prototype yourself be

advisable?

None, as far as I'm concerned.

IMO it's easier to type

#include <stdio.h>

and get it right the first time.

If you foul up the 'hand made' declaration, it
might not become evident until your program
behaves in a bizarre fashion.

-Mike
Nov 13 '05 #5
On 26 Sep 2003 02:41:17 -0700, ne*****@tokyo.com (Mantorok Redgormor)
wrote in comp.lang.c:
Would this by any chance invoke undefined behavior?

extern int printf(const char *, ...); int main(void) {
printf("Hello\n"); return 0; }

That is, providing my own printf prototype would invoke undefined
behavior in anyway? Assuming the implementation is c89 of course.


From ANSI89/ISO90 standard:

"Provided that a library function can, be declared without reference
to any type defined in a header, it is also permissible to declare the
function, either explicitly or implicitly, and use it without
including its associated header. If a function that accepts a variable
number of arguments is not declared (explicitly or by including its
associated header), the behavior is undefined."

So as long as the prototype is correct, it is perfectly well defined.

--
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.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #6
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
No, I don't think it will invoke undefined behaviour. If the prototype
has the correct form, the compiler won't care who supplied it, you or
the preprocessor.


Under what circumstances would supplying the prototype yourself be advisable?


Under no circumstances I can think of. Furthermore supplying only the
prototype of printf might not be sufficient to make things work (think
about stdout).

Regards

Irrwahn
--
ERROR 103: Dead mouse in hard drive.
Nov 13 '05 #7
In article <hp********************************@4ax.com>,
Irrwahn Grausewitz <ir*****************@freenet.de> wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
No, I don't think it will invoke undefined behaviour. If the prototype
has the correct form, the compiler won't care who supplied it, you or
the preprocessor.
Under what circumstances would supplying the prototype yourself be advisable?


Under no circumstances I can think of.


If for some reason you want to be able to put an entire program on
one line (f'rexample, wanting a program to print its own source code
without having embedded backslashes to worry about escaping in the
output, as was discussed here not long ago), you'd need some way to
avoid preprocessor directives.

Whether or not this is a good reason is subject to debate. I can't
think of any other reasons that could claim to be good ones.

Furthermore supplying only the
prototype of printf might not be sufficient to make things work (think
about stdout).
The code that calls printf need not know anything about stdout; the
implementation of printf (in the runtime library) needs to know where to
put its output, but as long as the calling code calls it correctly (which
in the case of printf means having a correct prototype in scope[1][2]
and matching the arguments to the format string), the implementation is
required to make it work with no further requirements on the calling code.
dave

[1] This is best done by using "#include <stdio.h>" (you'll get no
argument about the "best" here), but "int printf(const char *,...);"
(in C89) is also perfectly valid.

[2] The requirement to have a prototype in scope is a result of printf
being a variadic function. In general, you can call a non-variadic
function whose arguments aren't subject to promotion without a
prototype[3], though there's no excuse for actually doing so.

[3] Question for the language lawyers: Is this legal? (There's no
argument that it's Evil, but that's not the same question.)
/*Not a prototype, but makes sure we have the correct return type*/
void *malloc();
/*size_t is assumed to not be subject to the usual promotions.
(Is this guaranteed?)
Note that we use sizeof to get a size_t without actually having
the implementation's definition in scope.
*/
struct foo *foo_ptr=malloc(sizeof *foo);

--
Dave Vandervies dj******@csclub.uwaterloo.caHow neat do I have to be with my garbage?

That depends. How much do you want your programs to smell?
--Al Morgan and Richard Heathfield in comp.lang.c
Nov 13 '05 #8

"Irrwahn Grausewitz" <ir*****************@freenet.de> wrote in message
news:hp********************************@4ax.com...
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
No, I don't think it will invoke undefined behaviour. If the prototype
has the correct form, the compiler won't care who supplied it, you or
the preprocessor.
Under what circumstances would supplying the prototype yourself be

advisable?
Under no circumstances I can think of. Furthermore supplying only the
prototype of printf might not be sufficient to make things work (think
about stdout).


I'm not supporting the 'handmade' prototypes for lib
functions, but your 'reason' above isn't valid, since
there's no need to refer to 'stdout' when invoking
'printf()' (Unless of course you're wanting to print
its value).

-Mike
Nov 13 '05 #9
"Mike Wahler" <mk******@mkwahler.net> wrote:

"Irrwahn Grausewitz" <ir*****************@freenet.de> wrote in message
news:hp********************************@4ax.com.. .
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
>Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
>
>> No, I don't think it will invoke undefined behaviour. If the prototype
>> has the correct form, the compiler won't care who supplied it, you or
>> the preprocessor.
>
>Under what circumstances would supplying the prototype yourself be

advisable?

Under no circumstances I can think of. Furthermore supplying only the
prototype of printf might not be sufficient to make things work (think
about stdout).


I'm not supporting the 'handmade' prototypes for lib
functions, but your 'reason' above isn't valid, since
there's no need to refer to 'stdout' when invoking
'printf()' (Unless of course you're wanting to print
its value).

Hm, right, thanks to you and Dave for pointing out.

Irrwahn
--
ERROR 103: Dead mouse in hard drive.
Nov 13 '05 #10
dj******@csclub.uwaterloo.ca (Dave Vandervies) wrote:
Irrwahn Grausewitz <ir*****************@freenet.de> wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Under what circumstances would supplying the prototype yourself be advisable?


Under no circumstances I can think of.


If for some reason you want to be able to put an entire program on
one line (f'rexample, wanting a program to print its own source code
without having embedded backslashes to worry about escaping in the
output, as was discussed here not long ago), you'd need some way to
avoid preprocessor directives.

Whether or not this is a good reason is subject to debate.


Indeed...
--
ERROR 103: Dead mouse in hard drive.
Nov 13 '05 #11
On Fri, 26 Sep 2003 16:18:31 +0000 (UTC), in comp.lang.c , Christopher
Benson-Manica <at***@nospam.cyberspace.org> wrote:
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
No, I don't think it will invoke undefined behaviour. If the prototype
has the correct form, the compiler won't care who supplied it, you or
the preprocessor.


Under what circumstances would supplying the prototype yourself be advisable?


when you need the function prototype, but want to avoid including the
header due to side-effects of other prototypes, declarations or
definitions.

For example you have to maintain badly written legacy code that
#defines NULL to be something other than 0 or (void*)0, and fixing the
real bug is not an option.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #12
Mark McIntyre wrote:

On Fri, 26 Sep 2003 16:18:31 +0000 (UTC), in comp.lang.c , Christopher
Benson-Manica <at***@nospam.cyberspace.org> wrote:
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
No, I don't think it will invoke undefined behaviour. If the prototype
has the correct form, the compiler won't care who supplied it, you or
the preprocessor.


Under what circumstances would supplying the prototype yourself be advisable?


when you need the function prototype, but want to avoid including the
header due to side-effects of other prototypes, declarations or
definitions.

For example you have to maintain badly written legacy code that
#defines NULL to be something other than 0 or (void*)0, and fixing the
real bug is not an option.


If you're writing (or tempted to write) the prototype
yourself, it's clear you already have license to modify the
freaky code. True, this doesn't necessarily imply that you
have license to fix all its problems, but some amount of
change is clearly in the wind.

Another point of view is to consider the language in which
this screwball code is written: should that language be called
"C" or something else? It seems to me that defining NULL as
(char*)42 or using it as a function name or any such similar
abuse is reason to doubt the "C-ness" of the code. (And to
doubt the sanity of the coder, too.)

#define void int
void main(void argc, char **argv) {
return !puts("Hello, world!");
}

.... is at best "Sort-Of-C," not "C."

--
Er*********@sun.com
Nov 13 '05 #13
On Mon, 29 Sep 2003 11:29:26 -0400, in comp.lang.c , Eric Sosman
<Er*********@sun.com> wrote:
Mark McIntyre wrote:

when you need the function prototype, but want to avoid including the
header due to side-effects of other prototypes, declarations or
definitions.
If you're writing (or tempted to write) the prototype
yourself, it's clear you already have license to modify the
freaky code. True, this doesn't necessarily imply that you
have license to fix all its problems, but some amount of
change is clearly in the wind.


I think you missed my point. I'm not rewriting the dodgy legacy code
here. I'm maintaining something that uses it. I can't change that
header because its used by a zillion other house projects. So I'm
stuck with the fact that its header declares functions strtod() and
frexp(), macros __LINE__ etc.
Another point of view is to consider the language in which
this screwball code is written: should that language be called
"C" or something else?


I like to call it "a bl**dy mess" :-)
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #14
In <3F***************@sun.com> Eric Sosman <Er*********@sun.com> writes:
#define void int
void main(void argc, char **argv) {
return !puts("Hello, world!");
}

... is at best "Sort-Of-C," not "C."


Well, it happens to be a strictly conforming C program :-) That is,
if we assume that the definition of a s.c. program allows its existence...

Oddly enough, the standard allows replacing the C keywords by macros
if no header is included while the replacement is still in effect.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #15

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

Similar topics

8
by: aditya | last post by:
hi, Can anybody please tell me that how the following printf(...) statement works- main(){ int d=9; printf("%d",printf("%d")); return 0;
33
by: Kenneth Brody | last post by:
I know that passing printf() too few arguments, or arguments of the wrong type invokes UB. However, what about passing too many arguments, if the expected arguments are of the correct type? For...
2
by: john | last post by:
Hello, Supposed sizeof(short) == 2 and sizeof(int) == 4 and argument passing in a stack. When doing: int i1; int i2; printf("%d %d", i1, i2); // OK. 2 arg of size 4
10
by: lovecreatesbeauty | last post by:
Is parameter type conversion required for the 2nd argument on printf("%p", (void *)&i); ? But one would never call memcpy like: memcpy((void *)pi, (void *)pj, sizeof *pj); /*memcpy((void *)pi,...
16
by: Gernot Frisch | last post by:
Hi, class MyString { char* m_data; public: MyString(const char* c) { m_data = new char; strcpy(m_data, c);
18
by: sam_cit | last post by:
Hi Everyone, int main() { printf("not included stdio.h"); } Yes, i haven't included stdio.h and my compiler would generate a warning and would assume that it would return a int, my question...
34
by: Old Wolf | last post by:
Is there any possible situation for printf where %hd causes a different result to %d, and the corresponding argument was of type 'short int' ?
12
by: Spoon | last post by:
Hello, Does the following code invoke undefined behavior? #include <stdio.h> int main(void) { unsigned short int u = 42; printf("%u\n", u); return 0;
4
by: goutham1 | last post by:
hai can any body help me iam starting my c programming. i started my basic programme that is adding two numbers when i compiled my programme in cpp in the print f statement it is telling printf...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.