c preprocessor - Syntax error while using a #define in initializing an array, and as arguments to a function in C? -



c preprocessor - Syntax error while using a #define in initializing an array, and as arguments to a function in C? -

using #define while initializing array #include <stdio.h> #define test 1; int main(int argc, const char *argv[]) { int array[] = { test }; printf("%d\n", array[0]); homecoming 0; }

compiler complains:

test.c: in function ‘main’: test.c:7: error: expected ‘}’ before ‘;’ token make: *** [test] error 1 using #define functional input arguments #include <stdio.h> #define test 1; void print_arg(int arg) { printf("%d", arg); } int main(int argc, const char *argv[]) { print_arg(test); homecoming 0; }

compiler complains:

test.c: in function ‘main’: test.c:12: error: expected ‘)’ before ‘;’ token make: *** [test] error 1

how 1 solve these 2 problems? thought c search , replace on source file, replacing test 1, no?

the problem because there semicolon in #define test 1;.

with this, programme translates to:

int array[] = { 1; }; /*this illegal!*/

remedy: remove looks like:

#define test 1

which translates to:

int array[] = {1}; /*legal*/

c c-preprocessor

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -