T don not understand the difference between
int c;
and
char c;
char is declared as signed int. ...
You're using the words wrong, so I'm not quite sure what you mean. char
is a built in type, it isn't declared anywhere. The above code declares
"c", not "char".
char is an integer type, and it can be signed. If it is signed, it could
in principle have exactly the same representation as signed int, though
this would be rather unusual, and would require CHAR_BIT>=16. However,
even if it had the same representation, it would be a distinct type from
signed int, a distinction that matters for the purpose of some
compatibility rules.
But when i use some function in <ctype.h>
such as
isprintf(c);
I presume you mean isprint() rather than isprintf()?
not
isprintf((unsigned char)c);
I did't find some difference.
Can you put up some examples,that tell the difference?
Did you check whether CHAR_MIN is actually negative on your machine? If
not, you'll never see a difference. The only way that there could be a
difference is if c is negative. Did you test with negative values? If
there is a difference, it might occur only in some locale other than the
"C" locale - did you test with other locales?
The key point is that the behavior of isprint(c) is undefined when c has
any negative value other than EOF. Undefined behavior includes the
possibility that the behavior of isprint(c) is exactly the same as
isprint((unsigned char)c). Therefore, while the behavior could be
different, it's not possible to write a program which is guaranteed to
show any difference.
Because the behavior is undefined, it could in principle be arbitrarily
bad. In principle, it could erase arbitrary files from your hard disk.
In practice, such things are unlikely; you're unlikely to see anything
worse than a segfault, and not necessarily even that. However, do you
really want to bother finding out what the actual behavior is? The
proper response to learning that a given construct has undefined
behavior is to stop using it; you shouldn't invest too much time trying
to find out what the actual behavior of that construct is.
Exception: behavior undefined by the standard may be defined by some
other document. If the behavior is defined by some other document, and
you need to get that behavior, there's nothing wrong with writing such
code and relying upon the guarantees made by the other document.
However, you must keep in mind that your code will be safe only on
systems where that other document's definition of the behavior applies.