A mysterious error reported by gcc

mcmay 2014-11-18 07:23:05
各位达人,我用gcc的编译器编译以下程序,但老是在宏定义那里报错,但在VC6上却毫无问题。麻烦各位看看,谢谢!

/*
** This program reads input lines from standard input and prints
** each input line, followed by just some portions of the line, to
** the standard output.
**
** The first input is a lint of column numbers, which ends with a
** negative number. The column numbers are paired and specify
** ranges of columns from the input line that are to be printed.
** For example, 0 3 10 12 -1 indicates that only columns 0 through 3
** and columns 10 through 12 will be printed.
**/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COLS 20 /* max # of columns to process */
#define MAX_INPUT 1000 /* max len of input & output lines */
/× 上面的这个宏定义被报错:expected identifier before numeric constant */
int read_column_numbers(int columns[], int max);
void rearrange(char * output, char const * input, int n_columns, int const columns[]);

int main(void)
{
int n_columns; /* # of columns to process */
int columns[MAX_COLS]; /* the columns to process */
char input[MAX_INPUT]; /* array for input line */
char output[MAX_INPUT]; /* array for output line */

/*
** Read the list of columns numbers
*/
n_columns = read_column_numbers(columns, MAX_COLS);

/*
** Read, process and print the remaining lines of input
*/
puts("Input lines (empty line to quit):");
while(gets(input) != NULL){
printf("Original input: %s\n", input);
rearrange(output, input, n_columns, columns);
printf("Rearranged line: %s\n", output);
puts("Input lines (empty line to quit):");
}
return EXIT_SUCCESS;
}

/*
** Read the list of columns numbers, ignoring any beyond the specified maxim.
*/

int read_column_numbers(int columns[], int max)
{
int num = 0;
int ch;

/*
** Get the numbers, stopping at eof or when a number is < 0.
*/
puts("Input the column numbers:");
while (num < max && scanf("%d", &columns[num]) == 1 && columns[num] >= 0)
num += 1;

/*
** Make sure we have an even number of inputs, as they are supposed to be paired.
*/
if(num % 2 != 0){
puts("Last column number is not paired.");
exit(EXIT_FAILURE);
}
/*
** Discard the rest of the line that contained the final number.
*/
while ((ch = getchar()) != EOF && ch != '\n')
;
return num;
}

/*
** Process as line of input by concatenating the characters from the indicated columns.
** The output line is the NUL terminated.
*/

void rearrange(char * output, char const * input, int n_columns, int const columns[])
{
int col; /* subscript for columns array */
int output_col; /* output column counter */
int len; /* length of input line */

len = strlen(input);
output_col = 0;

/*
** Process each pair of column numbers.
*/
for(col = 0; col < n_columns; col += 2){
int nchars = columns[col+1] - columns[col] + 1;

/*
** If the input line isn't this long or the output array is full, we're done.
*/
if(columns[col] >= len || output_col == MAX_INPUT - 1)
break;

/*
** If there isn't room in the output array, only copy what will fit.
*/
if(output_col + nchars > MAX_INPUT - 1)
nchars = MAX_INPUT - output_col - 1;
/*
** Copy the relevant data.
*/
strncpy(output + output_col, input + columns[col], nchars);
output_col += nchars;
}
output[output_col] = '\0';
}
...全文
183 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mcmay 2014-11-18
  • 打赏
  • 举报
回复
引用 1 楼 brookmill 的回复:
gcc 4.8.2 编译通过。 楼主把MAX_INPUT改个名字再试试吧,我的/usr/include/limits.h里面有个宏定义也叫MAX_INPUT 楼主还可以试试用gcc -E test.c来看看预处理器到底把它弄成什么样了。
谢谢brookmill!我也怀疑就是这个MAX_INPUT宏名在捣鬼,难怪换到VC6编译就不会有事了。而且奇怪的是,我将源文件放到VC6的工程里编译之后再用CodeBlocks编译,竟然通过了!呵呵,编译器的内幕很深啊!
brookmill 2014-11-18
  • 打赏
  • 举报
回复
gcc 4.8.2 编译通过。 楼主把MAX_INPUT改个名字再试试吧,我的/usr/include/limits.h里面有个宏定义也叫MAX_INPUT 楼主还可以试试用gcc -E test.c来看看预处理器到底把它弄成什么样了。
赵4老师 2014-11-18
  • 打赏
  • 举报
回复
内幕再深,gcc是开源的。
下载代码方式:https://pan.quark.cn/s/dd3561eca308 在软件开发领域,面向对象编程(OOP)是一种普遍采纳的结构化方法,它使得开发者能够借助模拟现实环境中的实体和关系来构建软件系统。在本案例中,我们观察到的是一个关于借助抽象类来执行不同几何图形面积求解的实践应用。现在,让我们详细分析这一议题。 标题 "应用抽象类计算面积" 清晰地表明我们将要讨论一个抽象类,此类设定了一个用于测量图形面积的标准函数,但并未提供实际的执行过程。抽象类在诸如C#或Java等编程语言中通常借助`abstract`修饰符进行声明,它们无法直接创建对象实例,仅能作为其他类的基础模板。 描述部分提及的"图形界面应用"暗示这是一个基于视觉用户界面(GUI)的系统,可能运用了.NET Framework的Windows Forms或WPF技术,或者是Java平台的Swing或JavaFX框架。在这样环境下,用户能够通过视觉元素与这些几何体进行互动,例如输入相关尺寸并观看到计算得出的面积值。 抽象类“几何体”内嵌了“计算面积”这一抽象函数。在代码层面,这可以被表述为: ```csharp public abstract class GeometricShape { public abstract double CalculateArea(); } ``` 随后,有三个派生类:圆(Circle)、矩形(Rectangle)和三角形(Triangle),它们各自提供了这个抽象函数的具体实现。比如,圆的面积是通过π乘以半径的平方得到的,矩形的面积是长和宽的乘积,而三角形的面积可能是底乘以高再除以2的结果。这些类将提供具体实现来计算它们各自的面积: ```csharp p...
内容概要:本文系统研究了移相控制全桥LLC谐振变换器的工作特性,深入分析其在不同工作模式下的运行机理与性能表现,重点探讨了软开关实现、高效率能量转换及宽范围电压调节等关键技术优势。通过Simulink搭建精确的仿真模型,对谐振腔参数、开关频率、电压增益、系统效率等关键指标进行仿真分析,验证了理论设计的正确性。同时,详细研究了移相控制策略对系统动态响应、稳定性和轻载/重载工况适应性的影响,揭示了控制参数与电路参数之间的耦合关系,为高频高效电源设计提供了理论依据和实践指导。; 适合人群:具备电力电子技术、模拟电路及自动控制理论基础,从事开关电源、新能源变换器、电动汽车充电模块或高频电源系统研发的工程师及高校研究生。; 使用场景及目标:①掌握全桥LLC谐振变换器的拓扑结构、工作原理与关键参数设计方法;②理解移相控制在实现零电压开通(ZVS)和零电流关断(ZCS)中的作用机制;③通过Simulink仿真掌握变换器建模、参数优化与性能评估流程,服务于实际产品开发与学术课题研究。; 阅读建议:建议读者结合提供的Simulink仿真模型进行同步操作,重点关注谐振网络(Lr, Lm, Cr)参数与移相角之间的匹配设计,深入理解软开关条件的形成过程,并通过调整负载和输入电压进行多工况仿真,以全面掌握系统动态特性。

70,039

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧