What is wrong with this code?

J

Johs

For some reason I get a compile error when I try to compile this:

#include <stdlib.h>
#include <stdio.h>

#define M 3;
typedef int p[M];

int main(int argc)
{

return 0;
}



gcc -g test.c -o test
test.c:5: error: expected ']' before ';' token
make: *** [test] Error 1

Any ideas, have looked at it from all directions I cannot see the error
 
S

Sourcerer

#define M 3;

Should be changed to

#define M 3

--
"It is easy in the world to live after the world's oppinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/
 
M

Malcolm McLean

Johs said:
For some reason I get a compile error when I try to compile this:

#include <stdlib.h>
#include <stdio.h>

#define M 3;
typedef int p[M];
^^^
int main(int argc)
{
return 0; }



gcc -g test.c -o test
test.c:5: error: expected ']' before ';' token
make: *** [test] Error 1

Any ideas, have looked at it from all directions I cannot see the error

C uses lots of semi colons in places you might not expect, and rejects them
in places you would expect them to be needed. In this case, the semicolon is
interpreted as part of the define.
 
J

jacob navia

Johs a écrit :
For some reason I get a compile error when I try to compile this:

#include <stdlib.h>
#include <stdio.h>

#define M 3;

THE BUG IS HERE
You forgot that ";" should not be a part of the definition.
typedef int p[M];

The preprocessor will replace:
typedef int p[3;];

hence the syntax error
int main(int argc)
{

return 0;
}



gcc -g test.c -o test
test.c:5: error: expected ']' before ';' token
make: *** [test] Error 1

Any ideas, have looked at it from all directions I cannot see the error
 
S

Sourcerer

Johs said:
int main(int argc)
{
return 0; }


Is int main(int argc); according to standard? I think there are only these:
int main(void);
and
int main(int argc, char **argv);
and its void equivalents.

--
"It is easy in the world to live after the world's oppinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/
 
J

Joe Wright

Johs said:
For some reason I get a compile error when I try to compile this:

#include <stdlib.h>
#include <stdio.h>

#define M 3;
typedef int p[M];

int main(int argc)
{

return 0;
}



gcc -g test.c -o test
test.c:5: error: expected ']' before ';' token
make: *** [test] Error 1

Any ideas, have looked at it from all directions I cannot see the error

No semicolon after the 3.

#define M 3
 
J

Jack Klein

For some reason I get a compile error when I try to compile this:

#include <stdlib.h>
#include <stdio.h>

#define M 3;
typedef int p[M];

The replacement text for 'M' is "3;". So when 'M' is expanded in the
line above, the line becomes:

typedef int p[3;];
int main(int argc)
{

return 0;
}



gcc -g test.c -o test
test.c:5: error: expected ']' before ';' token
make: *** [test] Error 1

Any ideas, have looked at it from all directions I cannot see the error

See the extra semicolon? The solution is to remove the semicolon from
the definition of 'M':

#define M 3
 
R

Richard Heathfield

Sourcerer said:
Is int main(int argc); according to standard?
No.

I think there are only
these: int main(void);
and
int main(int argc, char **argv);
and its void equivalents.

Its *exact* equivalents (semantically speaking).
 
R

Richard Heathfield

Johs said:
For some reason I get a compile error when I try to compile this:

#include <stdlib.h>
#include <stdio.h>

#define M 3;
typedef int p[M];

int main(int argc)
{

return 0;
}



gcc -g test.c -o test
test.c:5: error: expected ']' before ';' token
make: *** [test] Error 1

Any ideas, have looked at it from all directions I cannot see the error

As others have noted, the semicolon in your macro definition should not be
there. As one other person has noted (but I think deserves more of a
mention), your code has another error, an incorrect definition of main. The
return type, int, is correct, but main either takes no parameters:

int main(void)

or two parameters:

int main(int argc, char **argv)

and only these two forms of main (and their exact semantic equivalents) are
guaranteed by the Standard to work.
 
S

santosh

Sourcerer said:
Is int main(int argc); according to standard? I think there are only these:
int main(void);
and
int main(int argc, char **argv);
and its void equivalents.

Can you explain what you mean by "its void equivalents"? Thanks.
 
M

Martin Ambuhl

Johs said:
For some reason I get a compile error when I try to compile this:

You have two obvious errors:
#include <stdlib.h>
#include <stdio.h>

#define M 3;
^^
This means the string 'M' will be replaced with the string '3;'. The
semicolon is an error.
typedef int p[M];

int main(int argc)
^^^^^^^^
This is not one of the defined signatures for main
{
return 0;
}
gcc -g test.c -o test

This is not sufficient. gcc by default compiles a language with many
extensions not part of C. Tell gcc to approximate one of the standard
versions of C with
-std=c89 (or -ansi)
or
-std=c99
Setting reasonable diagnostic levels is a good idea. One possible set
is
-W -Wall -pedantic
Read your documentation for more possibilities. And when you use another
compiler, check its documentation as well. The options will almost
certainly be different.

test.c:5: error: expected ']' before ';' token
make: *** [test] Error 1

Any ideas, have looked at it from all directions I cannot see the error
 
S

Sourcerer

santosh said:
Can you explain what you mean by "its void equivalents"? Thanks.

void main(void);
and
void main(int argc, char **argv);

--
"It is easy in the world to live after the world's oppinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/
 
X

Xian

Johs said:
For some reason I get a compile error when I try to compile this:

#include <stdlib.h>
#include <stdio.h>

#define M 3;
typedef int p[M];

int main(int argc)
{

return 0;
}



gcc -g test.c -o test
test.c:5: error: expected ']' before ';' token
make: *** [test] Error 1

Any ideas, have looked at it from all directions I cannot see the error

When things go wrong with preprocessor stuff, looking at its output is
nice. Use the -E switch with gcc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,470
Messages
2,571,807
Members
48,797
Latest member
PeterSimpson
Top