Khaled Barazi
Khaled Barazi

Reputation: 8741

Different treatment of %d in ObjectiveC versus iOS

1st question here. Reviewed FAQ but did not find answer.

I learned Obj C using Kochan book and he classifies %i for integers, %f for float, %d for double.

Using iOS, it seems that it is rather: %i and %d for integer and %f for float

Can someone confirm and maybe explain the rationale. I already looked through the string programming guide of Apple.

Thank you in advance.

Upvotes: 3

Views: 2771

Answers (1)

Tommy
Tommy

Reputation: 100632

The standard C/C++ syntax is followed. So %d and %i are both 'signed integer', %f is 'decimal floating point' and will show floats or doubles.

Without being sure why d and i are duplicated, the reason that %f does floats and doubles is that things passed via an ellipsis are subject to the C promotion rules. If you pass any integer type that is smaller than an int, it'll be promoted to a full size int for passing. Similarly, if you pass a float, it'll be promoted to a double. So the thing receiving the arguments just needs to think in terms of integer versus floating point, not about storage size.

Upvotes: 5

Related Questions