13

私はいくつかの厄介な前処理を試みていましたが、次のようなものを思いつきました:

#include <stdio.h>

#define SIX =6

int main(void)
{
  int x=6;
  int y=2;

  if(x=SIX)
    printf("X == 6\n");
  if(y=SIX)
    printf("Y==6\n");

  return 0;
}

gcc は私にエラーを与えます:

test.c: 関数 'main' 内:
test.c:10:8: エラー: '=' トークンの前に式が
必要です test.c:12:8: エラー: '=' トークンの前に式が必要です

何故ですか?

4

4 に答える 4

15

The == is a single token, it cannot be split in half. You should run gcc -E on your code

From GCC manual pages:

-E Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output.

Input files that don't require preprocessing are ignored.

For your code gcc -E gives the following output

  if(x= =6)
    printf("X == 6\n");

  if(y= =6)
    printf("Y==6\n");

The second = is what causes the error message expected expression before ‘=’ token

于 2013-08-23T15:59:47.130 に答える
5

プリプロセッサは文字レベルでは機能せず、トークン レベルで動作します。したがって、置換を実行すると、次のようになります。

if (x = = 6)

あなたの希望ではなく:

if (x==6)

#これには、文字列化演算子など、いくつかの特定の例外があります。

于 2013-08-23T16:01:16.027 に答える
4
if(x=SIX) 

として解析されます

if (x= =6).

したがって、エラーが発生します。

于 2013-08-23T16:05:05.153 に答える
0

どのツールチェーンを使用していますか? GCC を使用している場合は、-save-tempsオプションを追加し、test.i中間結果を確認して問題をトラブルシューティングできます。

x=との間にスペースがあると思います=6

于 2013-08-23T16:02:57.627 に答える