C语言 sscanf 双打

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2706789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:16:56  来源:igfitidea点击:

sscanf for doubles

cscanf

提问by bobobobo

This is a simple problem, but I can't see it:

这是一个简单的问题,但我看不到:

  char *s = "f 8.649292" ;
  double d ;
  sscanf( s, "f %f", &d ) ;

  printf( "d is %f\n", d ) ;

Why is dnot containing the double value 8.649292?

为什么d不包含双精度值8.649292

回答by bobobobo

Oh wait, nevermind. d needs to be a float.

哦等等,没关系。d 需要是一个float.

And to make it work you could use %lffor a double

为了使它工作,你可以使用%lf双倍

  char *s = "f 8.649292 " ;
  double d ;
  sscanf( s, "f %lf", &d ) ;

  printf( "d is %lf\n", d ) ;