P
paytam
hi all
I want to redefine a function getchar() in header stdio.h ,but I don't
know what should I do.
I want to redefine a function getchar() in header stdio.h ,but I don't
know what should I do.
I want to redefine a function getchar() in header stdio.h ,but I don't
know what should I do.
hi all
I want to redefine a function getchar() in header stdio.h ,but I don't
know what should I do.
hi all
I want to redefine a function getchar() in header stdio.h ,but I don't
know what should I do.
hi all
I want to redefine a function getchar() in header stdio.h ,but I don't
know what should I do.
open the header file stdio.h - /usr/include/stdio.h and change it to
whatever u want.
done.
open the header file stdio.h - /usr/include/stdio.h and change it
to whatever u want.
Chris Dollin said:You will be.
First, editing the header file doesn't redefine any functions, always
supposing you can do it.
Second, if the OP is J Random Unix User, they won't be able to edit
/usr/include/stdio.h, because they won't have permission to do so.
Third, if the OP is J Random RISC OS User, there /isn't/ a file
called /usr/include/stdio.h, there isn't a directory called
/usr/include, there isn't a directory called /usr, and there isn't
a directory called /.
Fourth, in any case the standard includes need not be "files" /at
all/, so there may be nothing to edit except the compiler
executable. The term "playing with fire" doesn't begin to cover
it.
CBFalconer said:This has to be one of the most imbecelic (and wrong) replies ever
seen in c.l.c,
Maybe so, but many have considered some of *my* postings a
worthy contender for that hotly contested title
Anyway, what's the accepted wisdom about doing the following?
(Is it at least well defined?)
-------------
#include <stdio.h>
int mygetchar (void)
{
printf ("My getchar\n");
return getchar();
}
#define getchar() mygetchar()
int main (void)
{
char c = getchar ();
printf ("got char '%c'\n", c);
return 0;
}
goose said:.... snip ...
Anyway, what's the accepted wisdom about doing the following?
(Is it at least well defined?)
-------------
#include <stdio.h>
int mygetchar (void)
{
printf ("My getchar\n");
return getchar();
}
#define getchar() mygetchar()
int main (void)
{
char c = getchar ();
printf ("got char '%c'\n", c);
return 0;
}
Andrew said:(Should be "#define getchar mygetchar" without the parentheses).
Whether it's a wise thing to do is questionable.
CBFalconer said:Strictly speaking it is UB. redefining entries in the standard
library that have been referenced via the appropriate headers is
not allowed.
In practice, this particular one MAY work. No guarantees.
I take it back. It will work, because you never use the define.
Now think about why I said that.
goose said:CBFalconer wrote:
.... snip ...
I did. I'm afraid I miss the point; AFAICT, getchar (the #define)
should get text-substituted with mygetchar. I'd be happy to hear
why that won't happen (Are we talking recursive mygetchar() bomb?
thats simple enough to fix by replacing
getchar ();
in mygetchar with
(getchar) ();
although the @define follows the use of the function getchar()).
CBFalconer said:You defined a functional macro with getchar(). You never used it,
because the subsequent occurance of getchar was followed by a
blank.
Andrew Poelstra said:[snip]int mygetchar (void)
{
printf ("My getchar\n");
return getchar();
}
#define getchar() mygetchar()
(Should be "#define getchar mygetchar" without the parentheses).
Well, either should be okay (can't test it right now, no compiler
handy) with the only difference being a matter of taste. I'd rather
leave the () in 'cos then a simple scanning of the #define tells
me that its used "just like a function call" further on.
We-ell ... the OP asked a very unwise thing to do anyway, one
can only hope it is because he wants to put it to a /wise/
use, and not willy-nilly trample all over the standard library.
Andrew Poelstra said:It would be easier to read, but if you accidentally put a space in
there, getchar will be replaced with "() mygetchar()" instead of just
"mygetchar()".
Whether it's a wise thing to do is questionable. It actually seems like
a good idea in some debugging cases. (For example, redefining malloc to
print a message to stdout about how much memory is allocated or when).
I can't think of why it would be used in production code, though.
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.