Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSLog(@"%d", a) NSLog(@"%g", a); difference between @"%d" and @"%g"

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...
    NSLog(@"Hello, World!");
    float a = 12.3454323;
    NSLog(@"%d", a);
    NSLog(@"%g", a);
    [pool drain];
    return 0;
}

hi, i made very simple program to explain my question: output of this program is:

1st line: some random number (394883904, or 89374e-15 or ...) 2line: 12.3454323

so.. my question: what is @"%d" and what is @"%g").. because, IF a is INTEGER (int a = 156)

then @"%d" gives 156 BUT @"%g" gives 8.32059e-315 or similar :)

i'm doing bluetooth transfer of these values, but this is my problem to send positions what are in integer and then to show it, it is working but i have to check what is what, so, is there any lesson about @"%d" and similar staff? when use @"%d" and when use @"%g".. and are there any other @"%something"? thank you

edit: of course, zero line is hello world! :)

like image 852
vogue Avatar asked Mar 08 '11 18:03

vogue


1 Answers

%d prints out an integer, %g prints out a float or double. If you give %d a float or double, or %g an integer, you will get incorrect results that may or may not be more or less equivalent to what you would get with *(int *)&floatVar or *(double *)&intVar.

The full list of string formatting specifiers is in the documentation.

like image 105
Anomie Avatar answered Dec 11 '22 11:12

Anomie