Using% f to print an integer variable

The output of the following program c: 0.000000 Is there any output logic or is it dependent on the response compiler or am I just getting the garbage value?

#include<stdio.h>

int main()
{
    int x=10;
    printf("%f", x);
    return 0;
}

PS: - I know that trying to print an integer value using% f is stupid. I just ask about it from a theoretical point of view.

+5
source share
6 answers

From the latest C11 project :

§7.16.1.1 / 2

...if type is not compatible with the type of the actual next argument 
(as promoted according to the default argument promotions), the behavior 
is undefined, except for the following cases:

— one type is a signed integer type, the other type is the corresponding 
unsigned integer type, and the value is representable in both types;
— one type is pointer to void and the other is a pointer to a character type.
+6
source

The most important thing to remember is that, as Chris points out, the behavior is undefined. If it were in a real program, the only reasonable task would be to fix the code.

, , , ( ).

printf "%f" double . 0.000000.

:

int x=10;
printf("%f", x);

, , :

  • int - 4
  • double - 8
  • int double printf , ,

, () int 10 4- , printf 8 double. 4 10 ( hex, 0x0000000a); 4 , , . 4-, 8- . ( - , , undefined.)

-, . , printf, int double, memcpy().

#include <stdio.h>
#include <string.h>

void print_hex(char *name, void *addr, size_t size) {
    unsigned char *buf = addr;
    printf("%s = ", name);
    for (int i = 0; i < size; i ++) {
        printf("%02x", buf[i]);
    }
    putchar('\n');
}

int main(void) {
    int i = 10;
    double x = 0.0;
    print_hex("i (set to 10)", &i, sizeof i);
    print_hex("x (set to 0.0)", &x, sizeof x);

    memcpy(&x, &i, sizeof (int));
    print_hex("x (copied from i)", &x, sizeof x);
    printf("x (%%f format) = %f\n", x);
    printf("x (%%g format) = %g\n", x);

    return 0;
}

x86:

i (set to 10) = 0a000000
x (set to 0.0) = 0000000000000000
x (copied from i) = 0a00000000000000
x (%f format) = 0.000000
x (%g format) = 4.94066e-323

, double ( ​​ IEEE ), , "%f" 0.000000.

, undefined, , " " . , . ; , .

, , ( ).

+3

10 :

00000000 00000000 00000000 00001010

printf IEEE 754.

( MSB LSB):

: 1
: 8
: 23

10 1010 , , , printf.

+1

.

.

chris :

printf: undefined, ( varargs, IIRC).

: , undefined , / , .

"undefined"?

:

int main()
{
    int i       = 10 ;
    void * pi   = &i ;
    double * pf = (double *) pi ; /* oranges are apples ! */
    double f    = *pf ;

    /* what is the value inside f ? */

    return 0;
}

, double (.. pf) , (.. i), undefined , , .

, !

, ( ), , , , int-:

typedef union
{
   char c[8] ; /* char is expected to be 1-byte wide     */
   double f ;  /* double is expected to be 8-bytes wide  */
   int i ;     /* int is expected to be 4-byte wide      */
} MyUnion ;

f i , c ( ) , .

void printMyUnion(MyUnion * p)
{
   printf("[%i %i %i %i %i %i %i %i]\n"
      , p->c[0], p->c[1], p->c[2], p->c[3], p->c[4], p->c[5], p->c[6], p->c[7]) ;
}

, .

:

int main()
{
   /* this will zero all the fields in the union */
   memset(myUnion.c, 0, 8 * sizeof(char)) ;
   printMyUnion(&myUnion) ; /* this should print only zeroes */
                            /* eg. [0 0 0 0 0 0 0 0] */

   memset(myUnion.c, 0, 8 * sizeof(char)) ;
   myUnion.i = 10 ;
   printMyUnion(&myUnion) ; /* the representation of the int 10 in the union */
                            /* eg. [10 0 0 0 0 0 0 0] */

   memset(myUnion.c, 0, 8 * sizeof(char)) ;
   myUnion.f = 10 ;
   printMyUnion(&myUnion) ; /* the representation of the double 10 in the union */
                            /* eg. [0 0 0 0 0 0 36 64] */

   memset(myUnion.c, 0, 8 * sizeof(char)) ;
   myUnion.f = 3.1415 ;
   printMyUnion(&myUnion) ; /* the representation of the double 3.1415 in the union */
                            /* eg. [111 18 -125 -64 -54 33 9 64] */

   return 0 ;
}

. Visual ++ 2010.

, , , , .

, , , .

, , , ( -).

printf , , , , int.

P.S.: , int double , , .

int double!

?

Helios .

int main()
{
   int x=10;
   printf("%f",(double)(x));
   return 0;
}

, , printf:

   /* printf("...", [[10 0 0 0]]) ; */
   printf("%i",x);

   /* printf("...", [[10 0 0 0 ?? ?? ?? ??]]) ; */
   printf("%f",x);

   /* printf("...", [[0 0 0 0 0 0 36 64]]) ; */
   printf("%f",(double)(x));

, "10" "10.0".

, "% i" - [[????????]], printf - [[10 0 0 0]] .

"% f" - [[???????????????]], printf - [[10 0 0 0]], 4 . , 4 (, "" [[10 0 0 0]], - [[10 0 0 0????]]

printf , , [[0 0 0 0 0 0 36 64]], printf double.

+1

this is mostly trash. Small integers look like abnormal floating point numbers that should not exist.

0
source

You can use the int variable as follows:

int i = 3;
printf("%f",(float)(i));
0
source

All Articles