printf("\"My salary was increased by 15%%!\"\n");

why this prints "My salary was increased by 15%!"
i think it should print %% instead of %

Think of %l as just ONE thing - a format specifying the data type to be printed. So what's left is just one % char to print.

but %! will print %! but according to you it should print!

% 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.

commented: Nice .... I did not know this +1

i have checked it and it is printing
Hello, World%!
so what you are saying is different

I mis-read your post, so let's clarify:

printf("15%!");   //prints 15%!
printf("15%%!");  //prints 15%! same as the first one

If you want two % char's you need:

printf("15%%%!"); //prints 15%%!

i know that but i want to know the reason behind it why its happening

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.

So there you are.

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 7.19.6.1 The 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 %%."

I hope this helps.

in fact,

printf("15%!");   //prints 15!

(note small syntax error above) ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.