paytam@gmail.co m said:
[color=blue]
> hi all
> I want to redefine a function getchar() in header stdio.h ,but I don't
> know what should I do.[/color]
Want something else instead. Redefining standard library functions is a bad
idea.
But if you just want to be able to use a different name for it, that's fine.
Just do something like this:
#include <stdio.h>
#define paytamTriesToGe tACharFromStdin getchar
int main(void)
{
int ch = 0;
while((ch = paytamTriesToGe tACharFromStdin ()) != EOF)
{
putchar(ch);
}
return 0;
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
paytam@gmail.co m wrote:
[color=blue]
> I want to redefine a function getchar() in header stdio.h ,but I don't
> know what should I do.[/color]
You should ask in a newsgroup dedicated to your particular compiler
suite, since in the general case the ISO C Standard declares this to
cause undefined behaviour.
paytam@gmail.co m wrote:[color=blue]
> hi all
> I want to redefine a function getchar() in header stdio.h ,but I don't
> know what should I do.[/color]
open the header file stdio.h - /usr/include/stdio.h and change it to
whatever u want.
done.
paytam@gmail.co m wrote:
[color=blue]
> hi all
> I want to redefine a function getchar() in header stdio.h ,but I don't
> know what should I do.[/color]
Something else.
[You can't do so portably, implementation-specific techniques are implementation
specific and generally offtopical, and you haven't explained why you want to
pull the rug out from under your victims' feet.]
--
Chris "look! a unicorn! (fx:hack) I mean /horse/, sorry!" Dollin
"Who are you? What do you want?" /Babylon 5/
rrs.matrix@gmai l.com wrote:
[color=blue]
> paytam@gmail.co m wrote:[color=green]
>> hi all
>> I want to redefine a function getchar() in header stdio.h ,but I don't
>> know what should I do.[/color]
>
> open the header file stdio.h - /usr/include/stdio.h and change it to
> whatever u want.
> done.[/color]
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.
Finally, even if you /can/ do any of this, it's very likely you
shouldn't. `getchar` is supposed to do what `getchar` is supposed
to do - and people use it to do that.
--
Chris "making 'finally' topical on c.l.c" Dollin
"Who are you? What do you want?" /Babylon 5/
rrs.matrix@gmai l.com wrote:[color=blue]
> paytam@gmail.co m wrote:[color=green]
>>
>> I want to redefine a function getchar() in header stdio.h ,but I
>> don't know what should I do.[/color]
>
> open the header file stdio.h - /usr/include/stdio.h and change it
> to whatever u want.[/color]
This has to be one of the most imbecelic (and wrong) replies ever
seen in c.l.c, not to mention the childish use of 'u'.
--
Chuck F (cbfalconer@yah oo.com) (cbfalconer@mai neline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE maineline address!
Chris Dollin <chris.dollin@h p.com> wrote:
[color=blue]
> rrs.matrix@gmai l.com wrote:
>[color=green]
> > open the header file stdio.h - /usr/include/stdio.h and change it to
> > whatever u want.
> > done.[/color]
>
> 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.[/color]
Fifth, if you try to do this on a multi-user system the sysadmin will
start withdrawing your privileges with extreme prejudice. Try doing it
twice and the privileges withdrawn may well include the right to draw
breath.
CBFalconer wrote:[color=blue]
> rrs.matrix@gmai l.com wrote:[color=green]
> > paytam@gmail.co m wrote:[color=darkred]
> >>
> >> I want to redefine a function getchar() in header stdio.h ,but I
> >> don't know what should I do.[/color]
> >
> > open the header file stdio.h - /usr/include/stdio.h and change it
> > to whatever u want.[/color]
>
> This has to be one of the most imbecelic (and wrong) replies ever
> seen in c.l.c,[/color]
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;
}
On 2006-06-23, goose <ruse@webmail.c o.za> wrote:[color=blue]
> CBFalconer wrote:[color=green]
>> rrs.matrix@gmai l.com wrote:[color=darkred]
>> > paytam@gmail.co m wrote:
>> >>
>> >> 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.[/color]
>>
>> This has to be one of the most imbecelic (and wrong) replies ever
>> seen in c.l.c,[/color]
>
> 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;
> }
>
> -------------
>[/color]
(Should be "#define getchar mygetchar" without the parentheses).
It's well-defined, because after your #define, all instances of getchar
will be replaced with mygetchar. Since there ins't anything called
'mygetchar' in stdio.h, there won't be any name conflicts at all. The
preprocessor will wash away the text 'getchar', meaning no conflicts
there, either.
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.
--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Andrew Poelstra wrote:[color=blue]
> On 2006-06-23, goose <ruse@webmail.c o.za> wrote:[/color]
<snipped>
[color=blue][color=green]
> >
> > #define getchar() mygetchar()
> >
> > int main (void)
> > {
> > char c = getchar ();
> > printf ("got char '%c'\n", c);
> > return 0;
> > }
> >
> > -------------
> >[/color]
>
> (Should be "#define getchar mygetchar" without the parentheses).
>[/color]
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.
<snipped>
[color=blue]
> Whether it's a wise thing to do is questionable.[/color]
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.
goose wrote:[color=blue]
>[/color]
.... snip ...[color=blue]
>
> 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;
> }[/color]
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.
--
Chuck F (cbfalconer@yah oo.com) (cbfalconer@mai neline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE maineline address!
CBFalconer wrote:[color=blue]
> goose wrote:[color=green]
> >[/color]
> ... snip ...[color=green]
> >
> > 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;
> > }[/color]
>
> Strictly speaking it is UB. redefining entries in the standard
> library that have been referenced via the appropriate headers is
> not allowed.
>[/color]
Hm? getchar (the #define) and getchar (the function prototype)
don't necessarily step on each others toes, right? Or does
"redefining " above mean "cannot reuse any symbols declared
in the standard headers whether said symbols are prototype,
#defines or typedefs?
[color=blue]
> In practice, this particular one MAY work. No guarantees.[/color]
Never is with UB.
[color=blue]
>
> I take it back. It will work, because you never use the define.
> Now think about why I said that.
>[/color]
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()).
goose wrote:[color=blue]
> CBFalconer wrote:
>[/color]
.... snip ...[color=blue][color=green]
>>
>> I take it back. It will work, because you never use the define.
>> Now think about why I said that.[/color]
>
> 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()).[/color]
You defined a functional macro with getchar(). You never used it,
because the subsequent occurance of getchar was followed by a
blank.
--
Chuck F (cbfalconer@yah oo.com) (cbfalconer@mai neline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE maineline address!
CBFalconer <cbfalconer@yah oo.com> writes:[color=blue]
> goose wrote:[color=green]
>> CBFalconer wrote:
>>[/color]
> ... snip ...[color=green][color=darkred]
>>>
>>> I take it back. It will work, because you never use the define.
>>> Now think about why I said that.[/color]
>>
>> 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()).[/color]
>
> You defined a functional macro with getchar(). You never used it,
> because the subsequent occurance of getchar was followed by a
> blank.[/color]
Nope.
For a function-like macro, the left parenthesis must immediately
follow the macro name (with no intervening whitespace) *in the macro
definition*. In an invocation, the left parenthesis merely has to be
the next preprocessing token.
The whole point of a function-like macro is that it can be invoked as
if it were an actual function. Disallowing whitespace in an
invocation would have broken that.
--
Keith Thompson (The_Other_Keit h) kst-u@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.
Comment