繁体   English   中英

target-c,float和double中的scanf函数

[英]scanf function in objective-c, float and double

我只是Objective-C的初学者。

下面是温度计算器。

我在互联网上找到了解决方案。 问题是scanf。

首先,我将f设置为双精度,但是程序有问题。

所以我将其更改为浮动。

请问Objective-C中的scanf函数发生了什么?

只能设置字符,整数和浮点数吗?

另一个问题是,如果我想设置一个double以便在仅接受double变量的另一个函数中使用?

谢谢

进口

int main(int argc,const char * argv []){

@autoreleasepool {
    double c;
    float f;
    NSLog(@"Please enter F temp");
    scanf("%f", &f);
    c = (f-32) / 1.8;
    //c = 1.3E-3;
    // insert code here...
    NSLog(@"The C temp is %.3f", c);

}
return 0;

}

使用%f表示float ,使用%lf表示double 但是,请务必检查scanf() (或sscanf() )的返回值,以确保它解析了正确的值数:

double d;
printf("Entry thy number mortal: ");
if (scanf("%lf", &d)) == 1) {
    printf("Oh that's nice, you entered %f\n", d);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM