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
Post a Comment