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.

For detailed information on how the printf function works in C, refer to Open Group Base Specifications or the C standard.

C99 knows these members of the printf()-family:

  printf     vprintf     wprintf     vwprintf
 fprintf    vfprintf    fwprintf    vfwprintf
 sprintf    vsprintf    swprintf    vswprintf
snprintf   vsnprintf   snwprintf   vsnwprintf

POSIX additionally knows these:

 dprintf    vdprintf

GNU adds these:

asprintf   vasprintf

The name is generally of the form:

  • optional v for va_list
  • output specifier: None for stdout, f for FILE*, s for supplied buffer, sn for supplied buffer of specified length, as for allocated buffer, d for supplied file descriptor
  • optional w for wide variant.
  • printf

Documentation for printf function is available on Linux/Unix in section 3 of the manual and can be accessed by using man 3 printf.

For a multi-lingual description of the history of printing formatted strings across many languages, refer to Wikipedia article printf format string

9422 questions
104
votes
25 answers

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 solution the better.
goe
  • 5,207
  • 14
  • 45
  • 49
102
votes
7 answers

C++ equivalent of sprintf?

I know that std::cout is the C++ equivalent of printf. What is the C++ equivalent of sprintf?
lital maatuk
  • 5,921
  • 20
  • 57
  • 79
102
votes
13 answers

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 loop, but that is just what I wanted to avoid.
Maestro
  • 9,046
  • 15
  • 83
  • 116
100
votes
3 answers

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

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; } When I run myprogram > output.txt in a Linux…
Archr
  • 1,059
  • 1
  • 7
  • 11
100
votes
7 answers

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: void Error(const char* format, ...); }; The Error…
user5722
  • 1,264
  • 3
  • 10
  • 13
98
votes
16 answers

How to append strings using sprintf?

I am facing a serious issue with sprintf. Suppose my code snippet is: sprintf(Buffer,"Hello World"); sprintf(Buffer,"Good Morning"); sprintf(Buffer,"Good Afternoon"); . . . Some hundred sprints.... If I do like this, it's getting overwritten. How…
user46646
  • 153,461
  • 44
  • 78
  • 84
94
votes
3 answers

Two decimal places using printf( )

I'm trying to write a number to two decimal places using printf() as follows: #include int main() { printf("When this number: %d is assigned to 2 dp, it will be: 2%f ", 94.9456, 94.9456); return 0; } When I run the program, I get the…
aali
94
votes
3 answers

printf anomaly after "fork()"

OS: Linux, Language: pure C I'm moving forward in learning C programming in general, and C programming under UNIX in a special case. I detected a strange (for me) behaviour of the printf() function after using a fork() call. Code #include…
pechenie
  • 1,908
  • 2
  • 18
  • 17
91
votes
3 answers

Is it possible to print out only a certain section of a C-string, without making a separate substring?

Say I have the following: char* string = "Hello, how are you?"; Is it possible to print out only the last 5 bytes of this string? What about the first 5 bytes only? Is there some variation of printf that would allow for this?
Tim
  • 4,295
  • 9
  • 37
  • 49
89
votes
10 answers

Why does printf("%f",0); give undefined behavior?

The statement printf("%f\n",0.0f); prints 0. However, the statement printf("%f\n",0); prints random values. I realize I'm exhibiting some kind of undefined behaviour, but I can't figure out why specifically. A floating point value in which all…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
89
votes
13 answers

How does this program work?

#include int main() { float a = 1234.5f; printf("%d\n", a); return 0; } It displays a 0!! How is that possible? What is the reasoning? I have deliberately put a %d in the printf statement to study the behaviour of printf.
Lazer
  • 90,700
  • 113
  • 281
  • 364
89
votes
14 answers

How to fmt.Printf an integer with thousands comma

Does Go's fmt.Printf support outputting a number with the thousands comma? fmt.Printf("%d", 1000) outputs 1000, what format can I specify to output 1,000 instead? The docs don't seem to mention commas, and I couldn't immediately see anything in the…
BrandonAGr
  • 5,827
  • 5
  • 47
  • 72
86
votes
6 answers

How to print a string in C++

I tried this, but it didn't work. #include string someString("This is a string."); printf("%s\n", someString);
node ninja
  • 31,796
  • 59
  • 166
  • 254
86
votes
2 answers

Why does %d stand for integer?

I know this doesn't sound productive, but I'm looking for a way to remember all of the formatting codes for printf calls. %s, %p, %f are all obvious, but I can't understand where %d comes from. Is %i already taken by something else?
grasingerm
  • 1,955
  • 2
  • 19
  • 22
83
votes
1 answer

What is PRIu64 in C?

I am new to C and I am confronted with: #include #include int main(void) { uint64_t foo = 10; printf("foo is equal to %" PRIu64 "!\n", foo); return 0; } And it works! I don't understand why. Can somebody…
torr
  • 1,256
  • 3
  • 11
  • 20