% is part of conversion specifications such as %d or %f . Therefore, if you simply write %! , it will be interpreted as requiring a second argument matching the data type associated to ! (a so-called conversion specifier). printf("Hello, World%!"); will print Hello, World! . Furthermore, it will display a formatting warning at compilation.
Because the percent char is used internally, by the printf() function, as a part of a format specifier.
In essence, you're telling printf() to print out a value or variable, with the %! format protocol.
Which fails, because there is no %! format protocol, so printf() just prints out the exclamation point normally. It "eats" the % since it thought it was a format specifier, and it always "eats" format specifiers (replacing them with actual data in a specific format).
If you study your header files (stdio.h in this case), you might learn a lot, but the truth is, when you get far enough "under the hood" on these "Why?" questions, you'll wind up with one answer, before long:
"Because it was made that way".
C was made up by just a handful of programmers, based somewhat on the B language, which was based somewhat on the A language.
If you're going to have format specifiers for your print function, you have to have chars dedicated to those purposes. And you don't want those char's being printed, themselves.
i know that but i want to know the reason behind it why its happening
It is happening because in The C Programming Language by Brian W. Kernighan & Dennis M. Ritchie (Prentice Hall 1978) on page 147 (based on 15th printing) where it is finishing its description of the control string for printf, it says:
"If the character after the % is not a conversion character, that character is printed; thus % may be printed by %%."
In general, do not use this book as a reference; instead use the second edition which was published during the finalization of the ANSI stanard (C89).
This handling was continued during the standardization in ANSI C89 and ISO C90 which are nearly equivalent. I do not have access to either of these documents.
In the ISO C99 standard in section 126.96.36.199The fprintf function paragraphs 4 through 8, it describes the conversion specifications. At the end of paragraph 8, it says "% The % character is written. No argument is converted. The complete conversion specification shall be %%."