scanf(), ungetc() behaviour.

K

Keith Thompson

Arndt Jonasson said:
I prefer using 'return' from main, for about the only reason that I
may otherwise get compilation warnings telling me that the code
between the call to 'exit' and the end of the function is not reached.

Why would you have code between a call to exit and the end of the
function?
There is a preprocessor symbol defined by the standard for indicating
successful exit: EXIT_SUCCESS (and one for failure - EXIT_FAILURE). It
is usually 0, but I suppose it must be something else in some environment.

And of course 0 is also a valid and portable exit status.
 
W

Walter Roberson

Why would you have code between a call to exit and the end of the
function?

I have observed compilers sometimes complain that the implicit return
of a function was not reachable when the function ended with exit()...
even in some very straight forward cases.

I haven't observed it particularily recently; it's a QOI issue that
has probably generally improved over time.
 
C

CBFalconer

Richard said:
Argento said:
.... snip ...

Nevertheless, it is incorrect. And now you *know* it's incorrect.

I know for certain that at least one regular contributor to this
group, and possibly more than one, has got into the habit of
completely ignoring any "plea-for-help" article in which the
questioner has used void main, on the grounds that they can't be
all that serious about C if they can't even get the entry point
right.

Well, I have taken to cutting things back to the first few glaring
errors, such as #include <conio.h> or void main(), and ignoring the
rest (assuming I decide to answer at all). After all, one needs to
fix the earliest errors first, all else may well be spurious. This
also reduces the bandwidth wastage.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
C

CBFalconer

Nick said:
Argento wrote:
.... snip ...

they are wrong. If your book uses void main() and fflush(stdout) then
destroy it. DO NOT give it to a charity shop as some other poor sucker
will mistake it for a C book. If you want a good book try
http://cm.bell-labs.com/cm/cs/cbook/

Harrumph. Book burning for fflush(stdout) would be unwise, not to
mention profligate. :)

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
C

CBFalconer

.... snip about usage of ungetc ...
Not portable? Why not?

Please don't snip attributions for material you quote. Please DO
snip material not germane to your reply, and attributions for
material totally snipped.
From N869:

7.19.5.2 The fflush function

Synopsis

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

Description

[#2] If stream points to an output stream or an update
stream in which the most recent operation was not input, the
fflush function causes any unwritten data for that stream to
be delivered to the host environment to be written to the
file; otherwise, the behavior is undefined.

[#3] If stream is a null pointer, the fflush function
performs this flushing action on all streams for which the
behavior is defined above.

Returns

[#4] The fflush function sets the error indicator for the
stream and returns EOF if a write error occurs, otherwise it
returns zero.

After ungetc has been called, the most recent operation on the
stream most definitely was not input. This allows a strict limit
on the buffer space that must be provided.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
C

CBFalconer

Argento said:
.... snip ...

I do know about this, what i meant was alternative to fflush(stdin).
I am a beginner who doesn't have much experience with stream, I/O
and memory, so i always run away from using fgets() and sscanf() in
cases like this one (just to test a function). I will put more hard
work on learning how to use these functions so that i won't make
the same mistakes again.

Consider the following function:

int flushln(FILE *f) {
int ch;

do {
ch = getc(f);
while (('\n' != ch) && (EOF != ch));
return ch;
}

Each call will eat up the remainder of a line. If the previous
line has already been eaten it will eat the next line, so you need
to keep track of when you absorb '\n's.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
C

CBFalconer

Argento said:
.... snip ...

FYI, unfortunately, when i check back on my university's course
unit just now. They do use
void main() and fflush( stdin ) :(

Publish the instructors email address here and we will collectively
set him/her right. It is not right to warp the minds of the
innocent.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
C

CBFalconer

John Tsiombikas (Nuclear / Mindlapse) said:
.... snip ...

No, both a return from main, and a call to exit results in closing
all the open files and flushing the repsective buffers. They are
identical in this respect.


That's what I do, but not out of fear that returning from main
will let them open. It's a matter of style, it simply looks
better, more symmetric if you will, to explicitly close what you
open.

The reason for doing it yourself is that you get to test for
closing errors, and then you can decide what, if any, remedial
steps to take. This may be crucial in avoiding data loss.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
R

Richard Heathfield

Nick Keighley said:
If your book uses void main() and fflush(stdout) then
destroy it.

(You mean fflush(stdin), of course.) Before deciding whether to burn the
book, however, please ensure that it is not merely using them as examples
of common errors to be avoided.
 
C

CBFalconer

Walter said:
.... snip ...

I have observed compilers sometimes complain that the implicit
return of a function was not reachable when the function ended
with exit()... even in some very straight forward cases.

Good for them. I consider that a sign of poor code structure.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
A

Argento

Well, I have taken to cutting things back to the first few glaring
errors, such as #include <conio.h> or void main(), and ignoring the
rest (assuming I decide to answer at all). After all, one needs to
fix the earliest errors first, all else may well be spurious. This
also reduces the bandwidth wastage.

After so a few replies to me, i have understand about the problem with
void main() but i do not have much idea about <conio.h> other then its
not in the standard. Is there any other problem related to it other then its
not in the stardard/less portable?
 
A

Argento

Publish the instructors email address here and we will collectively
set him/her right. It is not right to warp the minds of the
innocent.

Are you serious about this? Before you answer that first question, I
would just want you to know, those course books are from OUHK
(Open University of Hong Kong, that's where I currently study about this)
taken from the course code MT258.
 
A

Argento

(You mean fflush(stdin), of course.) Before deciding whether to burn the
book, however, please ensure that it is not merely using them as examples
of common errors to be avoided.
Definitely not. It's used in almost every-little-single code, lol.
 
A

Argento

The reason for doing it yourself is that you get to test for
closing errors, and then you can decide what, if any, remedial
steps to take. This may be crucial in avoiding data loss.
Yes, yes, error checking *-)
Because I am just at Unit 5 of the course, there ain't much error
checking idea in my mind yet. Thanks, very informative.
 
V

Vladimir S. Oka

Argento said:
After so a few replies to me, i have understand about the problem with
void main() but i do not have much idea about <conio.h> other then its
not in the standard. Is there any other problem related to it other
then its not in the stardard/less portable?

Please do not remove attribution lines when quoting. It's imporatnat to
know who said what, as well.

As for conio.h, there's nothing wrong with it as such (AFAIK). It's just
that it's implementation and system specific and thus off topic here.
If you have questions about it, they're better answered in one of the
Borland or MS-DOS groups (if my memory serves, but look it up
yourself).

The only possible thing "wrong" with conio.h is that as soon as it's in
your program, it (your program) becomes non-portable, which in these
parts is considered a Bad Thing.
 
V

Vladimir S. Oka

Argento said:
Yes, yes, error checking *-)
Because I am just at Unit 5 of the course, there ain't much error
checking idea in my mind yet. Thanks, very informative.

You're around 4 units late in starting error checking... ;-)

(don't snip attributions)
 
V

Vladimir S. Oka

Argento said:
Are you serious about this? Before you answer that first question, I
would just want you to know, those course books are from OUHK
(Open University of Hong Kong, that's where I currently study about
this) taken from the course code MT258.

In my experience, collective mind of c.l.c regulars trumps your (or any)
University or course books.

(attributions please)

--
BR, Vladimir

At a contest for farting in Butte
One lady's exertion was cute :
It won the diploma
For fetid aroma,
And three judges were felled by the brute.
 
A

Argento

Vladimir S. Oka said:
Please do not remove attribution lines when quoting. It's imporatnat to
know who said what, as well.

As for conio.h, there's nothing wrong with it as such (AFAIK). It's just
that it's implementation and system specific and thus off topic here.
If you have questions about it, they're better answered in one of the
Borland or MS-DOS groups (if my memory serves, but look it up
yourself).

The only possible thing "wrong" with conio.h is that as soon as it's in
your program, it (your program) becomes non-portable, which in these
parts is considered a Bad Thing.

Thanks. I had been reading this newsgroup like a month and only starting
from
yesterday, do i post. Sorry for removing the attribution lines and snip
thing, I use
to roam about in the local newsgroups (Hong Kong, that's where I live), they
have
different manners. They do top-posting and quote the whole thing. I guess I
got
lots to learn from all of you other then C questions.
 
V

Vladimir S. Oka

Argento said:
Thanks. I had been reading this newsgroup like a month and only
starting from
yesterday, do i post. Sorry for removing the attribution lines and
snip thing, I use
to roam about in the local newsgroups (Hong Kong, that's where I
live), they have
different manners. They do top-posting and quote the whole thing. I
guess I got
lots to learn from all of you other then C questions.

Every group has it's own customs. In brief, here you don't top-post,
don't remove attributions, and try to snip stuff that's not relevant,
while leaving enough context. The last one may be difficult, so not
doing it is normally not commented upon (unless you quote a whole 100+
line post with little benefit). Also, your replies should be
interspersed with the quoted text if it makes it clearer to what part
of the post you're replying to. There are also rules on topicality, but
these are better learned by lurking.

Anyway, thanks for taking an interest in learning local customs.

PS
Asbestos underware may also come in handy. ;-)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top