As explained in detail by Sourav Ghosh, using formats that are inconsistent with the actual types passed is a potential problem. Yet for this particular case, on current PC architectures, this is not really a problem, as neither int
nor unsigned int
have trap representations.
You can scan a negative number with scanf("%u", &number);
. It will be negated in the destination type, namely unsigned int
, with the same bitwise representation as the negative number in a signed int
, for two's complement representation which is almost universal on current architectures.
scanf
converts %u
by matching an optionally signed decimal integer, whose format is the same as expected for the subject sequence of the strtoul
function with the value 10
for the base argument. The corresponding argument shall be a pointer to unsigned integer.
The strtol
, strtoll
, strtoul
, and strtoull
functions convert the initial portion of the string pointed to by nptr
to long int
, long long int
, unsigned long int
, and unsigned long long int
representation, respectively. First, they decompose the input string into three parts: an initial, possibly empty, sequence of white-space characters (as specified by the isspace
function), a subject sequence resembling an integer represented in some radix determined by the value of base, and a final string of one or more unrecognized characters, including the terminating null character of the input string. Then, they attempt to convert the subject sequence to an integer, and return the result.
If the value of base is between 2 and 36 (inclusive), the expected form of the subject sequence is a sequence of letters and digits representing an integer with the radix specified by base, optionally preceded by a plus or minus sign, but not including an integer suffix.
... If the subject sequence has the expected form and the value of base is between 2 and 36, it is used as the base for conversion, ascribing to each letter its value as given above. If the subject sequence begins with a minus sign, the value resulting from the conversion is negated (in the return type).
If the type of number
is unsigned int
, behavior is defined and the negative value is parsed and stored into number
using the unsigned negation semantics. Printing this value with printf("%d", number);
is at best implementation defined, but again, on current PC architectures, will print the negative number that was originally parsed by scanf("%u", &number);
Conclusion: although it seems harmless, it is very sloppy to use int
and unsigned int
interchangeably and to use the wrong formats in printf
and scanf
. As a matter of fact, mixing signed and unsigned types in expressions, esp. in comparisons is very error prone as the C semantics for such constructions are sometimes counter-intuitive.