-1

The following code:

int N = 100;
double total_time = 100;
double dt = total_time/N;
NSLog(@"Answer: %d", dt);

gives an interesting output: 5766 instead of 1.

Only in the case of adding

NSLog(@"Answer: %f", dt);

outputs the right answer. However, I would like to output "double" value instead of "float". What's wrong with my expectations and syntax?

Darius Miliauskas
  • 3,391
  • 4
  • 35
  • 53
  • You should already know basic C if you are attempting Objective-C, and one of the things you should know is the simple `printf` format scheme (as well as the difference between an integer and a float value). – Hot Licks Apr 07 '15 at 18:49
  • Do you mean that I shouldn't ask the questions about Objective-C if I do not know basic C so good? How do you define what is basic and what is not? For me, some questions look basic, for others - not. – Darius Miliauskas Apr 07 '15 at 18:55
  • If you don't know, eg, how to use `printf` to print floating point values, that's a good sign that you're not familiar enough with C to be starting into Objective-C. – Hot Licks Apr 07 '15 at 18:58

2 Answers2

1

%d means "decimal representation of int", not "double".

You always output a double value using %f, because NSLog() has an open arg list. In such a case, all floating point numbers are passed as doubles. So you should read %f as "floating point number" not float.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
1

%d and %i are the same thing, both decimal integer formats.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57