简体   繁体   中英

Floating-point numbers in C

I'm using the following code to get the output up to 5 decimal characters of any number input by user when divided by 1, I have to typecast it with (float) .

Can any one tell me how this can be done without typecasting or using float constant?

int main() {
    int n;
    scanf("%d",&n);
    printf("%.5 ", 1/(float)n);
    return 0;
}

您可以使用仅使用整数的这段代码

 printf(n==1?"1.00000":"0.%05d ", 100000/n);

Taking your question strictly literally, you could do:

int main() {
    float n;
    scanf("%f",&n);
    printf("%.5f", 1/n);
    return 0;
}

In this code, there is no float literal and no (float) cast.

从技术上讲,这应该做到:

printf("%.5f ", 1/pow(n,1));

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