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. 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
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
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 |
"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
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
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.
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
"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
"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. 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.
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>
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
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>
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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;
|
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...
|
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
|
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,...
|
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);
|
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...
|
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' ?
|
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;
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
| |