S
Sathyaish
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()
{
/*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? 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.
Another doubt I have resolved by heuristic endeavour is the EOF
character representation on the keyboard is Ctrl+Z.
Why?
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?
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?
*/
int c;
while ((c=getchar()) != EOF) putchar(c);
printf("%d", EOF);
//To emulate EOF, press Ctrl+Z
}
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()
{
/*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? 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.
Another doubt I have resolved by heuristic endeavour is the EOF
character representation on the keyboard is Ctrl+Z.
Why?
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?
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?
*/
int c;
while ((c=getchar()) != EOF) putchar(c);
printf("%d", EOF);
//To emulate EOF, press Ctrl+Z
}