[英]scanf function in objective-c, float and double
I just a beginner in objective-C. 我只是Objective-C的初学者。
Below is a calculator of temperature. 下面是温度计算器。
I find a solution on the internet. 我在互联网上找到了解决方案。 The problem is the scanf. 问题是scanf。
At first, I set the f as a double, but program has problem. 首先,我将f设置为双精度,但是程序有问题。
So I change it to float. 所以我将其更改为浮动。
May I ask what's going on on scanf function in objective-c? 请问Objective-C中的scanf函数发生了什么?
Only can set character, int and float? 只能设置字符,整数和浮点数吗?
Another question is, what if I want to set a double, to use in another function which only accept double variable? 另一个问题是,如果我想设置一个double以便在仅接受double变量的另一个函数中使用?
Thanks 谢谢
int main(int argc, const char * argv[]) { 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;
} }
Use %f
for float
and %lf
for double
. 使用%f
表示float
,使用%lf
表示double
。 However be sure to check the return value from scanf()
(or sscanf()
) to ensure it parsed the correct number of values: 但是,请务必检查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.