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

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

Related

What does conversion specifier %n exactly do? [duplicate]

This question already has answers here:
What is the use of the %n format specifier in C?
(12 answers)
Closed 7 years ago.
I just encountered this by looking in the standard:
7.19.6.1 The fprintf function
in
8 The conversion specifiers and their meanings are:
regarding to:
n The argument shall be a pointer to signed integer into which is written the
number of characters written to the output stream so far by this call to
fprintf. No argument is converted, but one is consumed. If the conversion
specification includes any flags, a field width, or a precision, the behavior is
undefined.
What does this mean? What does %n do?
Did I get it correct, that acording to:
Returns
14 The fprintf function returns the number of characters transmitted
In this snippet:
int a, b;
b = printf ("Thi%n\s is just a test",&a);
a would equal to b?
the number of characters written to the output stream so far
"So far", means wherever you place your %n, the result will change. As of your example, it will be 3.
If your increase your %s position by one char, the resulting variable pointed will increase by one. Placing your %s at the very end of the string will make it equal to the value returned by printf
a = 3 and b = 19 for your case
a will be equal to number of character printed before %n.
Suppose you try to print printf ("This%sis%n just a test","coder", &a);
Then the value of a will be this + coder + is = 11.
And the value of b is always the total number of characters printed

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("%%");

understanding a notation in printf [duplicate]

This question already has answers here:
What does "%.*s" mean as a format specifier in printf?
(7 answers)
Closed 9 years ago.
This might be a very basic question to many of you, but I am not able to understand
what %.*s doing?
void substring(int i, int j, char *ch)
{
printf("The substring is: %.*s\n", j - i, &ch[i]);
//what is %.*s doing?
}
The * is taking the length limit for the string from the argument before the string. So the printf will output (at most) j - i characters from &ch[i] to stdout. If the string is shorter, then the entire string will be printed, but it will not be blank padded.
Here is a good reference for printf: http://en.cppreference.com/w/c/io/fprintf.*
And this is what it says:
. followed by integer number or * that specifies precision of the conversion. In the case when * is used, the precision is specified by an additional argument of type int. If the value of this argument is negative, it is ignored. See the table below for exact effects of precision.
And for s, it says:
Precision specifies the maximum number of bytes to be written.
So in your case, it prints a maximum of j-i characters.
* In fact, it's a very good reference for just about all standard C and C++ libraries. Use it!

Resources