Printing '%' with printf in C/C++ [duplicate] - c

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to escape the % (percent) sign in C's printf
How can I print '%' in C?
E.g.:
printf("A: %.2f%", pc);
...fails, the compiler complains there is an invalid conversion. Of course an easy way is;
printf("A: %.2f%c", pc, '%');
But it's rather inelegant...
I looked on the web, but I didn't find any escape sequence for %. I thought % would work, but it doesn't.

printf("A: %.2f%%", pc);

Just double the '%' in the format string and it will print '%'.

For future printf reference, type:
man 3 printf
on any Linux command prompt. It can do a lot of crazy stuff that most people just aren't aware of.

%% will output % using printf.
Check the example at the end of page here

Related

How can i place % symbol after an interger [duplicate]

This question already has answers here:
How to escape the % (percent) sign in C's printf
(13 answers)
Closed 5 years ago.
I need to print a string with an integer an the symbol % after it.
Here is what i try:
sprintf(txt, "%d",CurrentArrayValue,'%');
But my result is only my number. None symbol after that
Use, simply a %% in the format string:
sprintf(txt, "%d%%",CurrentArrayValue * 100);
Your '%' is ignored by sprintf since your formatting string indicates to the function that only one additional argument is needed.
You have to multiply CurrentArrayValue by 100 yourself by the way: I slipped that in to the function parameter list.
sprintf(txt, "%d%%",CurrentArrayValue);
This will do the trick.
From standard §7.21.6.1 C11 standard N1570
%
A % character is written. No argument is converted. The complete conversion specification shall be %%.

Why does printf("%%") print only one percent (%) symbol? [duplicate]

This question already has answers here:
How to escape the % (percent) sign in C's printf
(13 answers)
Closed 8 years ago.
When I run this following code:
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
printf("%%");
getch();
}
I get % as an output?
What might be the reason behind this logic?
That is what printf does: it is print formatted (f for formatted). It uses % as the formatting character. It is the only reserved character and needs to be escaped to represent it self, i.e. %%. See the manual for more information on formatting: printf.
P.S.: Never use a string that is not a part of the program as the first argument. To print a string message that was input by a user, do printf(%s, message);. Otherwise you will have a security hole in your code.
% comes into format specifiers.
Example
When we write printf("%d", 20);, it will print 20 rather than %d. because the compiler treats % as a format specifier. In the mind of the compiler, the meaning of % is somewhat special.
So if you want that "%" should be the output, then you must write printf("%%"). Here the first % sign will suppress the meaning of the % format specifier and will print % as an output.
From the standard ISO/IEC 9899:1999 (E)
7.19.6.1
Each conversion specification is introduced by the character %.
The conversion specifiers and their meanings are:
% - A % character is written. No argument is converted. The complete
conversion specification shall be %%.
For C printf, % is a special character which typically indicates a parameter to substitute at that position: printf("Hello, %s\n", "World!"); results in "Hello, World!". There are lots of different things you can put after the % depending on the data you want to output. So that leaves the problem of "What if I want to print a percent symbol"?
The solution: Use %%.
The same is true of the special escape character \. "\n" means to print a new line. If you want to actually print the forward slash, you have to put it twice \\
See Printf format string and MSDN.

Printing a % sign in C using 'printf' [duplicate]

This question already has answers here:
How to escape the % (percent) sign in C's printf
(13 answers)
Closed 9 years ago.
I am trying to print a % sign using printf.
I have tried with no luck:
printf("\%");
I'm sure it's really simple, but I've just started C.
Use printf("%%"); The backslash is the escape character for C strings; the compiler interprets it. The percent sign is printfs escape character; the printf routine interprets it.
Here are two ways:
printf("%%\n");
printf("%c\n", '%');
Try by using the following escape sequence:
%%
Check out all escapes usable in *rintf() functions family

How do I print the percent sign(%) in C? [duplicate]

This question already has answers here:
How to escape the % (percent) sign in C's printf
(13 answers)
Closed 7 years ago.
Why doesn't this program print the % sign?
#include <stdio.h>
main()
{
printf("%");
getch();
}
Your problem is that you have to change:
printf("%");
to
printf("%%");
Or you could use ASCII code and write:
printf("%c", 37);
:)
There's no explanation in this topic why to print a percentage sign. One must type %% and not for example an escape character with percentage - \%.
From comp.lang.c FAQ list · Question 12.6:
The reason it's tricky to print % signs with printf is that % is
essentially printf's escape character. Whenever printf sees a %, it
expects it to be followed by a character telling it what to do next.
The two-character sequence %% is defined to print a single %.
To understand why % can't work, remember that the backslash \ is the
compiler's escape character, and controls how the compiler interprets
source code characters at compile time. In this case, however, we want
to control how printf interprets its format string at run-time. As far
as the compiler is concerned, the escape sequence % is undefined, and
probably results in a single % character. It would be unlikely for
both the \ and the % to make it through to printf, even if printf were
prepared to treat the \ specially.
So the reason why one must type printf("%%"); to print a single % is that's what is defined in the printf function. % is an escape character of printf's, and \ of the compiler.
Use "%%". The man page describes this requirement:
% A '%' is written. No argument is converted. The complete conversion specification is '%%'.
Try printing out this way
printf("%%");

"formatted" C language [duplicate]

This question already has answers here:
Formatted and unformatted input and output and streams
(3 answers)
Closed 9 years ago.
I want to know what is meant by formatted in the printf and scanf functions of the C language. I am new to C programming and did not understand what is meant by formatted. Can anyone please give me the appropriate explanation?
printf("%s %d", "abc", 123);
%s %d is the format string and contains instructions for what to do with the arguments. In this case, it says print string, print a space, then print an integer.
As in formatted output
So you can 'print' say a double called myMoney of 11003.145 as $11,003.15.

Resources