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
46
votes
4 answers

Rounding to nearest fraction (half, quarter, etc.)

So, I need to create the following functions but my head can't think of any possibility in PHP without complicated math. Round always up to the nearest decimal (1.81 = 1.90, 1.89 = 1.90, 1.85 = 1.90) Round always down to the nearest decimal (1.81 =…
user1649331
46
votes
8 answers

What's the proper use of printf to display pointers padded with 0s

In C, I'd like to use printf to display pointers, and so that they line up properly, I'd like to pad them with 0s. My guess was that the proper way to do this was: printf("%016p", ptr); This works, but this gcc complains with the following…
Florian
43
votes
8 answers

Only show decimal point if floating point component is not .00 sprintf/printf

I am pretty formatting a floating point number but want it to appear as an integer if there is no relevant floating point number. I.e. 1.20 -> 1.2x 1.78 -> 1.78x 0.80 -> 0.8x 2.00 -> 2x I can achieve this with a bit of regex but wondering if there…
Bo Jeanes
  • 6,294
  • 4
  • 42
  • 39
43
votes
4 answers

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

Why doesn't this program print the % sign? #include main() { printf("%"); getch(); }
Paul Filch
  • 867
  • 2
  • 7
  • 13
43
votes
7 answers

printf not printing on console

I’m getting started in the C language. I am using eclipse (juno) as my IDE and installed CDT plugin. I have also unpacked mingw64 (GCC Compiler). I wrote a very simple program to see if it works. This is my code: #include int main() { …
Mr T.
  • 4,278
  • 9
  • 44
  • 61
42
votes
5 answers

Passing too many arguments to printf

Any C programmer who's been working for more than a week has encountered crashes that result from calling printf with more format specifiers than actual arguments, e.g.: printf("Gonna %s and %s, %s!", "crash", "burn"); However, are there any…
JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
42
votes
5 answers

Why does printf not print out just one byte when printing hex?

pixel_data is a vector of char. When I do printf(" 0x%1x ", pixel_data[0] ) I'm expecting to see 0xf5. But I get 0xfffffff5 as though I was printing out a 4 byte integer instead of 1 byte. Why is this? I have given printf a char to print out - it's…
BeeBand
  • 10,953
  • 19
  • 61
  • 83
42
votes
4 answers

How should I properly use __attribute__ ((format (printf, x, y))) inside a class method in C++?

I'm trying to define a class method for debug prints that will behave like printf: inline void debug(const char* fmt, ...) __attribute__ ((format (printf, 1, 2))) When I compile with -Wformat or -Wall, This complains about: error: format string…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
41
votes
3 answers

How do I customize output of a custom type using printf?

I've read through a good chunk of Expert F# and am working on building an actual application. While debugging, I've grown accustomed to passing fsi commands like this to make things legible in the repl window: fsi.AddPrinter(fun (x : myType) ->…
flatline
  • 42,083
  • 4
  • 31
  • 38
41
votes
6 answers

string format for intptr_t and uintptr_t

What is the string format for intptr_t and uintptr_t which is valid for both the 32 and 64 bit architecture . EDIT warning: format ‘%x’ expects type ‘unsigned int’, but argument 2 has type "AAA" This is the warning i am getting in 64 bit but not…
thetna
  • 6,903
  • 26
  • 79
  • 113
41
votes
4 answers

Why does printf() promote a float to a double?

From a previous question: If you attempt to pass a float to printf, it'll be promoted to double before printf receives it printf() is a variadic function right? So does a variadic function promote a float argument to a double before passing it?
user4420637
40
votes
9 answers

printf in bash: "09" and "08" are invalid numbers, "07" and "06" are fine

This is my bash script - I just want to left-pad a set of numbers with zeroes: printf "%04d" "09" printf "%04d" "08" printf "%04d" "07" printf "%04d" "06" Output: ./rename.sh: line 3: printf: 09: invalid number 0000 ./rename.sh: line 4: printf:…
Richard
  • 31,629
  • 29
  • 108
  • 145
39
votes
11 answers

How do I align a number like this in C?

I need to align a series of numbers in C with printf() like this example: -------1 -------5 ------50 -----100 ----1000 Of course, there are numbers between all those but it's not relevant for the issue at hand... Oh, consider the dashes as spaces,…
rfgamaral
  • 16,546
  • 57
  • 163
  • 275
39
votes
2 answers

How to check that two format strings are compatible?

Examples: "Something %d" and "Something else %d" // Compatible "Something %d" and "Something else %f" // Not Compatible "Something %d" and "Something %d else %d" // Not Compatible "Something %d and %f" and…
Erik B
  • 40,889
  • 25
  • 119
  • 135
39
votes
4 answers

Why doesn't PRIu64 work in this code?

As per this answer, I tried printing a uint64_t, but it gives me an error: error: expected ``)' before 'PRIu64' Following is the minimal code showing what I am trying to do: #define __STDC_FORMAT_MACROS #include #include…
Masked Man
  • 1
  • 7
  • 40
  • 80