在非常基本的 C 程序上使用 GDB 进行温度转换时出现错误 "printf.c: No such file or directory"
Getting an error that states "printf.c: No such file or directory" while using GDB on a very basic C program for temperature conversion
- 这就是 GDB 向我展示的内容
(gdb) break main
Breakpoint 1 at 0x722: file homework1.c, line 4.
(gdb) run
Starting program: /home/aj_the_kid/ECE_373/homework1
Breakpoint 1, main () at homework1.c:4
4 {
(gdb) step
8 printf("Enter the temperature (for conversion) in Fahrenheit: ");
(gdb) step
__printf (
format=0x555555554838 "Enter the temperature (for conversion) in Fahrenheit: ") at printf.c:28
28 printf.c: No such file or directory.
(gdb) bt
#0 __printf (
format=0x555555554838 "Enter the temperature (for conversion) in Fahrenheit: ") at printf.c:28
#1 0x0000555555554742 in main () at homework1.c:8
(gdb) next
32 in printf.c
(gdb) next
33 in printf.c
(gdb) step
_IO_vfprintf_internal (s=0x7ffff7dd0760 <_IO_2_1_stdout_>,
format=0x555555554838 "Enter the temperature (for conversion) in Fahrenheit: ", ap=ap@entry=0x7fffffffde20) at vfprintf.c:1244
1244 vfprintf.c: No such file or directory.
(gdb) step
1275 in vfprintf.c
- 这是我的 Linux
测试程序
#include <stdio.h>
int main()
{
float temp_F, temp_C;
// Prompt user for input (i.e. temp in Fahrenheit)
printf("Enter the temperature (for conversion) in Fahrenheit: ");
scanf("%f\n", &temp_F);
// Convert F to C
temp_C = (temp_F - 32) * (5/9);
printf("The temperature in Celsius is: %.2f\n", temp_C);
return 0;
}
- 编辑:固定代码以支持浮点型运算
但我仍然遇到问题,它要求我在空行中再次输入华氏度,就像它第一次没有捕获用户输入一样
终端视图
aj_the_kid@AJs-Sandbox:~/ECE_373$ rm file1
aj_the_kid@AJs-Sandbox:~/ECE_373$ gcc -g -o file1 file1.c
aj_the_kid@AJs-Sandbox:~/ECE_373$ ./file1
Enter the temperature (for conversion) in Fahrenheit: 32
32
The temperature in celcius is: 0.00
aj_the_kid@AJs-Sandbox:~/ECE_373$
来自评论:
来自 gdb vfprintf.c: No such file or directory
的错误消息是由于您正试图进入一个您没有源代码的库函数。
计算中的问题是除法(5/9)
是作为整数除法执行的。所以结果是 0。这可以通过更改为 (5.0/9.0)
来解决
而scanf
问题是由于格式字符串末尾的\n
引起的。 scanf
对空格有点奇怪。将 \n
放在格式字符串的末尾会告诉 scanf 跳过所有空格,直到用户键入非空格字符并按回车键。您可以通过删除 \n
来修复它
- 这就是 GDB 向我展示的内容
(gdb) break main
Breakpoint 1 at 0x722: file homework1.c, line 4.
(gdb) run
Starting program: /home/aj_the_kid/ECE_373/homework1
Breakpoint 1, main () at homework1.c:4
4 {
(gdb) step
8 printf("Enter the temperature (for conversion) in Fahrenheit: ");
(gdb) step
__printf (
format=0x555555554838 "Enter the temperature (for conversion) in Fahrenheit: ") at printf.c:28
28 printf.c: No such file or directory.
(gdb) bt
#0 __printf (
format=0x555555554838 "Enter the temperature (for conversion) in Fahrenheit: ") at printf.c:28
#1 0x0000555555554742 in main () at homework1.c:8
(gdb) next
32 in printf.c
(gdb) next
33 in printf.c
(gdb) step
_IO_vfprintf_internal (s=0x7ffff7dd0760 <_IO_2_1_stdout_>,
format=0x555555554838 "Enter the temperature (for conversion) in Fahrenheit: ", ap=ap@entry=0x7fffffffde20) at vfprintf.c:1244
1244 vfprintf.c: No such file or directory.
(gdb) step
1275 in vfprintf.c
- 这是我的 Linux 测试程序
#include <stdio.h>
int main()
{
float temp_F, temp_C;
// Prompt user for input (i.e. temp in Fahrenheit)
printf("Enter the temperature (for conversion) in Fahrenheit: ");
scanf("%f\n", &temp_F);
// Convert F to C
temp_C = (temp_F - 32) * (5/9);
printf("The temperature in Celsius is: %.2f\n", temp_C);
return 0;
}
- 编辑:固定代码以支持浮点型运算
但我仍然遇到问题,它要求我在空行中再次输入华氏度,就像它第一次没有捕获用户输入一样
终端视图
aj_the_kid@AJs-Sandbox:~/ECE_373$ rm file1
aj_the_kid@AJs-Sandbox:~/ECE_373$ gcc -g -o file1 file1.c
aj_the_kid@AJs-Sandbox:~/ECE_373$ ./file1
Enter the temperature (for conversion) in Fahrenheit: 32
32
The temperature in celcius is: 0.00
aj_the_kid@AJs-Sandbox:~/ECE_373$
来自评论:
来自 gdb vfprintf.c: No such file or directory
的错误消息是由于您正试图进入一个您没有源代码的库函数。
计算中的问题是除法(5/9)
是作为整数除法执行的。所以结果是 0。这可以通过更改为 (5.0/9.0)
而scanf
问题是由于格式字符串末尾的\n
引起的。 scanf
对空格有点奇怪。将 \n
放在格式字符串的末尾会告诉 scanf 跳过所有空格,直到用户键入非空格字符并按回车键。您可以通过删除 \n