【发布时间】:2016-04-01 13:05:18
【问题描述】:
我想打印速度的屏幕索引。我不是代码的作者。
代码在这里http://pastebin.com/47CbB1vb
而get line也是编译所必需的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* get_line:
* Reads a line into string, returns length.
* Kernighan & Ritchie p.69
*
*/
int
get_line(fp, line, lim)
FILE *fp;
char *line;
int lim;
{
int c, i;
i = 0;
while (--lim > 0 && (c = getc(fp)) != EOF && c != '\n')
line[i++] = c;
if (c == '\n')
{
line[i++] = c;
}
line[i] = '\0';
if (c == EOF)
return (-1);
else
return (i);
}
/*
* ignore_line:
* Gets the next line (up to and including the newline
* character) from the file pointed to by fptr and
* promptly loses it. Taken from asc2ah.c.
*
* Siggi 29.06.1990
*/
int
ignore_line(dat_fp)
FILE *dat_fp;
{
char string[256];
char *fgets();
if (fgets(string, 250, dat_fp) == NULL) /* nothing there */
return (-1);
return (0); /* there was something */
当我尝试用 gcc 编译时
gcc gi_line.c vel2d.c -lm -o vel2d
vel2d.c: In function ‘main’:
vel2d.c:205:19: warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘int’ [-Wformat=]
fprintf(stdout,"%.1f",index);
^
vel2d.c:206:9: error: ‘else’ without a previous ‘if’
else
^
我只包含了 fprintf 行。没有那行我可以编译代码并且它可以完美运行。那么我应该改变什么?
【问题讨论】:
-
你的代码 sn-p 中的
fprintf在哪里? (它在第 205 行,而有问题的else在第 206 行。您的代码没有那么多行。) -
@owacoder:好的,我现在看到了。 “我不需要大括号,它只是一个语句”和“我将在这里添加一个快速打印语句进行调试”的典型案例。
-
return是一个语句,而不是一个函数。没有必要给结果加上括号,也不应该在现代代码中用如此简单的表达式来完成。长期以来,不推荐使用非原型函数声明符。代码很古老,可能包含与标准 C 不同的其他过时功能。 -
不要只发布代码的外部链接。请参阅How to Ask 并提供minimal reproducible example