If you liked the post, Share on Facebook, Tweet and Google Plus (use buttons above). You can also Subscribe to our feed via Email for free.
Home » Programming » How to Print Percentage(%) in C language
How to Print Percentage(%) in C language
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:
how to print %c in c program ?
printf("%%c");
Thanks for the post. It was very interesting and meaningful.
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??
how to print # in c
what is meaning of %% ?
simply enter
cout<<the value assigned<<"%"<<endl;
Post a Comment