gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Two Questions

#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...

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

#7137 - Sweex - Tue Jun 10, 2003 12:00 pm

Before I answer your questions I would like to point out the following. This is a GameBoy Advance board, and although it is the C/C++ forum it is slightly expected that posts have something to do with the GBA. Also, you questions are very much newbie questions.

Perhaps you should seek help on another forum. Don't get me wrong, I'm not having a go at you. Just some advice, and probably people here will not be too excited answering your question. (Try flipcode.com or gamedev.net!)

Now for you questions: The first one. You are right. Normally #include <stdio.h> should be included in order to use printf. However, I am not familiar with the compiler you mentioned, and they might do things a little different.

Second question: I'm not all too sure CTRL-Z results in the ascii code defined in EOF (End Of *File*)... CtrlZ could be treated differently...

#7143 - sgeos - Tue Jun 10, 2003 4:03 pm

fsckedhack wrote:
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?


printf is part of the library. Have you ever opened stdio.h and looked at it? I've never used your compiler, but I'm 90% sure it only contains constants and other stuff need to safe-ly use the functions in that library. You can prototype the function yourself if you don't want to include the header. Don't do this though. Nothing stops you from protype-ing the function incorrectly.

fsckedhack wrote:
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?


Seems to work fine to me. Actually, I did not need to press enter. The program quits as soon as I press Ctrl-Z. Are you confusing Ctrl-Z with ^Z? The first is a control character with no ascii glyph of it's own. The second is two ascii characters that are completely independant from Ctrl-Z. Somebody just chose to use "^" followed by "Z" to represent Ctrl-Z because it doesn't have a glyph of it's own. Typinging ^Z is not equal to hitting Ctrl-Z.

-Brendan

#7145 - Quirky - Tue Jun 10, 2003 4:15 pm

My guess would be that a newline character followed by a ctrl-z is the "end of file" character in DOS. But as you don't have a command line on the GBA I wouldn't worry about it :-)

#7153 - tepples - Tue Jun 10, 2003 6:01 pm

fsckedhack wrote:
Code:
// #include <stdio.h>

main() {
  printf("Hello World!");
}


This program works even when <stdio.h> is NOT being included.

The library is libc, not <stdio.h>. The <stdio.h> just provides an explicit prototype for each I/O function.

You're using an implied prototype for printf(). If you've turned on warnings, you should get "Warning: implicit declaration of function `printf'"

ObTopic: And yes, printf() works on the GBA if you've installed something like AGBTTY and hooked it up to stdout. (How to actually hook up a given implementation of write() to a given FILE * in a given version of DevKit Advance is beyond my knowledge.)
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.