C语言 “错误:数字常量之前的预期标识符或'('”-?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4160225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:00:23  来源:igfitidea点击:

"error: expected identifier or ‘(’ before numeric constant" –?

cmacrosc-preprocessor

提问by Ryan Lester

The error in the title occurs on the '#define' line for every single function-like macro in this header file (starting at line 45). Am I doing something wrong?

标题中的错误出现#define在此头文件中每个类似函数的宏的“ ”行(从第 45 行开始)。难道我做错了什么?

#ifndef ASSEMBLER_H
#define ASSEMBLER_H


/* Ports */
#define   Input    0
#define   Output   15

/* Registers */
#define   Z    0
#define   A    1
#define   B    2
#define   C    3
#define   D    4
#define   E    5
#define   F    6
#define   G    7

/* OP Codes */
    /*-----Control--------*/
#define   HLT_OP    0
#define   JMP_OP    1
#define   CJMP_OP   2
#define   OJMP_OP   3
    /*-----Load/Store-----*/
#define   LOAD_OP   4
#define   STORE_OP  5
#define   LOADI_OP  6
#define   NOP_OP    7
    /*-----Math-----------*/
#define   ADD_OP    8
#define   SUB_OP    9
    /*-----Device I/O-----*/
#define   IN_OP     10
#define   OUT_OP    11
    /*-----Comparison-----*/
#define   EQU_OP    12
#define   LT_OP     13
#define   LTE_OP    14
#define   NOT_OP    15

/* Macros */
    /*-----Control--------*/
#define HLT()
  ( HLT_OP << 28 )
#define JMP(address)
  ( (JMP_OP << 28) | (address) )
#define CJMP(address)
  ( (CJMP_OP << 28) | (address) )
#define OJMP(address)
  ( (OJMP_OP << 28) | (address) )
    /*-----Load/Store-----*/
#define LOAD(dest, value)
  ( (LOAD_OP << 28) | ((dest) << 24) | (value) )
#define STORE(dest, value)
  ( (STORE_OP << 28) | ((dest) << 24) | (value) )
#define LOADI(dest, value)
  ( (LOADI_OP << 28) | ((dest) << 24) | (value) )
#define NOP()
  ( NOP_OP << 28 )
    /*-----Math-----------*/
#define ADD(dest, op1, op2)
  ( (ADD_OP << 28) | ((dest) << 24) | ((op1) << 20) | ((op2) << 16) )
#define SUB(dest, op1, op2)
  ( (SUB_OP << 28) | ((dest) << 24) | ((op1) << 20) | ((op2) << 16) )
    /*-----Device I/O-----*/
#define IN(reg)
  ( (IN_OP << 28) | ((reg) << 24) | (Input) )
#define OUT(reg)
  ( (OUT_OP << 28) | ((reg) << 24) | (Output) )
    /*-----Comparison-----*/
#define EQU(reg1, reg2)
  ( (EQU_OP << 28) | ((reg1) << 24) | ((reg2) << 20) )
#    define LT(reg1, reg2)
  ( (LT_OP << 28) | ((reg1) << 24) | ((reg2) << 20) )
#define LTE(reg1, reg2)
  ( (LTE_OP << 28) | ((reg1) << 24) | ((reg2) << 20) )
#define NOT()
  ( NOT_OP << 28 )



#endif

回答by James McNellis

A preprocessing directive (like #define) is terminated by a newline, so it can only be one line long. If you want a multi-line macro definition, you need to escape the newline:

预处理指令(如#define)由换行符终止,因此它只能是一行。如果你想要一个多行的宏定义,你需要转义换行符:

//                  v Make this the last character on the line
#define NOT()       \
  ( NOT_OP << 28 ) 

回答by Patrick

Macro's should be written on 1 line, not 2.

宏应该写在 1 行,而不是 2 行。

This is wrong:

这是错误的:

#define NOT()
  ( NOT_OP << 28 )

This is right:

这是对的:

#define NOT() ( NOT_OP << 28 )

If you really want to write it on multiple lines, use a backslash to 'escape' the new line. like this:

如果您真的想将其写在多行上,请使用反斜杠来“转义”新行。像这样:

#define NOT() \
  ( NOT_OP << 28 )

回答by Diego Sevilla

Hmmm... I guess you're not putting the subsitution for the #definein a different line, right?

嗯...我猜你不会把 的替代品#define放在不同的行中,对吗?

#define JMP(address)
( (JMP_OP << 28) | (address) )

should be

应该

#define JMP(address) ( (JMP_OP << 28) | (address) )

instead, for example. #definedeclarations have to be in just one line, or use \at the end of a line to expand to the next.

相反,例如。#define声明必须只在一行中,或者\在一行的末尾使用以扩展到下一行。