tags:

views:

247

answers:

7

Greetings

I am trying to learn C and finding myself getting stuck a lot, no complains : )

Anyway, I wrote a program and gcc does not like it, the following code is NOT the program but demonstrate the problem:

#define MAXLINE = 1000

int main()
{
   int tmp = MAXLINE;
   char line[MAXLINE];

   return 0;
}

when compile, I get the following error:

   test.c:7: error: expected expression before ‘=’ token

if I replace symbolic constant MAXLINE with int 1000, everything works.

what is going on?

Thanks

--rhr

+18  A: 

Defines don't need equal signs :)

#define maxline 1000
Byron Whitlock
+10  A: 

There shouldn't be = in define just

#define MAXLINE 1000
Cem Kalyoncu
Beaten by 7 seconds - bad luck :(
Jonathan Leffler
+1  A: 
#define MAXLINE 1000
pix0r
+3  A: 

The #define statement doesn't need the equals sign.

It should read:

#define MAXLINE 1000

pgb
+3  A: 

Use #define without '=':

#define MAXLINE 1000
igustin
+3  A: 

You shoud have

#define MAXLINE 1000

You can read more here http://gcc.gnu.org/onlinedocs/cpp/Object%5F002dlike-Macros.html#Object%5F002dlike-Macros

alinrus
+18  A: 

When the preprocessor replaces your definition of MAXLINE, your code is changed to

int main()
{
   int tmp = = 1000;
   char line[= 1000];
   return 0;
}

The C preprocessor is very dumb! Do not put anything extra in your #defines (no equals, no semicolons, no nothing)

pmg
Upvoted because, unlike the rest, you explained what actually happens when you define. :-)
Coding With Style
Agreed with Coding With Style
Carson Myers