我想从C编程语言中练习1-10。这个想法是创建一个输出等于输入的程序,但是,如果打印选项卡,它应该打印\t
而不是实际选项卡。它也建议用backspace/backslash做同样的事情,但我试图让它在向前移动的时候只使用一个标签。c编程语言练习1.10:我的解决方案有什么问题?
我确定一个标签的值为9,所以我想出了这个代码。我很困惑,为什么这不起作用 - 这似乎是解决问题的简单方法。如果字符getchar
收到的值等于9,则选项卡会显示该值,然后以纯文本格式输出\t
。我很乐意因为任何导致我用下面的代码咆哮错误的树而被打上脑门。我看到一些人发布解决方案here,但我仍然困惑于什么小细节导致它失败。
#include <stdio.h>
main(){
int c;
while ((c = getchar()) != EOF) {
if ((c == '\t') == 9) putchar("\t");
else purchar(c);
}
}
这带来了以下编译错误
tenth.c: In function 'main':
tenth.c:7:35: warning: passing argument 1 of 'putchar' makes integer from pointe
r without a cast
if ((c == '\t') == 9) putchar("\t");
^
In file included from tenth.c:1:0:
c:\mingw\include\stdio.h:645:43: note: expected 'int' but argument is of type 'c
har *'
__CRT_INLINE __cdecl __MINGW_NOTHROW int putchar(int __c)
^
C:\Users\*\AppData\Local\Temp\ccC4FPSb.o:tenth.c:(.text+0x18): undefined ref
erence to `purchar'
collect2.exe: error: ld returned 1 exit status
我也试过
main(){
int c;
while ((c = getchar()) != EOF) {
if (c == '\t') putchar("\t");
else purchar(c);
}
}
请注意,您应该编码至少旧的(C99)标准,最好是新的(C11)标准。既不允许'main()'(尽管C89/C90确实是这样做的)。新标准都要求显式指定返回类型 - 如果不打算使用命令行参数,则建议使用“int main(void)”。如果你的编译器没有提醒你这个问题,可以添加警告标记,这样做 - 或者得到一个更好的编译器。 –
谢谢你!我将改变我的代码,以适当的格式开始。 – cfinspan
'main()'函数有两个有效签名这两个签名在当前场景中都有返回类型'int',建议使用:'int main(void)' – user3629249