rhr
rhr

Reputation:

Why doesn't GCC like this?

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 it is compiled, 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?

Upvotes: 2

Views: 472

Answers (7)

Cem Kalyoncu
Cem Kalyoncu

Reputation: 14593

There shouldn't be = in define just

#define MAXLINE 1000

Upvotes: 10

pmg
pmg

Reputation: 108988

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)

Upvotes: 21

user173973
user173973

Reputation:

You shoud have

#define MAXLINE 1000

You can read more here http://gcc.gnu.org/onlinedocs/cpp/Object_002dlike-Macros.html#Object_002dlike-Macros

Upvotes: 3

igustin
igustin

Reputation: 1118

Use #define without '=':

#define MAXLINE 1000

Upvotes: 3

pgb
pgb

Reputation: 25001

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

It should read:

#define MAXLINE 1000

Upvotes: 3

pix0r
pix0r

Reputation: 31280

#define MAXLINE 1000

Upvotes: 1

Byron Whitlock
Byron Whitlock

Reputation: 53861

Defines don't need equal signs :)

#define maxline 1000

Upvotes: 20

Related Questions