Questions tagged [printf]
printf is a common function for formatted output. C and many other languages have a whole family of related functions. Only use this tag if the question is directly concerned with printf or related functions.
printf
9,541
questions
2432
votes
61
answers
2.4m
views
JavaScript equivalent to printf/String.Format
I'm looking for a good JavaScript equivalent of the C/PHP printf() or for C#/Java programmers, String.Format() (IFormatProvider for .NET).
My basic requirement is a thousand separator format for ...
697
votes
8
answers
1.3m
views
What is the printf format specifier for bool?
Since ANSI C99 there is _Bool or bool via stdbool.h. But is there also a printf format specifier for bool?
I mean something like in that pseudo code:
bool x = true;
printf("%B\n", x);
which would ...
695
votes
10
answers
501k
views
Why does printf not flush after the call unless a newline is in the format string?
Why does printf not flush after the call unless a newline is in the format string? Is this POSIX behavior? How might I have printf immediately flush every time?
615
votes
7
answers
1.1m
views
What is the conversion specifier for printf that formats a long?
The printf function takes an argument type, such as %d or %i for a signed int. However, I don't see anything for a long value.
600
votes
59
answers
1.2m
views
Is there a printf converter to print in binary format?
I can print with printf as a hex or octal number. Is there a format tag to print as binary, or arbitrary base?
I am running gcc.
printf("%d %x %o\n", 10, 10, 10); //prints "10 A 12\n&...
598
votes
5
answers
1.5m
views
Correct format specifier for double in printf
What is the correct format specifier for double in printf? Is it %f or is it %lf? I believe it's %f, but I am not sure.
Code sample
#include <stdio.h>
int main()
{
double d = 1.4;
printf(&...
526
votes
14
answers
420k
views
How can one print a size_t variable portably using the printf family?
I have a variable of type size_t, and I want to print it using printf(). What format specifier do I use to print it portably?
In 32-bit machine, %u seems right. I compiled with g++ -g -W -Wall -...
522
votes
18
answers
496k
views
'printf' vs. 'cout' in C++
What is the difference between printf() and cout in C++?
467
votes
14
answers
1.0m
views
How do you format an unsigned long long int using printf?
#include <stdio.h>
int main() {
unsigned long long int num = 285212672; //FYI: fits in 29 bits
int normalInt = 5;
printf("My number is %d bytes wide and its value is %ul. A normal ...
460
votes
7
answers
965k
views
How to printf "unsigned long" in C?
I can never understand how to print unsigned long datatype in C.
Suppose unsigned_foo is an unsigned long, then I try:
printf("%lu\n", unsigned_foo)
printf("%du\n", unsigned_foo)
printf("%ud\n", ...
419
votes
11
answers
516k
views
Printing leading 0's in C
I'm trying to find a good way to print leading 0, such as 01001 for a ZIP Code. While the number would be stored as 1001, what is a good way to do it?
I thought of using either case statements or if ...
341
votes
13
answers
375k
views
How to escape the % (percent) sign in C's printf
How do you escape the % sign when using printf in C?
printf("hello\%"); /* not like this */
322
votes
106
answers
256k
views
Printing 1 to 1000 without loop or conditionals
Task: Print numbers from 1 to 1000 without using any loop or conditional statements. Don't just write the printf() or cout statement 1000 times.
How would you do that using C or C++?
284
votes
9
answers
270k
views
Difference between fprintf, printf and sprintf?
Can anyone explain in simple English about the differences between printf, fprintf, and sprintf with examples?
What stream is it in?
I'm really confused between the three of these while reading ...
271
votes
5
answers
907k
views
printf() formatting for hexadecimal
Why, when printing a number in hexadecimal as an 8 digit number with leading zeros, does %#08X not display the same result as 0x%08X?
When I try to use the former, the 08 formatting flag is removed, ...
251
votes
19
answers
1.1m
views
How to print a float with 2 decimal places in Java?
Can I do it with System.out.print?
241
votes
5
answers
110k
views
PHP sprintf escaping %
I want the following output:-
About to deduct 50% of € 27.59 from your Top-Up account.
when I do something like this:-
$variablesArray[0] = '€';
$variablesArray[1] = 27.59;
$stringWithVariables = ...
234
votes
4
answers
149k
views
What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf)
What is the difference between %d and %i when used as format specifiers in printf and scanf?
216
votes
9
answers
535k
views
printf with std::string?
My understanding is that string is a member of the std namespace, so why does the following occur?
#include <iostream>
int main()
{
using namespace std;
string myString = "Press ENTER ...
216
votes
24
answers
88k
views
Why use sprintf function in PHP?
I am trying to learn more about the PHP function sprintf() but php.net did not help me much as I am still confused, why would you want to use it?
Take a look at my example below.
Why use this:
$...
215
votes
10
answers
241k
views
What is the difference between printf() and puts() in C?
I know you can print with printf() and puts(). I can also see that printf() allows you to interpolate variables and do formatting.
Is puts() merely a primitive version of printf(). Should it be used ...
213
votes
7
answers
440k
views
How can I print to standard error in C with 'printf'?
In C, printing to standard output (stdout) is easy, with printf from stdio.h.
However, how can I print to standard error (stderr)? We can use fprintf to achieve it apparently, but its syntax seems ...
180
votes
11
answers
471k
views
How do you allow spaces to be entered using scanf?
Using the following code:
char *name = malloc(sizeof(char) + 256);
printf("What is your name? ");
scanf("%s", name);
printf("Hello %s. Nice to meet you.\n", name);
A user can enter their name but ...
179
votes
8
answers
174k
views
Is there a way to specify how many characters of a string to print out using printf()?
Is there a way to specify how many characters of a string to print out (similar to decimal places in ints)?
printf ("Here are the first 8 chars: %s\n", "A string that is more than 8 chars");
Would ...
167
votes
5
answers
381k
views
What is the format specifier for unsigned short int?
I have the following program
#include <stdio.h>
int main(void)
{
unsigned short int length = 10;
printf("Enter length : ");
scanf("%u", &length);
printf("value is %u \n"...
165
votes
4
answers
157k
views
What does "%.*s" mean in printf?
I got a code snippet in which there is a
printf("%.*s\n")
what does the %.*s mean?
162
votes
9
answers
390k
views
Printf width specifier to maintain precision of floating-point value
Is there a printf width specifier which can be applied to a floating point specifier that would automatically format the output to the necessary number of significant digits such that when scanning ...
160
votes
4
answers
155k
views
Java: Literal percent sign in printf statement
I'm trying to add an actual percent sign into a printf statement in Java and I'm getting the error:
lab1.java:166: illegal escape character
System.out.printf("%s\t%s\t%1.2f\%\t%1.2f\%\...
159
votes
4
answers
368k
views
Left-pad printf with spaces
How can I pad a string with spaces on the left when using printf?
For example, I want to print "Hello" with 40 spaces preceding it.
Also, the string I want to print consists of multiple lines. Do I ...
158
votes
12
answers
134k
views
What is the use of the %n format specifier in C?
What is the use of the %n format specifier in C? Could anyone explain with an example?
148
votes
8
answers
127k
views
What's up with Java's "%n" in printf?
I'm reading Effective Java and it uses %n for the newline character everywhere. I have used \n rather successfully for newline in Java programs.
Which is the 'correct' one? What's wrong with \n ? Why ...
144
votes
9
answers
210k
views
Using colors with printf
When written like this, it outputs text in blue:
printf "\e[1;34mThis is a blue text.\e[0m"
But I want to have format defined in printf:
printf '%-6s' "This is text"
Now I have tried several ...
138
votes
17
answers
203k
views
Avoid trailing zeroes in printf()
I keep stumbling on the format specifiers for the printf() family of functions. What I want is to be able to print a double (or float) with a maximum given number of digits after the decimal point. ...
137
votes
6
answers
77k
views
Using printf with a non-null terminated string
Suppose you have a string which is NOT null terminated and you know its exact size, so how can you print that string with printf in C? I recall such a method but I can not find out now...
134
votes
13
answers
163k
views
Padding characters in printf
I am writing a bash shell script to display if a process is running or not.
So far, I got this:
printf "%-50s %s\n" $PROC_NAME [UP]
The code gives me this output:
JBoss ...
131
votes
8
answers
499k
views
Printing hexadecimal characters in C
I'm trying to read in a line of characters, then print out the hexadecimal equivalent of the characters.
For example, if I have a string that is "0xc0 0xc0 abc123", where the first 2 characters are ...
131
votes
3
answers
6k
views
How does the below program output `C89` when compiled in C89 mode and `C99` when compiled in C99 mode?
I've found this C program from the web:
#include <stdio.h>
int main(){
printf("C%d\n",(int)(90-(-4.5//**/
-4.5)));
return 0;
}
The interesting thing with this program is that ...
129
votes
4
answers
329k
views
printf format specifiers for uint32_t and size_t
I have the following
size_t i = 0;
uint32_t k = 0;
printf("i [ %lu ] k [ %u ]\n", i, k);
I get the following warning when compiling:
format ‘%lu’ expects type ‘long unsigned int’, but argument ...
125
votes
5
answers
189k
views
The "backspace" escape character '\b': unexpected behavior?
So I'm finally reading through K&R, and I learned something within the first few pages, that there is a backspace escape character, \b.
So I go to test it out, and there is some very odd behavior:...
125
votes
6
answers
199k
views
How to format strings using printf() to get equal length in the output
I have two functions, one which produces messages like Starting initialization... and another which checks return codes and outputs "Ok", "Warning" or "Error". However, ...
118
votes
5
answers
25k
views
Python's many ways of string formatting — are the older ones (going to be) deprecated?
Python has at least six ways of formatting a string:
In [1]: world = "Earth"
# method 1a
In [2]: "Hello, %s" % world
Out[2]: 'Hello, Earth'
# method 1b
In [3]: "Hello, %(planet)s" % {"planet": ...
115
votes
13
answers
182k
views
Integer ASCII value to character in BASH using printf
Character to value works:
$ printf "%d\n" \'A
65
$
I have two questions, the first one is most important:
How do I take 65 and turn it into A?
\'A converts an ASCII character to its value using ...
113
votes
1
answer
18k
views
How does Haskell printf work?
Haskell's type safety is second to none only to dependently-typed languages. But there is some deep magic going on with Text.Printf that seems rather type-wonky.
> printf "%d\n" 3
3
> printf "%...
110
votes
9
answers
80k
views
Clean code to printf size_t in C++ (or: Nearest equivalent of C99's %z in C++)
I have some C++ code that prints a size_t:
size_t a;
printf("%lu", a);
I'd like this to compile without warnings on both 32- and 64-bit architectures.
If this were C99, I could use printf("%z", a);....
109
votes
5
answers
187k
views
In C can a long printf statement be broken up into multiple lines?
I have the following statement:
printf("name: %s\targs: %s\tvalue %d\tarraysize %d\n", sp->name, sp->args, sp->value, sp->arraysize);
I want to break it up. I tried the following but it ...
108
votes
11
answers
25k
views
Why is printf with a single argument (without conversion specifiers) deprecated?
In a book that I'm reading, it's written that printf with a single argument (without conversion specifiers) is deprecated. It recommends to substitute
printf("Hello World!");
with
puts("Hello World!...
108
votes
26
answers
135k
views
How to format a number using comma as thousands separator in C?
In C, how can I format a large number from e.g. 1123456789 to 1,123,456,789?
I tried using printf("%'10d\n", 1123456789), but that doesn't work.
Could you advise anything? The simpler the ...
106
votes
7
answers
115k
views
C++ equivalent of sprintf?
I know that std::cout is the C++ equivalent of printf.
What is the C++ equivalent of sprintf?
104
votes
13
answers
229k
views
How to repeat a char using printf?
I'd like to do something like printf("?", count, char) to repeat a character count times.
What is the right format-string to accomplish this?
EDIT: Yes, it is obvious that I could call printf() in a ...
102
votes
7
answers
149k
views
How to pass variable number of arguments to printf/sprintf
I have a class that holds an "error" function that will format some text. I want to accept a variable number of arguments and then format them using printf.
Example:
class MyClass
{
public:
...