In C every function has a fixed number of parameters. Well, there are variadic functions but if you look at the header files these are actually macros which themselves call plain functions that, in the end, have a fixed number of arguments.
Anyway, if you really want to have "default arguments" a simple way is to specify a "wrapper function" that converts some default value, for example NULL in the case of a const char* into an appropriate default value of your choice. Example:
Code:
int main()
{
hello("my message", NULL); // use default argument for argument 2
hello("my message", "my test msg");
return 0;
}
So how do you write hello() in such a way that NULL is converted into "Testing...\n" ? Here is one way
Code:
void hello_ (const char *message, const char *testMsg) {
printf("message is %s\n", message);
printf("testMsg is %s\n", testMsg);
}
// void hello(const char *message, const char *testMsg = "Testing...\n");
void hello(const char *message, const char *testMsg)
{
if (testMsg == NULL) hello_(message, "Testing...\n");
else hello_(message, testMsg);
}
If this case NULL is a magic value that you are using to mean to the function "I dont care about this parameter so just give me the default"