#7131 - fsckedhack - Tue Jun 10, 2003 9:30 am
Just starting to learn the C programming language, and I have two questions that have immediately popped up after buying "The C Programming Language 2nd Edition" today...
1) First program...
This program works even when <stdio.h> is NOT being included. I had thought that the printf() function was part of that library, but I guess it's not? I'm compiling on Bloodshed's Dev-C++ compiler I got from sourceforge.net
2) Second question.
Executable file is found here. http://www.codechaos.com/trash/cntchar.exe
Now, when I enter Ctrl-Z on an empty line then hit enter, the program exits. But if I type in Ctrl-Z on the same line as some text, say I enter "123456^Z" then hit enter, the program seems to completely ignore that last ^Z. Why is that? Shouldn't the ^Z still be in the input stream, cause getchar() to return EOF, and end the program?
Some more behavior from this short program. entering "1234567890\n" then "^Z\n" I'll get 11 characters entered. the 10 digits and the carrage return at the end. If I enter "1234567890^Z\n^Z\n" I only get 10 characters entered. But, I can enter "1234567890^Z\n1234567890\n^Z\n" and the program returns 21 characters entered. Seems like ^Z cancels the carrage return when there's other characters on the line, and the program only recognizes the EOF when it's on a line by itself.
I know this is a long post, but I don't completely understand what's going on here, so any help would be appreciated! I want to understand exactly what's going on with everything I code.
-fsckedhack
1) First program...
Code: |
// #include <stdio.h>
main() { printf("Hello World!"); } |
This program works even when <stdio.h> is NOT being included. I had thought that the printf() function was part of that library, but I guess it's not? I'm compiling on Bloodshed's Dev-C++ compiler I got from sourceforge.net
2) Second question.
Code: |
#include <stdio.h>
main() { int c; int CharCount = 0; printf("This program will count the number of characters of whatever you type.\n"); printf("Please type stuff in now. Type Ctrl-Z to exit.\n"); while ((c = getchar()) != EOF) CharCount++; printf("You have typed %d characters.", CharCount); } |
Executable file is found here. http://www.codechaos.com/trash/cntchar.exe
Now, when I enter Ctrl-Z on an empty line then hit enter, the program exits. But if I type in Ctrl-Z on the same line as some text, say I enter "123456^Z" then hit enter, the program seems to completely ignore that last ^Z. Why is that? Shouldn't the ^Z still be in the input stream, cause getchar() to return EOF, and end the program?
Some more behavior from this short program. entering "1234567890\n" then "^Z\n" I'll get 11 characters entered. the 10 digits and the carrage return at the end. If I enter "1234567890^Z\n^Z\n" I only get 10 characters entered. But, I can enter "1234567890^Z\n1234567890\n^Z\n" and the program returns 21 characters entered. Seems like ^Z cancels the carrage return when there's other characters on the line, and the program only recognizes the EOF when it's on a line by itself.
I know this is a long post, but I don't completely understand what's going on here, so any help would be appreciated! I want to understand exactly what's going on with everything I code.
-fsckedhack