How to use ("% 10.2f", & d) in C I want the number to be whole

-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;
}
    
asked by anonymous 11.10.2018 / 01:09

2 answers

1

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

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

Then the f formatter, for floating-point values, as well as documentation indicates:

  

Decimal floating point, lowercase

However, if you print a int where the function expects a float will not display the number correctly because a float is not stored in the same way as a int in relation to the bits. p>

If you want to still print the integer as if it were a float you can do a conversion to float at the time of printing that already gives you the result you would expect:

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

Still, if you did all integer operations the result will always be an integer, so the decimal part will always be .00

Recommended reading:

What is the meaning of the & amp; & amp; ; "(And commercial) in C language?

    
11.10.2018 / 01:32
0

Instead of using this, printf("%10.2f",&d); places printf("%i",d); as

You have already defined above that "d" is integer.

    
15.10.2018 / 15:41