Why does this code print 0 when I pass a nonzero value?

The code is here:

#include <stdio.h>
    int main(void) {
    test(7.4, 4);
    return 0;
}

void test(float height, float radius){
printf("%f", height);

}

It will be printed:

0.000000

Why is this? Why doesn't it just print 7.4?

+4
source share
4 answers

test . , , C, . 7.4 4 , , , double int, , 7.4 double , double, - int.

, test, , float s, . , float, , , .

, test , . , :

nodecl.c: In function ‘main’:
nodecl.c:4:3: warning: implicit declaration of functiontest’ [-Wimplicit-function-declaration]
   test(7.4, 4);
   ^
nodecl.c: At top level:
nodecl.c:8:6: warning: conflicting types fortest
 void test(float height, float radius){
      ^
nodecl.c:4:3: note: previous implicit declaration of ‘test’ was here
   test(7.4, 4);
   ^

, , , , .

+7

Linux gcc , : 7.4 double, - , test main.

0000000000400506 <main>:
  400506:       55                      push   %rbp
  400507:       48 89 e5                mov    %rsp,%rbp
  40050a:       48 83 ec 10             sub    $0x10,%rsp
  40050e:       48 b8 9a 99 99 99 99    movabs $0x401d99999999999a,%rax
  400515:       99 1d 40 
  400518:       bf 04 00 00 00          mov    $0x4,%edi
  40051d:       48 89 45 f8             mov    %rax,-0x8(%rbp)
  400521:       f2 0f 10 45 f8          movsd  -0x8(%rbp),%xmm0
  400526:       b8 01 00 00 00          mov    $0x1,%eax
  40052b:       e8 07 00 00 00          callq  400537 <test>
  400530:       b8 00 00 00 00          mov    $0x0,%eax
  400535:       c9                      leaveq 
  400536:       c3                      retq  

, 0x401d99999999999a - 64- , .. 7.4. test (float, ), printf cvtss2sd ( ) xmm0:

0000000000400537 <test>:
  400537:       55                      push   %rbp
  400538:       48 89 e5                mov    %rsp,%rbp
  40053b:       48 83 ec 10             sub    $0x10,%rsp
  40053f:       f3 0f 11 45 fc          movss  %xmm0,-0x4(%rbp)
  400544:       f3 0f 11 4d f8          movss  %xmm1,-0x8(%rbp)
  400549:       f3 0f 5a 45 fc          cvtss2sd -0x4(%rbp),%xmm0
  40054e:       bf e4 05 40 00          mov    $0x4005e4,%edi
  400553:       b8 01 00 00 00          mov    $0x1,%eax
  400558:       e8 83 fe ff ff          callq  4003e0 <printf@plt>
  40055d:       c9                      leaveq 
  40055e:       c3                      retq   
  40055f:       90                      nop

, 64- stdout, .. 0x9999999a, 0.

+2

- , . test().

+1

GGC,

 float.c:11:6: warning: conflicting types for ‘test’ [enabled by default]
 void test(float height, float radius)
      ^
float.c:7:5: note: previous implicit declaration of ‘test’ was here
     test(7.4, 4);
     ^

Then I added a function prototype test()for the main () function, for example

void test(float, float);

Then compiled again, I got the correct output. So add a function prototype test().

0
source

Source: https://habr.com/ru/post/1656991/


All Articles