• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: short question but I don't know how to describe it
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: short question but I don't know how to describe it


  • Subject: Re: short question but I don't know how to describe it
  • From: Clark Cox <email@hidden>
  • Date: Mon, 8 Mar 2010 15:54:25 -0800

On Mon, Mar 8, 2010 at 2:16 PM, Ed Wynne <email@hidden> wrote:
>
> On Mar 8, 2010, at 4:55 PM, Nick Zitzmann wrote:
>>
>> On Mar 7, 2010, at 5:07 PM, Marx Bievor wrote:
>>
>>> I can substitute a String with %@ and an int with %d... like in return
>>> @"Hi I am %@ and %d years old", name, age;
>>> what is the right command to substitute a bool and a float? I cannot find
>>> any reference at apple's docs.
>>
>> You generally want to use %f for floats and doubles. An ObjC BOOL is a
>> signed 8-bit character type, so %hhd ought to work.
>
> A much better and future-proof (translation: more paranoid) strategy with
> printf-style format strings, as used by NSString, is to explicitly upcast
> integer parameters to known compatible types.

For types smaller than int, there is no need to explicitly cast; the
standard guarantees that such values are converted to int (or unsigned
int)  implicitly. In fact, the 'h' and 'hh' modifiers are completely
redundant and are ignored when passed to printf-like functions (their
only real use is in scanf).

Given:

char c = ...;

All of the following are identical:

printf("%hhd", c);
printf("%hd", c);
printf("%d", c);

--
Clark S. Cox III
email@hidden
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: short question but I don't know how to describe it
      • From: Greg Parker <email@hidden>
References: 
 >short question but I don't know how to describe it (From: Marx Bievor <email@hidden>)
 >Re: short question but I don't know how to describe it (From: Nick Zitzmann <email@hidden>)
 >Re: short question but I don't know how to describe it (From: Ed Wynne <email@hidden>)

  • Prev by Date: Re: Custom View in Toolbar
  • Next by Date: Objective-C Garbage Collection problems
  • Previous by thread: Re: short question but I don't know how to describe it
  • Next by thread: Re: short question but I don't know how to describe it
  • Index(es):
    • Date
    • Thread