C语言 在 C 程序中使用 #define

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15464652/
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 05:44:38  来源:igfitidea点击:

Using #define in C Program

cc-preprocessor

提问by Oliver Spryn

I am trying to use #defineto define a constant in my program. I realize I could use const, but I am trying to get a good understanding of #define. Could someone please explain why the following code does not work, and should be done instead?

我试图#define在我的程序中定义一个常量。我意识到我可以使用const,但我试图很好地了解#define. 有人可以解释为什么下面的代码不起作用,而是应该这样做吗?

#include <stdio.h>
#define M 20;
typedef int Marray_t[M][M]; //I can't define an M x M array

int main() {
  Marray_t A;
  int i;

  for (i = 0; i < M; ++i) { //Can't iterate up to M
    A[i] = i;
  }

  return 0;
}

回答by nabroyan

You must remove ;after20, like this

你必须删除;after 20,像这样

#define M 20