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.

Filter by
Sorted by
Tagged with
2264votes
56answers
2.1mviews

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 ...
631votes
10answers
426kviews

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?
user avatar
  • 11.5k
592votes
8answers
983kviews

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 ...
user avatar
  • 30.9k
561votes
7answers
962kviews

What is the argument 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.
user avatar
558votes
5answers
1.3mviews

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(&...
user avatar
  • 5,591
515votes
56answers
1.0mviews

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&...
user avatar
  • 7,802
478votes
14answers
350kviews

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 -...
user avatar
  • 18.9k
459votes
16answers
426kviews

'printf' vs. 'cout' in C++

What is the difference between printf() and cout in C++?
user avatar
  • 4,631
428votes
13answers
877kviews

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 ...
user avatar
  • 28.4k
407votes
7answers
828kviews

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", ...
user avatar
  • 68.7k
390votes
11answers
437kviews

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 ...
user avatar
  • 7,141
323votes
106answers
254kviews

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++?
318votes
13answers
328kviews

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 */
user avatar
  • 8,379
262votes
8answers
242kviews

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 ...
user avatar
239votes
5answers
728kviews

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, ...
user avatar
  • 2,411
220votes
17answers
872kviews

How to print a float with 2 decimal places in Java?

Can I do it with System.out.print?
user avatar
  • 2,353
215votes
4answers
140kviews

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?
user avatar
  • 4,530
212votes
5answers
94kviews

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 = ...
user avatar
208votes
24answers
82kviews

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: $...
user avatar
  • 46.5k
201votes
10answers
214kviews

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 ...
user avatar
  • 457k
189votes
8answers
435kviews

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 ...
user avatar
  • 15.8k
165votes
11answers
417kviews

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 ...
user avatar
  • 35.2k
163votes
6answers
326kviews

How I can print to stderr in C?

In C, Printing to stdout is easy, with printf from stdio.h. However, how can print to stderr? We can use fprintf to achieve it apparently, but its syntax seems strange. Maybe we can use printf to ...
user avatar
  • 2,051
152votes
8answers
139kviews

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 ...
user avatar
  • 31.6k
148votes
1answer
106kviews

Format number as fixed width, with leading zeros [duplicate]

The following code a <- seq(1,101,25) b <- paste("name", 1:length(a), sep = "_") produces this output: "name_1" "name_26" "name_51" "name_76" "name_101" I'd like to have the same width ...
user avatar
  • 1,847
147votes
4answers
136kviews

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\%\...
user avatar
  • 3,592
145votes
11answers
115kviews

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?
user avatar
  • 12.9k
145votes
4answers
118kviews

What does "%.*s" mean in printf?

I got a code snippet in which there is a printf("%.*s\n") what does the %.*s mean?
user avatar
  • 3,410
139votes
4answers
296kviews

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 ...
user avatar
139votes
8answers
318kviews

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 ...
user avatar
  • 10.7k
131votes
3answers
6kviews

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 ...
user avatar
  • 19.6k
124votes
8answers
109kviews

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 ...
user avatar
  • 211k
123votes
16answers
170kviews

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. ...
user avatar
  • 10.6k
120votes
13answers
139kviews

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 ...
user avatar
  • 1,201
118votes
7answers
417kviews

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 ...
user avatar
  • 13.2k
117votes
6answers
65kviews

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...
user avatar
  • 3,052
116votes
4answers
261kviews

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 ...
user avatar
  • 29.3k
113votes
5answers
166kviews

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:...
user avatar
  • 7,825
112votes
8answers
161kviews

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 ...
user avatar
  • 2,939
112votes
5answers
23kviews

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": ...
user avatar
  • 20.2k
110votes
1answer
16kviews

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 "%...
user avatar
  • 52.3k
106votes
6answers
166kviews

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, ...
user avatar
105votes
11answers
19kviews

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!...
user avatar
  • 1,380
101votes
9answers
77kviews

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);....
user avatar
  • 3,762
100votes
3answers
6kviews

Results of printf() and system() are in the wrong order when output is redirected to a file [duplicate]

I have a C program that compiles to an executable called myprogram. This is its main function: int main(int argc, char ** argv) { printf("this is a test message.\n"); system("ls"); return 0; } ...
user avatar
  • 1,061
97votes
5answers
154kviews

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 ...
user avatar
  • 49.8k
97votes
13answers
152kviews

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 ...
user avatar
94votes
7answers
100kviews

C++ equivalent of sprintf?

I know that std::cout is the C++ equivalent of printf. What is the C++ equivalent of sprintf?
user avatar
  • 5,645
92votes
12answers
187kviews

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 ...
user avatar
  • 8,334
89votes
7answers
130kviews

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: ...
user avatar
  • 1,144

15 30 50 per page
1
2 3 4 5
178