¬a\/b said:
in the time i were sure now not;
it seems the compiler here cast the argument of printf
to int first than to call printf. is that behaviour for every function
or just for printf? is this behaviour just for chars or is for all
types?
When you pass an argument and the compiler does not know
the type of the corresponding parameter (that is, when you are
calling a function with no prototype or when you are passing
one of the "..." arguments to a variable-argument function),
the compiler "promotes" the argument by converting its value
to one of a standard set of types. For example, arguments of
type float are converted to double and passed to the function
as if you had provided double values to begin with.
"Narrow" integer types -- char, short, some others -- are
also promoted, but the promotion is a little bit tricky:
- If all possible values of the narrow type are within the
range of int, the value is promoted to an int and passed
as if you had provided an int argument.
- Otherwise, the value is promoted to an unsigned int and
passed as if you had provided an unsigned int argument.
So: *p is an unsigned char, which is a "narrow" integer
type. On most machines int has a wider range than char, so the
value of *p will be converted from char to int and passed to
printf() as an int. On a few unusual machines it is possible
that unsigned char can hold values that are numerically larger
than an int can hold, and on these machines *p will be converted
to unsigned int. (On these machines you will have sizeof(int)==1,
and since int is at least sixteen bits wide it follows that char
will be at least sixteen bits wide.)
in a machine like above should be
sizeof(void*)<=sizeof(unsigned char)
Since sizeof(unsigned char) is 1, `=' is possible but
`<' is not.
in the other case how is it possible "to printf" a pointer
if sizeof(char*)> sizeof(unsigned char) and cast the pointer
with (unsigned char)
I'm sorry: I do not understand your question.
this is more than i can understand
As explained above, there is some ambiguity about the
type of the value printf() will receive when you provide an
unsigned char argument: On most machines the argument will be
converted to int, but on unusual machines it may be converted
to unsigned int. This line removes the ambiguity by casting
the value to unsigned int: now printf() will receive an unsigned
int on all machines, typical and unusual. But "%d" is not the
right conversion specifier for unsigned int; "%d" works with an
ordinary int or signed int value. Since the value in this case
will be an unsigned int, I use "%u" instead of "%d".