【问题标题】:Why is scanf returning 0.000000 when it is supplied with a double?为什么 scanf 在提供双精度时返回 0.000000?
【发布时间】:2010-09-12 21:09:24
【问题描述】:

我有以下汇编代码(为 Linux 上的 NASM 编写):

; 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

当我组装并运行程序时,我得到了预期的提示。但是,无论我输入什么数字,输出总是0.000000

我做错了什么?

【问题讨论】:

    标签: assembly x86 scanf nasm


    【解决方案1】:

    您正在尝试使用 '%f' 标记扫描浮点数,但提供了双精度。将 float 变量传递给 scanf,然后转换为 double 或将 '%lf' 作为格式字符串传递给 scanf。

    【讨论】:

    • 这太奇怪了...printf() 强迫我们使用doublesscanf() 没有?
    • @Cirno:啊,这很好解释。谢谢。
    猜你喜欢
    • 2015-01-06
    • 2016-03-31
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多