p 161...File access

M

mdh

Hi All,
There is a line in the discussion of K&R (section 7-5) that says ...
(end of 2nd paragraph, about the middle of the page)...in connection
with the description of getc and putc...that

"Like getchar and putchar, getc and putc may be macros instead of
functions"

Now, recently, I asked why va_list, va_arg, va_start etc were macros,
and the answers included these two insights from KT;

a function can't modify an argument.

 A C function cannot take 
a type name as an argument
<<<<<<<

Is the significance of this statement ( ie relating to putchar/
getchar) related to macros in va_list at all. And if not, what
significance/insight should I attribute to this statement?
Thanks as usual.
 
M

Martien Verbruggen

There is a line in the discussion of K&R (section 7-5) that says ...
(end of 2nd paragraph, about the middle of the page)...in connection
with the description of getc and putc...that

"Like getchar and putchar, getc and putc may be macros instead of
functions"

If they are macros, it's usually for efficiency reasons.

You need to KNOW if getc() and putc() might be macros for
reasons such as:
FILE *f = &array_of_open_files[0];

c = getc(f++);
might not do what you expect it to do because the first argument
of getc() might be evaluated more than once, causing unexpected
results. This is not an issue if getc() *MUST* be a function.

If a library function is implemented as a macro, it is not allowed to
evaluate its arguments more than once.

Excerpt from 7.1.4 p1:

Any invocation of a library function that is implemented as a macro shall
expand to code that evaluates each of its arguments exactly once, fully
protected by parentheses where necessary, so it is generally safe to use
arbitrary expressions as arguments.162)

Footnote 162 says: Such macros might not contain the sequence points that the
corresponding function calls do

(This is copied from WG14/N1256. The C99 standard itself has identical
wording, except that the footnote number is 156)

In other words, the code above is guaranteed to behave identical whether
getc() is a macro or a function.


Martien
 
B

Ben Pfaff

Martien Verbruggen said:
If a library function is implemented as a macro, it is not allowed to
evaluate its arguments more than once. [...]
In other words, the code above is guaranteed to behave identical whether
getc() is a macro or a function.

It is amazing that you went to all the trouble of quoting all
that without actually looking at the description of the getc
function:

7.19.7.5 The getc function
[...]
2 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.
 
K

Keith Thompson

Gordon:

The above (starting with "Now, recently") was written by mdh. He
quoted something I had written, and he attributed it to me ("insights
from KT"). You left *that* indirect attribution in place.

Nothing bad happened.

Thus endeth the lesson.
 
M

mdh

(e-mail address removed) (Gordon Burditt) writes:

[...]


Hi Keith,
I am still at a little bit of a loss why K&R specifically mentioned
this factoid. There may have been a lesson in the earlier answer, but
it evaded me.
 
M

Martien Verbruggen

Martien Verbruggen said:
If a library function is implemented as a macro, it is not allowed to
evaluate its arguments more than once. [...]
In other words, the code above is guaranteed to behave identical whether
getc() is a macro or a function.

It is amazing that you went to all the trouble of quoting all
that without actually looking at the description of the getc
function:

7.19.7.5 The getc function
[...]
2 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.

I never noticed that before. This seems to be limited to getc, putc,
getwc and putwc. Any others that are exceptions to this rule that I
missed?

Martien
 
K

Keith Thompson

mdh said:
Hi Keith,
I am still at a little bit of a loss why K&R specifically mentioned
this factoid. There may have been a lesson in the earlier answer, but
it evaded me.

I wasn't aware that K&R mentioned it (though it's possible they do).

*I* mentioned it in response to your question about why va_* are macros.
 
F

Flash Gordon

Gordon Burditt wrote, On 29/09/08 19:10:
Keith Thompson has on many occasions specifically told you that he does
not want his posts quoted without attribution. Since this has often been
done when replying to you I find it unbelievable that you are not aware
of his desire.
Something bad happened...

If that is bad, it is something bad happening in response to YOUR
failure to provide attributions.

However, as you are well aware Keith means that no one has sued him as a
result of posting that argument and it did not leed to complaints about
miss-attribution.

Attributions are bad because they provoke many of the "regulars"
into talking about the attributions and the people named in them
rather than talking about C. They call each other "trolls" and
trade insults.

It is not the attributions that cause that. After all, such posts are
generally in *direct* response to the person, *not* in response to a
post quoting that person.
Please don't read the attributions. And if you must read the
attributions, don't copy names from them into the body of the
article.

Please ignore Gordon on this matter. The "standard" on news groups is to
attribute quoted material to the poster.
The meaning of what was said doesn't change with who said
it.

No, but how likely it is to be reliable does depend on who posted it. It
also allows one to spot whether it is a debate between just two people
or one with multiple people which can be useful.

For the record, I explicitly deny permission to quote any of the
articles I have ever posted to any news group without attribution. I
also deny permission to quote any of my future articles without attribution.
 
K

Keith Thompson

The above was written by me said:
Are you sure "KT" isn't a reference to a Knowledge Tree web site?
Yes.


Something bad happened. Someone posted the article to which I am
replying, and talked about nothing but attributions, which are
entirely off-topic. Nowhere do any names of posters appear in the
C standard (with the possible exception of Dennis Ritchie).
Yes, this article is just as much an offender.

Attributions are bad because they provoke many of the "regulars"
into talking about the attributions and the people named in them
rather than talking about C. They call each other "trolls" and
trade insults.

Nonsense. Your deliberate snipping of attributions occasionally
provokes such discussions. If you left them alone, nothing bad would
happen, and this discussion would not be taking place.
Please don't read the attributions. And if you must read the
attributions, don't copy names from them into the body of the
article. The meaning of what was said doesn't change with who said
it.

What I write is judged in part by what I've written before; same for
everyone else here. If Steve Summit writes something, I check my
facts very carefully before publicly disagreeing. If certain other
posters write something, I check my facts very carefully before
agreeing. Furthermore, this newsgroup isn't just a series of factual
statements (or misstatements) made in a vacuum; it's a kind of
community. Knowing who said what makes it a more pleasant place and
makes it easier to follow the discussions.

I understand that you, Gordon Burditt, are not likely to change your
mind on this topic. I only brought it up again because you
(accidentally) left a partial attribution in place, rather than rudely
deleting deleting it. I don't intend to discuss it again, except for
my usual request when I happen to post a followup to something you
wrote.

I, Keith Thompson, wrote this. Permission to quote this, or anything
else I post to Usenet, without attribution is denied. Permission to
quote with proper attribution is granted, as always.
 
D

dj3vande

Gordon Burditt wrote, On 29/09/08 19:10:
[quoting somebody without attribution:]
Keith Thompson has on many occasions specifically told you that he does
not want his posts quoted without attribution. Since this has often been
done when replying to you I find it unbelievable that you are not aware
of his desire.

Perhaps it's time to start sending cease-and-desist letters?


dave
 
M

Martin Ambuhl

Gordon Burditt (Yes, that's whp wrote it; sue me Gordon for attributing
to said:
Are you sure "KT" isn't a reference to a Knowledge Tree web site?

Yes, and you are an idiot if think otherwise.
Something bad happened. Someone posted the article to which I am
replying, and talked about nothing but attributions, which are
entirely off-topic.

Something bad happened. You stole other people's words without
attribution. That is anti-social and dishonest.
Attributions are bad because they provoke many of the "regulars"
into talking about the attributions and the people named in them
rather than talking about C. They call each other "trolls" and
trade insults.

That is just incredibly stupid. Proper attribution does not lead to
discussion of attribution. Where do you get off telling such blatant lies?
Please don't read the attributions. And if you must read the
attributions, don't copy names from them into the body of the
article. The meaning of what was said doesn't change with who said
it.

This is not only stupid and dishonest, it violates long-standing usenet
policy. Do not pay any attention to Gordon Burditt: he is ignorant and
anti-social and is trying to entice others into his den of ignorance and
misbehavior.
 
K

Keith Thompson

Gordon Burditt wrote, On 29/09/08 19:10:
[quoting somebody without attribution:]
Keith Thompson has on many occasions specifically told you that he does
not want his posts quoted without attribution. Since this has often been
done when replying to you I find it unbelievable that you are not aware
of his desire.

Perhaps it's time to start sending cease-and-desist letters?

Nah. His use of my words without attribution is annoying, but I'm
under no obligation to do anything about it, and I choose not to in
this case. I doubt that bringing the legal system into this would be
either appropriate or constructive. This is about rudeness, not legal
liability, Gordon Burditt's absurd claims to the contrary
notwithstanding. I am Keith Thompson, replying to Flash Gordon's
reply to Gordon Burditt. Permission to quote this without attribution
is specifically denied.
 
J

jameskuyper

Gordon Burditt wrote:
....
Attributions are bad because they provoke many of the "regulars"
into talking about the attributions

Well, so does removing attributions.
... and the people named in them
rather than talking about C. They call each other "trolls" and
trade insults.

Have you seen any evidence to suggest that your refusal to attribute
things people said reduces the name calling? People can identify the
people they want to insult just from the style of the things that
other person says. Also, as far as I can tell, the desire to insult
someone here seems to stem primarily from the content of the message,
not the identity of the person who posted it.
Please don't read the attributions. And if you must read the
attributions, don't copy names from them into the body of the
article. The meaning of what was said doesn't change with who said
it.

When it contains first-person pronouns, the meaning most certainly
changes with the identity of the speaker.

Reputation is an important aspect of almost every culture, and that's
because a person's character can, quite legitimately, affect the right
way to think about what that person says; it can even determine
whether or not there's any point in spending time reading what that
person says.

Statements made by people who have a reputation for lying need to be
either ignored outright, or at least examined more carefully for lies
than those of people who have a reputation for honesty.

Statements by people who have a reputation for careless speech need to
be interpreted differently from those by people with a reputation for
speaking precisely - the same exact sentence can mean quite different
things when written by those two different kinds of people.

Statements by people who have a reputation of being experts in a given
area should be taken more seriously, when discussing that area, than
those by people who do not.

Attribution both enables the building of reputations in the first
place, and also enables the legitimate application of a person's
reputation to interpreting words written by that person.
 
C

CBFalconer

Gordon said:
mdh said:
There is a line in the discussion of K&R (section 7-5) that
says ... (end of 2nd paragraph, about the middle of the page)
...in connection with the description of getc and putc...that

"Like getchar and putchar, getc and putc may be macros instead
of functions"

If they are macros, it's usually for efficiency reasons.

You need to KNOW if getc() and putc() might be macros for
reasons such as:
FILE *f = &array_of_open_files[0];

c = getc(f++);
might not do what you expect it to do because the first argument
of getc() might be evaluated more than once, causing unexpected
results. This is not an issue if getc() *MUST* be a function.

I have reinstalled the attribution that you carelessly omitted.

You know all this. Just read the description of the function:

7.19.7.5 The getc function

Synopsis
[#1]
#include <stdio.h>
int getc(FILE *stream);

Description

[#2] 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.

Returns

[#3] The getc function returns the next character from the
input stream pointed to by stream. If the stream is at end-
of-file, the end-of-file indicator for the stream is set and
getc returns EOF. If a read error occurs, the error
indicator for the stream is set and getc returns EOF.

You have various ways of ensuring that getc does NOT call a macro,
including placing the call in parentheses. i.e.:

ch = (getc)(f++); /* perfectly legal */
 
C

CBFalconer

Martien said:
Gordon Burditt said:
mdh said:
There is a line in the discussion of K&R (section 7-5) that
says ... (end of 2nd paragraph, about the middle of the page)
...in connection with the description of getc and putc...that

"Like getchar and putchar, getc and putc may be macros instead
of functions"

If they are macros, it's usually for efficiency reasons.

You need to KNOW if getc() and putc() might be macros for
reasons such as:
FILE *f = &array_of_open_files[0];

c = getc(f++);
might not do what you expect it to do because the first argument
of getc() might be evaluated more than once, causing unexpected
results. This is not an issue if getc() *MUST* be a function.

If a library function is implemented as a macro, it is not allowed
to evaluate its arguments more than once.
.... snip ...

In other words, the code above is guaranteed to behave identical
whether getc() is a macro or a function.

No it isn't. The standard contains specific exemptions for getc
and putc.
 
V

vippstar

You have various ways of ensuring that getc does NOT call a macro,
including placing the call in parentheses. i.e.:

ch = (getc)(f++); /* perfectly legal */

While absolutely correct, whenever I'd want a non-macro getc because I
don't want the argument to be evaluated more than once, I'd just use
fgetc.
 
D

dj3vande

(e-mail address removed) writes:

Nah. His use of my words without attribution is annoying, but I'm
under no obligation to do anything about it, and I choose not to in
this case. I doubt that bringing the legal system into this would be
either appropriate or constructive. This is about rudeness, not legal
liability, Gordon Burditt's absurd claims to the contrary
notwithstanding.

Probably true.
I was thinking in terms of root causes; the root cause for his
reluctance to maintain attributions seems to be complaints and threats
of lawsuits that he claims to have received when quoting with
attribution, so perhaps if he were to get those results when quoting
without attribution as well, he'd have less reason to cut them out.
(Which would have a particularly elegant symmetry, even if it's not
worth actually doing.)

I am Keith Thompson, replying to

Dave Vandervies's reply to
Flash Gordon's
reply to Gordon Burditt.

An excellent, though probably not intentional, demonstration of how
much easier it is to let the computer handle it for you. (Which of
course only works if everybody else is willing to play nicely.)


dave

--
Dave Vandervies dj3vande at eskimo dot com
o I just hang-up and swore at them for a good 5 minutes.

The sequence of these actions could be improved upon.
--Bogdan Imandei and Rik Steenwinkel in the scary devil monastery
 
K

Keith Thompson

Probably true.
I was thinking in terms of root causes; the root cause for his
reluctance to maintain attributions seems to be complaints and threats
of lawsuits that he claims to have received when quoting with
attribution, so perhaps if he were to get those results when quoting
without attribution as well, he'd have less reason to cut them out.
(Which would have a particularly elegant symmetry, even if it's not
worth actually doing.)

Tempting, but way too much effort. Perhaps eventually Gordwon will
get tired of so many people complaining about his behavior and/or
notice that the only person here who has claimed to agree with him is
"Kenny McCormack".
Dave Vandervies's reply to


An excellent, though probably not intentional, demonstration of how
much easier it is to let the computer handle it for you. (Which of
course only works if everybody else is willing to play nicely.)

Whoops, sorry I left you out.

Note the demonstration that, in the case of an unintentional missing
or incorrect attribution, a brief apology is all that's called for --
and in the presence of such an apology, any legal action would, I
expect, be thrown out of court very quickly. IANAL.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top