简体   繁体   中英

scanf function in objective-c, float and double

I just a beginner in objective-C.

Below is a calculator of temperature.

I find a solution on the internet. The problem is the scanf.

At first, I set the f as a double, but program has problem.

So I change it to float.

May I ask what's going on on scanf function in objective-c?

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?

Thanks

import

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 . However be sure to check the return value from scanf() (or sscanf() ) to ensure it parsed the correct number of values:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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