How to Print Percentage(%) in C language

on 10 June 2011

My friend was so proud of his C programming skills. So to bring him down to earth I just asked him a simple question about Printing Special Characters in C Language. I asked him to write a C code to print percentage symbol. He said what is a big deal in that!

He started writing code in Dev C++ and I was shocked to see his program:

void main() { printf("%"); getch(); }

Lol, compiler showed errors... He did much things like using escape sequence backslash(\) before % to print the percentage sign but all was in vain. At last he succeeded after about an hour!(After referring to a book and ASCII code table) This is the C code he programmed to print the percent special character:

printf("%c", 37);

But this method uses the ASCII code. This is not at all a good method to print it. I will show you a very simple method to print a % symbol which is as follows:

Just use a second percent sign to escape the percent sign!

printf("100%%\n"); // print 100% printf("%%"); // print a percent sign

So that was a simple method for which C language experts would scratch their head..

7 comments:

Anonymous said...

how to print %c in c program ?

Akash said...

printf("%%c");

Vee Eee Technologies said...

Thanks for the post. It was very interesting and meaningful.

Unknown said...

Hi.. Actually my job is displaying % in virtual key board and when user press i have to receive only one % and compare these string with wireless SSID ,if both same i have to make connection. But for displaying one "%" in vk(virtual key board) i am using printf("%%"); ,when user press % key,it receiving '%%' ,so it trying to compare with SSID(i have selected SSID as %) it's mismatching ?Friends any solution for this??

Anonymous said...

how to print # in c

Anonymous said...

what is meaning of %% ?

Anonymous said...

simply enter
cout<<the value assigned<<"%"<<endl;

Post a Comment