A few things to note:
#define
doesn't require braces ()
- only use them if you need to handle parameters
- The syntax doesn't include a semicolon:
#define test printf
- Calling a function like
printf()
like this (somewhat obscured) can be risky, especially if the caller doesn't expect that their string is used as a format string. Prefer #define test(msg) printf("%s", msg)
- After a
#define test ...
, the pre-processor will dumbly replace all instances of test
- thus the function declaration will actually read void printf("worked\n"); { ... }
The result should be either:
#include <stdio.h>
#define test(msg) printf("%s\n", msg)
void main(void) {
test("hello");
}
or:
#include <stdio.h>
void test(const char *msg) {
printf("%s\n", msg);
}
void main(void) {
test("hello");
}
If you are trying to use a #define
to redirect a function call, then you must use a different symbol... for example:
#include <stdio.h>
/* Comment / uncomment this to change behaviour */
#define REDIRECT_TEST
#ifdef REDIRECT_TEST
# define my_test(msg) printf("REDIRECTED:%s\n", msg)
#else
# define my_test test
#endif
void test(const char *msg) {
printf("%s\n", msg);
}
void main(void) {
my_test("hello");
}