I was curious at the start about how ungetc() returns the character to the
stream, so i did the following coding. Things work as expected except if I
change the scanf("%c",&j) to scanf("%d",&j). I don't understand how could
scanf() affect the content of i[0] and i[1]. Can someone tell me why?
#include <stdio.h>
#include <ctype.h>
void main()
{
char i[2], j;
printf("Please input a two digit number:");
i[0]= getchar();
i[1]= getchar();
ungetc(i[1],stdin);
ungetc(i[0],stdin);
scanf("%c", &j); //scanf("%d",&j);
fflush(stdin);
printf("\nYou entered %d in decimal and %c in character mode for
i[0]",i[0],i[0]);
printf("\nYou entered %d in decimal and %c in character mode for
i[1]",i[1],i[1]);
printf("\nYou entered %d in decimal and %c in character mode for j",j,j);
getchar();
}
Mar 3 '06
62 5060
On Sat, 04 Mar 2006 17:33:10 +0800, Argento wrote: 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.
I would not push this too far if I were you. I have had a look at some
of the TMA solutions and there are worse errors in there the void main()
and fflush(stdin)! One solution has some very odd logic (I can't say
"wrong" because the spec. is not given in the solution paper so it is
possible the strange behaviour is allowed). In one place in the
supporting material by the same author gets and scanf("%[\n]", ...) are
given as examples. In another, the example can overwrite an array.
Others use sentinel values which I don't think of a good practise.
Even good book can have errors, but these just look like bad teaching
examples to me.
--
Ben.
Argento wrote: 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.
Yes, I am serious. I don't know what we want to set him right
about, since you snipped that portion. However I vaguely remember
writing the above, although you snipped the attributions. Please
don't do that. I guess snipping is an acquired art.
We all have an interest in not having bad teachers go uncorrected.
They produce bad product, and make life harder for all of us
correcting those ex-students. Some of us will be more diplomatic
than others, and some will be outright rude.
I suspect if you, as a student, go to your instructor and insist
that he cease and desist from misinformation, it will adversely
affect your marks. However if peers tell him the same, usually
with bell, book, candle, and standards references in hand, s/he may
listen and no evil retribution will fall on you.
--
"If you want to post a followup via groups.google.c om, 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/googlegroupsrep ly/>
CBFalconer wrote: Argento wrote:
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.
Yes, I am serious. I don't know what we want to set him right about, since you snipped that portion. However I vaguely remember writing the above, although you snipped the attributions. Please don't do that. I guess snipping is an acquired art.
We all have an interest in not having bad teachers go uncorrected. They produce bad product, and make life harder for all of us correcting those ex-students. Some of us will be more diplomatic than others, and some will be outright rude.
I suspect if you, as a student, go to your instructor and insist that he cease and desist from misinformation, it will adversely affect your marks. However if peers tell him the same, usually with bell, book, candle, and standards references in hand, s/he may listen and no evil retribution will fall on you.
In my own experience, the young student who demands the instructor cease
and desist from anything at all, will lose big time. If student finds
instructor incompetent, student should change instructors. No student
has ever won the fight with the instructor, in my experience.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Richard Heathfield wrote: Argento said:
The C standard requires that main() be defined with a return type of int in a hosted environment, unless you have a C99 conforming compiler that specifically documents that it accepts "void main()", and I will most certainly bet that you do not. This means your program is undefined.
Well, sorry. I don't know much about the standard but i am using DEV C++ and the compiler compiles it happily.
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.
Is "void main" really that bad?
Does anyone have any statistics on the compilers that "void main" causes
problems with?
I suspect that such statistics would show that the use of "void main"
has similar ramifications to tearing off the tag of a bed mattress. If
anyone can prove me wrong, please back it up with some numbers.
--
jaysome
jaysome wrote: Richard Heathfield wrote: Argento said:
The C standard requires that main() be defined with a return type of int in a hosted environment, unless you have a C99 conforming compiler that specifically documents that it accepts "void main()", and I will most certainly bet that you do not. This means your program is undefined .
Well, sorry. I don't know much about the standard but i am using DEV C++ and the compiler compiles it happily.
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.
Is "void main" really that bad?
Does anyone have any statistics on the compilers that "void main" causes problems with?
I suspect that such statistics would show that the use of "void main" has similar ramifications to tearing off the tag of a bed mattress. If anyone can prove me wrong, please back it up with some numbers.
I don't think `void main()` will really do any nasty things to you.
However, most examples come from posters who are obviously new and
inexperienced, and I believe it signifies either bad teaching they've
been exposed to or not really caring about learning proper C. Something
along the lines of not capitalising `i`s and sentences, and using `u`,
`ur` and similar contractions.
Ask any of them about whether their particular implementation actually
allows `void main()`, which it can if it documents it, they wouldn't
have a clue what you're talking about. And of course, even if it did,
it wouldn't be portable, Standard C, and so would be off-topic here.
--
BR, Vladimir
Cleanliness becomes more important when godliness is unlikely.
-- P.J. O'Rourke
jaysome <ja*****@spamco p.com> writes: Richard Heathfield wrote: Argento said:The C standard requires that main() be defined with a return type of int in a hosted environment, unless you have a C99 conforming compiler that specifically documents that it accepts "void main()", and I will most certainly bet that you do not. This means your program is undefined .
Well, sorry. I don't know much about the standard but i am using DEV C++ and the compiler compiles it happily. 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.
Is "void main" really that bad?
Yes.
Does anyone have any statistics on the compilers that "void main" causes problems with?
I don't know, but that's not really the point.
I suspect that such statistics would show that the use of "void main" has similar ramifications to tearing off the tag of a bed mattress. If anyone can prove me wrong, please back it up with some numbers.
There is quite simply no reason to use "void main". Unless you happen
to be using an implementation that (unwisely, IMHO) documents the use
of "void main" as an extension, using it invokes undefined behavior.
Most likely it will work as expected; the worst *likely* problem is
that the program will return an unpredictable exit status to the
environment (which is a good enough reason in itself to avoid it).
But it also means that any time your program doesn't behave as you
expected, you can't be certain that your use of "void main" didn't
cause the problem.
Consistently using the *correct* form costs nothing, and it means you
always have one less thing to worry about.
"void main" *probably* won't cause any real problems, but unless
you're using random numbers, programming isn't about probabilities.
It's about writing correct code that does what you want it to do.
--
Keith Thompson (The_Other_Keit h) ks***@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.
jaysome said: Richard Heathfield wrote: ...they can't be all that serious about C if they can't even get the entry point right. Is "void main" really that bad?
Yes, for the reason I gave above.
It's called "cargo cult" programming. Setting aside freestanding
implementations for a moment, there is never a good reason to use void
main. So where does it come from? Not from thinking, that's for sure. No,
people use it because they've seen other people using it, without thinking
of the possibility that those they emulate may not be all that great at C
themselves. Computers copy stuff blindly about the place without
understanding it. People are supposed to be brighter than that.
Does anyone have any statistics on the compilers that "void main" causes problems with?
Does anyone have statistics on the number of programs that have failed
because of undefined behaviour?
I suspect that such statistics would show that the use of "void main" has similar ramifications to tearing off the tag of a bed mattress. If anyone can prove me wrong, please back it up with some numbers.
I suspect that statistics would show that those who don't care enough about
correctness to get the entry point right are also likely not to care enough
about getting other things right. So why bother trying to teach them? Just
let them guess and hope.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
On 2006-03-05, Richard Heathfield <in*****@invali d.invalid> wrote: jaysome said:
Richard Heathfield wrote: ...they can't be all that serious about C if they can't even get the entry point right.
Is "void main" really that bad?
Yes, for the reason I gave above.
It's called "cargo cult" programming. Setting aside freestanding implementations for a moment, there is never a good reason to use void main.
My entire career is freestanding implementations or things like vxWorks
where you just don't use main() at all.
Why?
Requirments for those types of things often state "ANSI (or ISO) C, with
no extensions," "No warnings from the compiler set at its most stringent
level," and "No unreachable code"
Defining main() void or with the wrong parameters violates the first
requirement (non-ANSI).
Ignoring the warning from main() not returning violates the Second
requirement (causes warnings).
Sticking a return; after a never exits loop violates the third
(unreachable code).
While there exist freestanding, as well as hosted, implementations where
it works, there's rather an enormous gulf between:
"This works on this version of the compiler running under this
processor on this OS cross-compiling to that processor under that OS"
and:
"This is Standard and supported by any conforming implementation. "
On Sun, 05 Mar 2006 02:07:41 -0500, Joe Wright wrote: No student has ever won the fight with the instructor, in my experience.
That may be true, but there should be no fight in the first place. Good
instructors know they can make mistakes and learn from them (rather more
students think they know better that actually do, but that should be no
excuse). To swing back almost OT, I once presented something rather like
this:
int order2(int *a, int *b)
{
if (*a > *b) {
int tmp = *a; *a = *b; *b = tmp;
return 1;
}
return 0;
}
int order4(int *a, int *b, int *c, int *d)
{
int swaps = 0;
while (order2(a, b) || order2(b, c) || order2(c, d))
swaps++;
return swaps;
}
and asked for an informal argument about the termination of the loop in
order4. It was not actually C, but no matter. Most were doing what I
wanted (informally showing that the loop terminates) and getting good
grades for that part until someone pointed out what will happen with
aliased pointers. Obvious enough, maybe, but I was thinking about other
matters when writing the assignment. The next year, the question was
better and asked for two arguments -- including one for when the pointers
might be aliased!
I would be wary of any teacher who does not learn from their students.
--
Ben.
In article <l8************ *************** ***@comcast.com >,
Charles Krug <cd****@aol.com > wrote: My entire career is freestanding implementations or things like vxWorks where you just don't use main() at all.
Of course, if you do not use a main() at all, there is no need to
concern yourself with the return-type and parameter(s) to main(). :-)
I actually prefer avoiding main() entirely in freestanding code,
for this reason.
(Also: although it is off topic, I will mention that as of 6.x, vxWorks
does use "main" functions in RTPs.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: clusardi2k |
last post by:
/*
The below code on SGI will wait for you to enter 2 things, but
on Linux it will only wait the first time.
I can make the code work by replacing the scanf with:
char data ;
fgets (data,5,stdin);
the_number = atoi (data);
|
by: Sivarn |
last post by:
I'm writing a program for that takes dates as input using scanf. I
want to verify that the user is inputting a full 4 digits for the
year. How do I do this? I know that the return value on printf is the
number of printed characters; so if I could somehow get my year
variable to store the leading zeros, I could just run a check:
int dummy = 0;
dummy = printf("%d", year);
|
by: TTroy |
last post by:
Hello C programmers,
Can someone tell me why ungetc can't sent back EOF, but it's sister
function getc has no trouble sending it to us? For a file, this might
not make a difference, but for an interactive terminal, it is probably
nice to push EOF back (because to user doesn't want to provide an EOF
twice).
How is it getc can send EOF down it's pipe, but we can't send EOF down
ungetc's pipe (especially when this pipe is the same)? ...
|
by: Rob Thorpe |
last post by:
Given the code:-
r = sscanf (s, "%lf", x);
What is the correct output if the string s is simply "-" ?
If "-" is considered the beginning of a number, that has been
cut-short then the correct output is that r = EOF. If it is taken to
be a letter in the stream, then the output should be r = 0, as far as
I can see. My compiler gives EOF.
|
by: Lalatendu Das |
last post by:
Dear friends,
I am getting a problem in the code while interacting with a nested
Do-while loop
It is skipping a scanf () function which it should not. I have written
the whole code below. Please help me in finding why such thing is
happening and what the remedy to it is. Kindly bear with my English.
int
main ()
| |
by: stasgold |
last post by:
Hello.
I maybe reinvent the weel ...
I'm trying to read positive integer number with the help of scanf, if
the input value is not positive number but negaive one zero or char , i
have to reread the input until I get the needed pos. number
I wrote the code , but it doesn't work the way it should : if i put
some char into input, the program goes infinite loop instead of
promting me to enter a new value.
|
by: Simon Biber |
last post by:
The following Example 3 is given in the 1999 C standard for the function
fscanf:
I have tested several implementations and none of them get the last case
right. In no case does fscanf return 0 indicating failure to match
"100ergs of energy" with "%f".
The actual behaviour varies. Some will match '100', leaving the 'e' unread:
|
by: Xavoux |
last post by:
Hello all...
I can't remind which function to use for safe inputs...
gets, fgets, scanf leads to buffer overflow...
i compiled that code with gcc version 2.95.2, on windows 2000
char tmp0 = "ABCDEFGHI\0";
char buff; /* Input buffer. */
char tmp1 = "ABCDEFGHI\0";
|
by: vid512 |
last post by:
hi.
i wanted to know why doesn't the scanf functions check for overflow
when reading number. For example scanf("%d" on 32bit machine considers
"1" and "4294967297" to be the same.
I tracked to code to where the conversion itself happens. Code in
scanfs just ignores return value from conversion procedures.
More info in case of glibc posted here:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |