Re: Elementary questions from a beginner
From: Joona I Palaste (palaste_at_cc.helsinki.fi)
Date: 03/01/04
- Next message: Joona I Palaste: "Re: Wierd output instead of 0s and 1s"
- Previous message: Sathyaish: "Wierd output instead of 0s and 1s"
- In reply to: Sathyaish: "Elementary questions from a beginner"
- Next in thread: Peter Pichler: "Re: Elementary questions from a beginner"
- Reply: Peter Pichler: "Re: Elementary questions from a beginner"
- Reply: John Cochran: "Re: Elementary questions from a beginner"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 1 Mar 2004 10:38:31 GMT
Sathyaish <VisualBasicLearner@yahoo.com> scribbled the following:
> Please forgive my nescience. I have worked extensively on Win32 but I
> am only familiar with C and C++. Lately, I have been practicing C from
> K&R. Here 're a few doubts I have written in the comments. I'd be
> grateful if someone could answer them.
> #include <stdio.h>
> void main()
Non-standard form of main(). Better would be int main(void).
> {
> /*This program reproduces on the screen whatever you write on your
> keyboard. TWICE.
> One time it writes the output by default (out of the program) and the
> other time it writes
> because the program is at work.
> So if yo write to the screen the following it appears as:
> Hello, this is a test program
> Hello, this is a test program
> The repitition occurs at the press of the Enter key.
> This is one thing I don't understand. Why is the output repeated at
> the press of the enter key?
It isn't. The first line isn't output, it's echo from your system
console, which C doesn't even know, or care about. The second line is
output from your program.
> Does getchar terminate
> reading at a newline? It doesn't seem so because after the newline it
> is still reading inside the while loop. It does
> not terminate the loop. It only puts char on the output device after
> every newline. Something I should expect from
> getline(). This is my doubt.
Your console (which is an OS concept and not a C one) buffers characters
until you press Enter. It then sends all of them to your C program,
which obediently displays them back to stdout, causing the second line
of text.
> Another doubt I have resolved by heuristic endeavour is the EOF
> character representation on the keyboard is Ctrl+Z.
> Why?
Because the makers of your OS thought it would be nice. It could be
any key combination they want.
> Another doubt has just creeped up. When I press Ctrl+C, it just
> terminates without warning. When I press Ctrl+Z, it
> asks, "Press any key to continue". Pressing any key thereafter
> terminates the program. Why is that so?
Your program is not causing this. Your compiler environment is. There is
no code in your program to display "Press any key to continue", so it
isn't doing it. Your compiler environment is noticing the program's end
and printing this "Press any key to continue" all on its own. Try
exiting your compiler and running your program straight from the command
line.
> I've also tried to print EOF. The value, it seems to me from the
> output is -1. K&R says EOF value is big enough to
> distinguish from char type range. As I see, -1 is not really *BIG
> ENOUGH* but an illegal value for a char type because
> normally char type is unsigned. But then I am reminded of the two's
> complement representation of numbers. Does the K&R
> book say it's *BIG ENOUGH* because of the binary representation of -1
> being all 1s, and therefore the largest possible
> value in the range?
EOF should be a value normally outside the range of (signed) char.
The C standard specifies EOF has to be a negative value. Because of
this, the return value of getchar() is int, not char, and you should
make note of this in your program.
The binary representation of -1 is not standardised. It could be all 1s,
but then it could be something different. So therefore the comment in
K&R is wrong, or at least confusing. The point is that EOF's value
must be outside the range of signed char.
> */
> int c;
> while ((c=getchar()) != EOF) putchar(c);
> printf("%d", EOF);
>
> //To emulate EOF, press Ctrl+Z
> }
-- /-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "'It can be easily shown that' means 'I saw a proof of this once (which I didn't understand) which I can no longer remember'." - A maths teacher
- Next message: Joona I Palaste: "Re: Wierd output instead of 0s and 1s"
- Previous message: Sathyaish: "Wierd output instead of 0s and 1s"
- In reply to: Sathyaish: "Elementary questions from a beginner"
- Next in thread: Peter Pichler: "Re: Elementary questions from a beginner"
- Reply: Peter Pichler: "Re: Elementary questions from a beginner"
- Reply: John Cochran: "Re: Elementary questions from a beginner"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|