1

I am new with c and I'm trying to use doubles variable. Unfortunately, the compiler (Code::Blocks) seems to ignore absolutely what i'm writing.

#include<stdio.h>
int main()
{
    double x=1.03;
    printf("x: %lf",x);
    return 0;
}

and the output is:

x: 0.000000

what is the problem?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • 2
    I don't see the problem. http://ideone.com/LUKwif. – R Sahu Nov 08 '15 at 22:10
  • 1
    Please do not post screenshots! You are expected to provide your question as text with proper formatting, unless its nature requires an image, of course. – too honest for this site Nov 08 '15 at 22:14
  • Got it. from now on i won't post screenshots. – user3579269 Nov 08 '15 at 22:17
  • I also can't reproduce this behaviour. I'm not sure what could be wrong. Perhaps it might help to let Code::Blocks remove and rebuild the program? Or reset its settings to the defaults? –  Nov 08 '15 at 22:18

1 Answers1

1

Use %f instead of %lf. Doubles only need %f; see the "specifiers" table here.

If printf is looking for a larger value than you are providing, what prints out will be affected by what happens to be in memory near the x argument that you provided. In this case, I'm guessing that's 0.

Edit: Thanks to @Olaf for pointing out that the specification says %lf should work just fine. Apparently the OP's compiler or compiler version is nonstandard. Alternatively, perhaps the project settings are selecting nonstandard compiler behaviour. (I suppose the compiler or library implementation of printf could be buggy, as well.)

cxw
  • 16,685
  • 2
  • 45
  • 81
  • `%lf` is fine for `double` arguments. Actually it is even a good idea to use it, as it is symmetric to `scanf`. – too honest for this site Nov 08 '15 at 22:14
  • Maybe for c99 %lf doesn't work? i work with c99 standard – user3579269 Nov 08 '15 at 22:18
  • `%lf` should work too, says [this answer](http://stackoverflow.com/a/4264154/824425). –  Nov 08 '15 at 22:19
  • @user3579269: It was **introduced** with C99 actually. – too honest for this site Nov 08 '15 at 22:20
  • @cxw: Please read in the standard: http://port70.net/~nsz/c/c11/n1570.html#7.21.6.1p7 – too honest for this site Nov 08 '15 at 22:20
  • 1
    now that I repalced the "%lf" to "%f" it is working. but i don't sure if %f give the same result as %lf is expected to give – user3579269 Nov 08 '15 at 22:22
  • @user3579269: So you are using a non-standard compliant compiler. – too honest for this site Nov 08 '15 at 22:23
  • yes sorry this is the first time i asked question on this site @Olaf – user3579269 Nov 08 '15 at 22:26
  • @cxw: Please have a look at the type-modifier table in the page you linked. That contradicts your answer, too. – too honest for this site Nov 08 '15 at 22:27
  • @Olaf I was looking at the "specifiers" table, in which the entry for `%lf` is blank. Where are you looking? I'm sorry to say I'm not seeing what you're pointing to. – cxw Nov 08 '15 at 22:30
  • @cxw: Hmm, you are right. Now I know why cppreference has such a bad reputation. It is actually wrong. See the link I provided. Or do you claim cppreference is the mandatory reference, not the official standard document? "l (ell) - Specifies that a following ... or has no effect on a following a, A, e, E, f, F, g, or G conversion specifier." – too honest for this site Nov 08 '15 at 22:33
  • @Olaf I claim nothing :) . I have no argument with the specification, but in this situation it appears the specification is not relevant since the OP has a non-compliant compiler. What I care about is that the OP is able to move forward with his program. I'm now going to clear out some of my previous comments so this doesn't look so much like a chat log ;) . Thanks for the spec link and clarification! – cxw Nov 08 '15 at 22:35
  • @cxw: Thanks for the edit. DV removed. But please change the Code::blocks reference. That is an IDE, not a compiler. We still don't know which compiler OP uses. But he might set an option for the compiler to make it compiant. – too honest for this site Nov 08 '15 at 22:38