I came to this question and see this line
printf(&c[i]);
I want to know that, is this a valid syntax ? Printing a string/string literal without format specifier %s
does't cause undefined behavior or constraint violation ?
And if this is a valid syntax then in what purpose it is used ?
I compiled this code
#include <stdio.h>
int main()
{
char *c = "Hello World";
printf(c);
printf("\n\n");
printf(&c[0]);
return 0;
}
and it compiles without giving any warning/error with output
Hello World
Hello World
It's unwise (particularly if said string came from a user), but it's not illegal.
The clause in question would be section 7.19.6.1, paragraph 3 (emphasis mine):
You should either use
fputs(c);
orfprintf("%s", c);
to make sure your string isn't accidentally interpreted to contain directives.