OK, I was just looking in the book and saw this: int getchar(void); in stdio.h.... now, doesn't getchar(); return a char, not an int?
Where can I find the general description for most library functions like this?
[edit]'tupid spelling errors =)
Printable View
OK, I was just looking in the book and saw this: int getchar(void); in stdio.h.... now, doesn't getchar(); return a char, not an int?
Where can I find the general description for most library functions like this?
[edit]'tupid spelling errors =)
getchar() returns an int, the int's value is the ASCII character number.
char c;
c = (char) getchar();
No. It returns an int. Actually all of the 'char' related functions, such as 'fgetc', 'fputc', getchar, etc, all take and return integers rather than characters.
The reason they do this is so you can test them for non-char values, such as EOF.
One of the best books I've found is the "Microsoft C Bible", an older book, but it has supurb examples and information on every function.
This is part of the reason I like their MSDN so much. (I prefer the disk-based MSDN myself, but the online version is still nice.)
Other than that, there are many resources (available at your favourite search engine); Here is one such resource.
Quzah.
Is this casting necessary, or isQuote:
Originally posted by kwigibo
char c;
c = (char) getchar(); [/B]
good enough? I'd assume the conversion would be done automatically. Also, is there a function that returns the ascii value for a char?Code:c = getchar();
Yes, the conversion is done automatically, but you'll loose significant bits. As already stated, the return is an int. It is so, because EOF is an int, normally with the value -1. This value is outside the range of a char, so by assigning the return to a char, you loose the ability to test for EOF.Quote:
Originally posted by dbaryl
Is this casting necessary, or isgood enough? I'd assume the conversion would be done automatically. Also, is there a function that returns the ascii value for a char?Code:c = getchar();
Therefore, the code should be:
Code:int c;
c = getchar();