为什么 scanf 在提供双精度值时返回 0.000000? [英] Why is scanf returning 0.000000 when it is supplied with a double?

查看:39
本文介绍了为什么 scanf 在提供双精度值时返回 0.000000?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

我做错了什么?

推荐答案

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

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.

这篇关于为什么 scanf 在提供双精度值时返回 0.000000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆