sscanf for double

bob*_*obo 9 c scanf

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

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

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

为什么d不包含双值8.649292

bob*_*obo 17

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

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

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

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

  • 使用`scanf`扫描双精度需要`%lf`,但使用'%lf`和`printf`是不正确的.只需使用`%f`即可. (3认同)