user3579269
user3579269

Reputation: 23

Output not match the code in c (double variable)

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?

Upvotes: 1

Views: 145

Answers (1)

cxw
cxw

Reputation: 17041

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.)

Upvotes: 1

Related Questions