Why is scanf returning 0.000000 when it is supplied with a double?

久未见 提交于 2021-02-05 09:12:59

问题


I have the following assembly code (written for NASM on Linux):

; This code has been generated by the 7Basic
; compiler <http://launchpad.net/7basic>

extern printf
extern scanf

      SECTION .data
printf_f: db "%f",10,0
scanf_f: db "%f",0

      SECTION .bss
v_0 resb 8

      SECTION .text

global main
  main:
push ebp
mov ebp,esp
push v_0         ; load the address of the variable
push scanf_f     ; push the format string
call scanf       ; call scanf()
add esp,8
push dword [v_0+4]  ; load the upper-half of the double
push dword [v_0]    ; load the bottom-half
push printf_f     ; push the format string
call printf       ; call printf
add esp,12
mov esp,ebp
pop ebp
mov eax,0
ret

When I assemble and run the program, I get a prompt as expected. However, no matter what number I enter, the output is always 0.000000.

What am I doing wrong?


回答1:


You are trying to scan a float with the '%f' token but providing a double. Pass in a float variable to scanf and then convert to a double or pass in '%lf' as the format string to scanf.



来源:https://stackoverflow.com/questions/3696627/why-is-scanf-returning-0-000000-when-it-is-supplied-with-a-double

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!