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
38
votes
3 answers

C: Which character should be used for ptrdiff_t in printf?

Which character should be used for ptrdiff_t in printf? Does C standard clearly explains how to print ptrdiff_t in printf? I haven't found any one. int a = 1; int b = 2; int* pa = &a; int* pb = &b; ptrdiff_t diff = b - a; printf("diff = %?",…
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
38
votes
6 answers

Print the structure fields and values in C

I am interested in printing the structure fields . Typedef struct { UINT32 thread_id; BOOL is_valid; }T_THREAD; Is there a way in "C" language to print the contents of a structure, something like ex: print (T_THREAD) and output should be…
user3555115
  • 546
  • 1
  • 4
  • 11
38
votes
3 answers

Print a leading '+' for positive numbers in printf

I've a temperature conversion program as an assignment, which I've completed. The program has many printf statements in it which print the temperature. Now the negative temperatures are printed the way I want them but the positive temperatures are…
user292844
  • 589
  • 2
  • 5
  • 7
38
votes
2 answers

C: Correct way to print "unsigned long" in hex

I have a function that gets an unsigned long variable as parameter and I want to print it in Hex. What is the correct way to do it? Currently, I use printf with "%lx" void printAddress(unsigned long address) { printf("%lx\n", address); } Should…
SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
38
votes
2 answers

C/C++ printf() before scanf() issue

I'm using Eclipse to code in C/C++ and I'm struggling with what might be something pretty easy. In my code below I use printf() and after scanf(). Althougth printf is written before scanf() the output differs. I was able to find out something about…
quapka
  • 2,799
  • 4
  • 21
  • 35
37
votes
4 answers

cross-platform printing of 64-bit integers with printf

In Windows, it is "%I64d". In Linux and Solaris, it is "%lld". If I want to write cross-platform printfs that prints long long values: what is good way of doing so ? long long ll; printf(???, ll);
Andrei
  • 8,606
  • 10
  • 35
  • 43
37
votes
6 answers

Does printf("%x",1) invoke undefined behavior?

According to the C standard (6.5.2.2 paragraph 6) If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are…
37
votes
9 answers

Printf example in bash does not create a newline

Working with printf in a bash script, adding no spaces after "\n" does not create a newline, whereas adding a space creates a newline, e. g.: No space after "\n" NewLine=`printf "\n"` echo -e…
WolfHumble
  • 373
  • 1
  • 3
  • 6
37
votes
4 answers

What is the difference between %f and %lf in C?

I have seen these two parameters in a C example in a C book, but the author didn't elaborate what the difference between the two is. I know that %f specifies that a float should take its place. I have tried looking this up but have had a hard time…
committedandroider
  • 8,711
  • 14
  • 71
  • 126
37
votes
4 answers

php sprintf with array

I have an array witch match string placeholders as follow: "some text %s another text %s extra text %s" and the array: $array[0] match the first %s $array[1] match the second %s $array[2] match the third %s I thought it could be done using the…
tarek
  • 751
  • 2
  • 7
  • 9
36
votes
3 answers

How to left justify text in Bash

Given a text, $txt, how could I left justify it to a given width in Bash? Example (width = 10): If $txt=hello, I would like to print: hello | If $txt=1234567890, I would like to print: 1234567890|
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
36
votes
2 answers

Why does wprintf transliterate Russian text in Unicode into Latin on Linux?

Why does the following program #include #include int main() { wprintf(L"Привет, мир!"); } print "Privet, mir!" on Linux? Specifically, why does it transliterate Russian text in Unicode into Latin as opposed to transcoding it…
vitaut
  • 49,672
  • 25
  • 199
  • 336
36
votes
5 answers

Variable sized padding in printf

Is there a way to have a variable sized padding in printf? I have an integer which says how large the padding is: void foo(int paddingSize) { printf("%...MyText", paddingSize); } This should print out ### MyText where the paddingSize should…
Egil
  • 8,101
  • 4
  • 17
  • 6
36
votes
3 answers

Understanding output of printf containing backslash (\012)

Can you please help me to understand the output of this simple code: const char str[10] = "55\01234"; printf("%s", str); The output is: 55 34
Radoslaw Krasimirow
  • 1,833
  • 2
  • 18
  • 28
36
votes
5 answers

What’s the correct way to use printf to print a clock_t?

I'm currently using a explicit cast to unsigned long long and using %llu to print it, but since size_t has the %z specifier, why doesn't clock_t have one? There isn't even a macro for it. Maybe I can assume that on an x64 system (OS and CPU) size_t…
Spidey
  • 2,508
  • 2
  • 28
  • 38