On 26.11.2015 17:29, Neels Hofmeyr wrote:
On Wed, Nov 25, 2015 at 08:53:45PM +0100, Holger
Freyther wrote:
+
(unsigned int)nrm->orig, (unsigned int)nrm->repl);
What is the explicit casting about?
To print values 80000000..ffffffff as positive numbers, the printf format
is using '%u'. Casting to unsigned int it is my preferred way of making
sure the argument matches the printf format...
Might not be necessary, but erases all doubt about intention or type
sizes.
As far as I understand the C99 specification, this not necessary. IMO
the make the code less readable and the hinder the compiler and tools
like coverity to find certain bugs, e.g. if someone raised the type of
nmr->orig to 'long long' for some reason, he/she wouldn't get a warning
from the tools.
Here is the definition of "integer promotions":
§6.3.1.1 (2): "If an int can represent all values of the original
type, the value is converted to an int; otherwise, it is converted to an
unsigned int. These are called the integer promotions. All other types
are unchanged by the integer promotions."
Here is the definition of "default argument promotions":
§6.5.2.2 (6): "If the expression that denotes the called function has
a type that does not include a prototype, the integer promotions are
performed on each argument, and arguments that have type float are
promoted to double. These are called the default argument promotions. ..."
And finally, these have to be applied to the trailing arguments or printf:
§6.5.2.2 (7): "... The ellipsis notation in a function prototype
declarator causes argument type conversion to stop after the last
declared parameter. The default argument promotions are performed on
trailing arguments."
Jacob
--
- Jacob Erlbeck <jerlbeck(a)sysmocom.de>
http://www.sysmocom.de/
=======================================================================
* sysmocom - systems for mobile communications GmbH
* Alt-Moabit 93
* 10559 Berlin, Germany
* Sitz / Registered office: Berlin, HRB 134158 B
* Geschaeftsfuehrer / Managing Directors: Holger Freyther, Harald Welte