int main (int argc, const char * arg...">

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 a very simple program to explain my question: the output of this program:

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

so .. my question is: 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 the like :)

I am transferring these values ​​via Bluetooth, but it is my problem to send positions that are in the integer and then show it, it works, but I have to check what it is, so is there any lesson about @ "% d" and similar staff? when using @ "% d" and when using @ "% g" .. and are there any other @ "% something"? thanks

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

+7
source share
1 answer

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

For a complete list of string formatting specifiers, see the documentation .

+14
source

All Articles