How to use ("%10.2f",&d) in C I want the number to come out integer

Asked

Viewed 562 times

-1

#include <iostream>
#include <stdio.h>


int main() 
{
        int a,b,c,d;
        printf("Escolha Qual das equacoes se adequa a sua duvida de 1 a 3\n\n");
        printf("?+X=X     escolha (1) \nX+?=X     escolha (2)\nX+X=?     escolha (3)    \n");
        scanf("%i",&a);
        switch(a) {
            case 1:  printf("Apenas Digite os numeros que faltam sem os sinais no modelo ?+X=X \n ");
                     scanf("%i",&b);
                     scanf("%i",&c);
                     d=b-c;
                     printf("%10.2f",&d);

            break;


        }


    return 0;
}

2 answers

1

Instead of using like this, printf("%10.2f",&d);put it as followsprintf("%i",d);

because Voce already defined above that "d" is integer.

1

First you are printing the variable memory address when using the operator &:

printf("%10.2f", &d);
//               ^---

Then the formatter f, is for floating comma values as well as documentation indicates:

Decimal floating point, lowercase

A int where the function expects a float will not display the number correctly as a float is not stored in the same way as a int in relation to bits.

If you still want to print the whole thing out like a float can make a conversion to float at the moment of the impression that it already gives you the result you would expect:

printf("%10.2f", (float)d);

Still, if you have done operations all in integers the result will always be an integer, and so the decimal part will always be .00

Recommended reading:

What is the meaning of the operator "&" (and commercial) in the C language?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.