Jump to content

Why printf("%d",first) where first = strtod(operation, &afterFirst) always returns 0?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
szmitek

szmitek

    Newbie

  • Members
  • Pip
  • 2 posts
Why this printf("%d",first) always returns 0 in my program ©:

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

    char operation[25], *afterFirst;

    double first;

    scanf("%25s", operation);

    first = strtod(operation, &afterFirst);

    printf("%d",first);

    return 0;

}

I would like to first = 4545,256 when I type eg. lkor4545,256+ 5. How to do this? Could you help me?

I compile program with Digital Mars 8.42 in MS Windows Server 2008. I am creating program to add, substrack, multiply and devide one number by another (simple calculator).

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Use %f instead of %d. Check for success. Input numbers in the form that strtod expects.

http://forum.codecal....html#post76184
http://forum.codecal...-numbers-c.html

Edited by dcs, 26 October 2008 - 04:50 PM.


#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I can see one major problem with it: %d in your printf is the code for an integer, not a double. Try %f.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
szmitek

szmitek

    Newbie

  • Members
  • Pip
  • 2 posts
Thank you.

printf("%f",first);
instead of
printf("%d",first);
works.