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;
}
Brian Tompsett - 汤莱恩
5,92972 gold badges63 silver badges135 bronze badges
asked Mar 17, 2013 at 18:39

1 Answer 1

65

You must remove ; after20, like this

#define M 20
ThiefMaster
320k85 gold badges607 silver badges648 bronze badges
answered Mar 17, 2013 at 18:40
2
  • 1
    Oh... it was defining it as 20;. I totally didn't think of that! Thank you. Commented Mar 17, 2013 at 18:41
  • 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. Commented Jul 2, 2017 at 9:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.