27

I am trying to use #define to 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?

#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;
}

1 Answer 1

65

You must remove ; after20, like this

#define M 20
Sign up to request clarification or add additional context in comments.

2 Comments

Oh... it was defining it as 20;. I totally didn't think of that! Thank you.
Also if like me you are an idiot and put = assignment in define, try removing it. This shows how much I dislike defines and how I don't use C enough any more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.