Disclaimer: my question isn’t practical at all, it’s more a question about 2 codes which allegedly abuse the rules and somewhat compile (with more or less warnings / errors according to the compiler).
As far as I know, the following code is valid C99 :
#include <stdio.h>
#include <stddef.h>
int main(void) {
size_t n = sizeof(int[printf("%s", "Hello")]);
}
I understand that sizeof doesn’t evaluate an expression unless it is a VLA (because it has non-constant size).
However, I’m not sure if this other piece of code is valid C99 :
#include <stdio.h>
int main(int argc, char** argv);
int main(int argc, char* argv[sizeof(int[printf("%sn", "Hello") + main(0, NULL)])]) {}
- Is it valid to have an array of size
sizeof(some VLA)
? - Is it valid to have a declaration like
int main(int, char**)
and a definition likeint main(int, char*[some size])
? - And is it valid to recursively call a function from its own parameters ?
I guess making an array of size sizeof(VLA)
is valid, I know no rule which would make this invalid.
If I remember correctly, char*[some size]
as function parameter decays to char**
, but I don’t know if it’s ill-formed because the declaration/definition don’t perfectly match.
And I have no idea if recursively call main from its parameters is valid or not, I obviously didn’t find any resource or example of a code like this. Also, does main have different rules than other functions about recursive calls in the parameters ?
Quotes from the standard would be very appreciated, thanks a lot.