U
Uno
Ian said:No, it isn't. getch() isn't a standard C function.
Ok.
int tty_getchar()
{
return getchar();
}
No (if you ignore the implicit int).
.... and ignore the screenshot:
http://i50.tinypic.com/28bxm43.png
Ian said:No, it isn't. getch() isn't a standard C function.
No (if you ignore the implicit int).
Uno said:You snipped out all the C and wondered where the C was.
Ok.
int tty_getchar()
{
return getchar();
I don't think it is misbehaving. The terminal is in cbreak mode so
it's going to echo the character returned by getch() but it's not
going to translate '\n' in the same way it does in cooked mode, thus
the screen walk. I suspect the writer of this FAQ item didn't test his
code. I have tested this on Fedora, OS X and Windows and it does the
screen walk in all systems. You have to output the carriage return
explicitly.
Just because it's written in C doesn't mean that comp.lang.c is a
good place to ask about it.
However, since these questions are frequently asked here, here are brief
answers for some common situations.
Ben said:No, I snipped the C because I was not commenting on it. I did not
complain about any lack of C, I just claimed that your questions would
be better answered in a Unix group. They are, largely, questions about
curses and man pages.
<snip more C>
Nobody said:To be fair to the OP, he is asking about the comp.lang.c FAQ.
Perhaps everything from:
onwards should be removed?
for(i = 0; i < 10; i++)
printf(" = %d\n", tty_getchar());
You snipped out all the C and wondered where the C was.
Why is main
junk in this standard C program:
int tty_break()
{
initscr();
*Yes.* There's gonna be a whole lot of stuff I don't understand in
there. So, I'd like to re-write these functions in a *much* simpler
form.
No, these functions I'm fixing to write won't have all the
functionality of what you would get by the header inclusion, but I do so
only to create scaffolding, and, yes, stay topical.
Ian Collins wrote:
The advertising says much more than this
and minimally needs a re-write.
why?
Invoking undefined behavior.
Part of this problem is best cast as standard C. The rest of it, I'll
discuss there.
Does anyone have any better notion of what to do with a main control in
such a program than using a retarded 'for' loop?
Wrong.
There's gonna be a whole lot of stuff I don't understand in
there. So, I'd like to re-write these functions in a *much* simpler
form.
No, these functions I'm fixing to write won't have all the
functionality of what you would get by the header inclusion, but I do so
only to create scaffolding, and, yes, stay topical.
int tty_getchar()
{
return getch();
}
This part is standard C,
Does main on faq 19.1 invoke undefined behavior?
Taking the question in its widest context, yes. That is, I can think of
one reason why one might not want to include headers in a C program.
Whether it's a *good* reason is another matter - I've heard opinions
both ways.
The context in which this reason is relevant is that of a C tutorial.
To the beginner, C has certain similarities to line noise. I still
remember trying to get a handle on C from a (ghastly) little yellow
paperback by Noel Kantaris, and the two things I struggled most with at
first were #include and #define. The rest seemed to make some kind of
sense, but those two just looked like gibberish.
A case can therefore be made for omitting headers from the early stages
of a tutorial work. One would begin by explaining main():
int main(void)
{
return 0;
}
which, of course, needs no headers anyway. One would then move on
immediately to explaining functions:
int foo(void)
{
return 6;
}
int main(void)
{
foo();
return 0;
}
(Obviously one would explain that, *for now*, we are not bothering to
use the return value of foo. Etc, etc, etc. I'm handwaving over a lot of
stuff here, because the only alternative would be to write the tutorial
right here!)
The author would no doubt stress that the control flow still starts at
main(), and that foo() is called at the appropriate time.
We're going to want to use a standard library function real soon now,
but first one would point out that the order of foo() and main() can be
changed around, *provided* that the compiler is granted a quick peek at
the nature of foo(), like this:
int foo(void);
int main(void)
etc etc.
A quick chat about prototypes would follow, and then the author would
mention standard library functions. Before long, example programs would
begin to look something like this:
int getchar(int);
int putchar(int);
int main(void)
{
int ch;
while((ch = getchar()) > 0) /* yeah, I know... */
{
putchar(ch);
}
return 0;
}
and would get progressively more complicated. At some point, one would
then say (on behalf of the reader): "but wait a minute - all this
'furniture' at the top of our programs is pretty much the same every
time; isn't there some way to say to the compiler 'just take it as read
that I've got all this standard stuff in the program'?"
And at that point, it becomes natural to introduce #include.
Uno said:But I'm asking you to ignore the extension and help me wrap my head
around a proper main control.
Nick Keighley said:On 28 June, 00:17, Uno <[email protected]> wrote:
[as re-posted by Nick:]the supposedly broken main()
why?
There is a part of this problem that directly bears on c.l.c., namely
that one of the faqs needs to be fixed up to be useful.
is freaking retarded, and judging by my machine's reaction, invoking
undefined behavior.
Ahem. See my reply elsethread. If you still think it's wrong, please
explain why.
Seebs said:No, it doesn't.
If we viewed "Uno can't understand it" as a crippling flaw in a document,
there would be no documents. All I ask is that you pick an alias/email
address and stick with it so you stay killfiled. (I would have asked that
you pay more attention and think before posting, but I am willing to limit
myself to the possible.)
There aren't too many things I find more boring than soccer
Richard said:Taking the question in its widest context, yes. That is, I can think of
one reason why one might not want to include headers in a C program.
Whether it's a *good* reason is another matter - I've heard opinions
both ways.
The context in which this reason is relevant is that of a C tutorial.
To the beginner, C has certain similarities to line noise. I still
remember trying to get a handle on C from a (ghastly) little yellow
paperback by Noel Kantaris, and the two things I struggled most with at
first were #include and #define. The rest seemed to make some kind of
sense, but those two just looked like gibberish.
A case can therefore be made for omitting headers from the early stages
of a tutorial work. One would begin by explaining main():
int main(void)
{
return 0;
}
which, of course, needs no headers anyway. One would then move on
immediately to explaining functions:
int foo(void)
{
return 6;
}
int main(void)
{
foo();
return 0;
}
(Obviously one would explain that, *for now*, we are not bothering to
use the return value of foo. Etc, etc, etc. I'm handwaving over a lot of
stuff here, because the only alternative would be to write the tutorial
right here!)
The author would no doubt stress that the control flow still starts at
main(), and that foo() is called at the appropriate time.
We're going to want to use a standard library function real soon now,
but first one would point out that the order of foo() and main() can be
changed around, *provided* that the compiler is granted a quick peek at
the nature of foo(), like this:
int foo(void);
int main(void)
etc etc.
A quick chat about prototypes would follow, and then the author would
mention standard library functions. Before long, example programs would
begin to look something like this:
int getchar(int);
int putchar(int);
int main(void)
{
int ch;
while((ch = getchar()) > 0) /* yeah, I know... */
{
putchar(ch);
}
return 0;
}
and would get progressively more complicated. At some point, one would
then say (on behalf of the reader): "but wait a minute - all this
'furniture' at the top of our programs is pretty much the same every
time; isn't there some way to say to the compiler 'just take it as read
that I've got all this standard stuff in the program'?"
And at that point, it becomes natural to introduce #include.
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.