I ran the following code segment in C:
printf("%%%\n");
I got the output "%" (without quotes). Can anyone explain what exactly happened? Why we got only one % sign in result?
I ran the following code segment in C:
printf("%%%\n");
I got the output "%" (without quotes). Can anyone explain what exactly happened? Why we got only one % sign in result?
%% will print %. %\n is not a valid conversion specifier.
You should always enable warnings. See the following:
warning: unknown conversion type character 0xa in format [-Wformat=]
printf("%\n");
^
As noted in the comments, this is undefined behavior because according to the C11 standard, it is undefined behavior if:
— An invalid conversion specification is found in the format for one of the formatted input/ouptut functions [...]