tags:

views:

59

answers:

3

When I compile the C codes (from Teach yourself C, 2nd edition-Herbert Schildt) written below in Borland C++ v5.02, I get a warning message as below: "c61.c(7,6): Conversion may lose significant digits"

What's wrong?

#include "stdio.h"

main()
{
 char ch;
 ch = getchar();
 printf(" You typed: %c", ch);

 return 0;
}

Same problem with another example:

#include "conio.h"
#include "stdio.h"

main()
{
 char ch;

 printf("Enter a character: ");
 ch = getche();
 printf("\nIts ASCII code is %d", ch);

 return 0;
}
+5  A: 

getchar() returns an int, so that it can return non-character values like EOF. So when you store the return value in a smaller datatype, like a char, you are potentially losing information.

Schildt's books are commonly considered the worst available for learning C (or C++) and this is a good example of why. You would be well advised to get a better book, such as K&R.

anon
yes, most of the example I've tested dont working.The third edition of this book claims it has been sold over 150,000 copies though.
Sharifhs
Gilles
Dear Neil, I regret that I don't really understand the explanation. I can't understand the term "return" you used here. What does mean by "getchar() returns an int"?
Sharifhs
@Sharifhs the book must be *really* bad if it doesn't cover the concept of return values of functions. In your code, you are storing the returned value in a char, but you need to store it in an int to get rid of the compiler warning.
anon
+1  A: 

The prototype for getchar is:

int getchar (void);

It returns an int because it needs to return EOF which is not a character.


Expanding on that, the function needs to be able to return any valid character, as well as being able to return a special "character" indicating that you're reached the end of file (or an error has occurred).

Since a char data type can only hold one of the characters, there would be no way to indicate this end of file if all the function returned was a char. That's because, if you selected one of those char values to represent end of file, you would not be able to tell the difference between the end of file and the actual character.

For example, let's choose 'A' to indicate end of file. When you actually get back an 'A' from getchar(), how will you know whether you've reached the end of file or whether the user actually entered 'A'?

To get around this, many functions that give you back a char will actually give you an int and use one of the integer values which don't have an actual char equivalent (such as -1).

In that case (assuming an 8-bit char), you would get back either a -1 if you've reached the end of the file or a value from 0 through 255 inclusive representing the character entered.


And you need to get yourself both a more modern book and a more modern compiler:

#include <stdio.h>
int main (void) {
    int ch = getchar();
    if (ch != EOF)
        printf("You typed: %c\n", ch);
    return 0;
}

Why anyone is still using Borland is this day and age (where gcc is both just as cheap and much better) is beyond me.

paxdiablo
dont working even now.....do you think it's an issue of my outdated compiler?
Sharifhs
Works fine for me with gcc. Keep in mind that you may be in cooked input mode. That means you may have to hit enter after your keypress.
paxdiablo
Thanks pabdialo, it works now!
Sharifhs
for EOF-Test better use the C89 feof(stdin) than manual test
@gordon: why? The only thing that will tell you is if you've _already_ hit end of file before entering `getchar()`. You'll _still_ have to check for EOF from `getchar()` itself since the end of file may happen while you're in there. And, if feof(stdin) is true, you'll _still_ get EOF out of `getchar()` either because of end of file or error trying to read past end of file. It seems you're introducing a check for no real benefit. I could be wrong however, it wouldn't be the first or last time :-)
paxdiablo
Dear paxdiablo, I regret that I don't really understand the explanation. What does mean by "returns a value/int" and EOF?
Sharifhs
@Sharifhs, see my update. Hopefully that explains it okay.
paxdiablo
paxdiablo, your answer was so valuable though it takes huge time to grasp for me. I kept reading those answers and just now your answer becomes clear to me. Thanks a million for your nice answer!
Sharifhs
A: 

What about this alternative program? It works fine.

#include <stdio.h>

main()
{
 char ch;

 getchar();

 printf("Enter the character: ");
 scanf("%c", &ch);
 printf(" You typed: %c\n", ch);
 return 0;
}

Please leave some comments.

Sharifhs