Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > getc fgetc

Reply
Thread Tools

getc fgetc

 
 
Bill Cunningham
Guest
Posts: n/a
 
      08-04-2009
I wrote this little reader program I like. It's just for fun anyway but
what does it mean that getc can be implemented as a macro and be reused
while fgetc can't? The only difference I see in them is the f in front. Also
Where I used != in my code I tried using the ! and I was given an error.

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
int a;
FILE *fp;
if ((fp = fopen(argv[1], "r")) == NULL) {
fputs("fopen error\n", stderr);
exit(1);
}
while ((a = getc(fp)) != EOF)
putc(a, stdout);
return 0;
}

while((a = getc(fp)) !EOF)

I wrote the above and something was wrong.

Bill


 
Reply With Quote
 
 
 
 
Fred
Guest
Posts: n/a
 
      08-04-2009
On Aug 4, 10:13*am, "Bill Cunningham" <nos...@nspam.invalid> wrote:
> * * I wrote this little reader program I like. It's just for fun anyway but
> what does it mean that getc can be implemented as a macro and be reused
> while fgetc can't? The only difference I see in them is the f in front. Also
> Where I used != in my code I tried using the ! and I was given an error..
>


The standard says that getc *may* (at the implementation's
discression)
be implemented as a macro rather that as a function, whereas
fgetc MUST be implemented as a function.

If getc is implemented as a macro, it may reference its argument
more than once.
> #include <stdlib.h>
> #include <stdio.h>
>
> int main(int argc, char *argv[])
> {
> * * int a;
> * * FILE *fp;
> * * if ((fp = fopen(argv[1], "r")) == NULL) {
> * * * * fputs("fopen error\n", stderr);
> * * * * exit(1);
> * * }
> * * while ((a = getc(fp)) != EOF)
> * * * * putc(a, stdout);
> * * return 0;
>
> }
>
> while((a = getc(fp)) !EOF)
>
> I wrote the above and something was wrong.
>


Syntax error. What do you expect from:
(xxx) !EOF

--
Fred K
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      08-04-2009

"Fred" <> wrote in message
news:d493947d-fa69-4d27-a899-...

[snip]

Syntax error. What do you expect from:
(xxx) !EOF

I was thinking that ! is short for !=. But obviously I must not be using it
right.

Bill


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-04-2009
Fred <> writes:
> On Aug 4, 10:13*am, "Bill Cunningham" <nos...@nspam.invalid> wrote:
>> * * I wrote this little reader program I like. It's just for fun anyway but
>> what does it mean that getc can be implemented as a macro and be reused
>> while fgetc can't? The only difference I see in them is the f in front. Also
>> Where I used != in my code I tried using the ! and I was given an error.
>>

>
> The standard says that getc *may* (at the implementation's
> discression)
> be implemented as a macro rather that as a function, whereas
> fgetc MUST be implemented as a function.
>
> If getc is implemented as a macro, it may reference its argument
> more than once.


Not quite. Any library function may additionally be implemented
as a macro, as long as that macro meets certain restrictions,
including that it must evaluate each of its arguments exactly once
(C99 7.1.4p1). Those restrictions are relaxed for getc, so that it
it's implemented as a macro, it may evaluate its stream argument
more than once. (The stream argument rarely has side effects, so
this doesn't create any great inconvenience for programmers, but
relaxing the requirement can make getc substantially more efficient.)

[...]

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-04-2009
"Bill Cunningham" <> writes:
> I wrote this little reader program I like. It's just for fun anyway but
> what does it mean that getc can be implemented as a macro and be reused
> while fgetc can't? The only difference I see in them is the f in front. Also
> Where I used != in my code I tried using the ! and I was given an error.
>

[snip]
>
> while ((a = getc(fp)) != EOF)
> putc(a, stdout);

[snip]
>
> while((a = getc(fp)) !EOF)
>
> I wrote the above and something was wrong.


"while ((a = getc(fp)) != EOF)" is correct.
"while((a = getc(fp)) !EOF)" is wrong.
The compiler told you so.
What's the problem?

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-04-2009
"Bill Cunningham" <> writes:
> "Fred" <> wrote in message
> news:d493947d-fa69-4d27-a899-...
>
> [snip]
>
> Syntax error. What do you expect from:
> (xxx) !EOF
>
> I was thinking that ! is short for !=. But obviously I must not be using it
> right.


It's not.

For the umpteenth time, don't guess. Look up ! and != in whatever
reference you're using.

I think one of your problems is that you often guess at something
without realizing that you're guessing, which I suppose makes my
repeated "don't guess" advice not as useful as it might be. You need
to distinguish between things that you're sure of (and right about!)
and things that you're not sure of and need to check in some
reference. I'm not sure how to advise you to acquire that skill.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      08-05-2009
On Tue, 04 Aug 2009 11:47:33 -0700, Keith Thompson wrote:

>> The standard says that getc *may* (at the implementation's
>> discression)
>> be implemented as a macro rather that as a function, whereas
>> fgetc MUST be implemented as a function.
>>
>> If getc is implemented as a macro, it may reference its argument
>> more than once.

>
> Not quite. Any library function may additionally be implemented
> as a macro, as long as that macro meets certain restrictions,
> including that it must evaluate each of its arguments exactly once
> (C99 7.1.4p1). Those restrictions are relaxed for getc, so that it
> it's implemented as a macro, it may evaluate its stream argument
> more than once. (The stream argument rarely has side effects, so
> this doesn't create any great inconvenience for programmers, but
> relaxing the requirement can make getc substantially more efficient.)


One point which your post doesn't make entirely clear:

Any library function may *additionally* be implemented as a macro, but it
still has to be implemented as a function; i.e. you can take its address
and call it indirectly, and you can #undef the macro to expose the
function declaration.

OTOH, getc() may be implemented solely as a macro, with no function.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-05-2009
Nobody <> writes:
> On Tue, 04 Aug 2009 11:47:33 -0700, Keith Thompson wrote:
>
>>> The standard says that getc *may* (at the implementation's
>>> discression)
>>> be implemented as a macro rather that as a function, whereas
>>> fgetc MUST be implemented as a function.
>>>
>>> If getc is implemented as a macro, it may reference its argument
>>> more than once.

>>
>> Not quite. Any library function may additionally be implemented
>> as a macro, as long as that macro meets certain restrictions,
>> including that it must evaluate each of its arguments exactly once
>> (C99 7.1.4p1). Those restrictions are relaxed for getc, so that it
>> it's implemented as a macro, it may evaluate its stream argument
>> more than once. (The stream argument rarely has side effects, so
>> this doesn't create any great inconvenience for programmers, but
>> relaxing the requirement can make getc substantially more efficient.)

>
> One point which your post doesn't make entirely clear:
>
> Any library function may *additionally* be implemented as a macro, but it
> still has to be implemented as a function; i.e. you can take its address
> and call it indirectly, and you can #undef the macro to expose the
> function declaration.


Right. I thought that the word "additionally", which I did use, made
that sufficiently clear.

> OTOH, getc() may be implemented solely as a macro, with no function.


I don't think that's correct. C99 7.19.7.5p2 says:

The getc function is equivalent to fgetc, except that if it is
implemented as a macro, it may evaluate stream more than once, so
the argument should never be an expression with side effects.

The implementation is still required to implement getc as an actual
function.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getc() != fgetc() ?? arun C Programming 11 07-28-2012 03:29 PM
getc() != fgetc() ?? arun C Programming 3 06-05-2011 12:22 AM
std::istream slowness vs. std::fgetc Jason K C++ 6 05-12-2005 02:16 PM
getc() vs. fgetc() William L. Bahn C Programming 13 07-21-2004 04:16 AM
fgetc() past EOF =?iso-8859-1?q?Jos=E9_de_Paula?= C Programming 6 01-19-2004 09:03 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57