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
32
votes
6 answers

printf formatting (%d versus %u)

What is difference between %d and %u when printing pointer addresses? For example: int a = 5; // check the memory address printf("memory address = %d\n", &a); // prints "memory address = -12" printf("memory address = %u\n", &a); // prints "memory…
kapil
31
votes
10 answers

C++ Streams vs. C-style IO?

I was coding some C++ for a small hobby project when I noticed that I'm using C-style operations to access IO (printf, fopen, etc.). Is it considered "bad practice" to involve C functions in C++ projects? What are the advantages of using streams…
gablin
  • 4,678
  • 6
  • 33
  • 47
31
votes
6 answers

Printing a char with printf

Are both these codes the same char ch = 'a'; printf("%d", ch); Will it print a garbage value? I am confused about this printf("%d", '\0'); Will this print 0 or garbage value? Because when i do this printf("%d", sizeof('\n')); It prints 4. Why…
user535450
31
votes
4 answers

Converting a void* to a std::string

After perusing the web and messing around myself, I can't seem to convert a void*'s target (which is a string) to a std::string. I've tried using sprintf(buffer, "%p", *((int *)point)); as recommended by this page to get to a C string, but to no…
Lewis
  • 1,310
  • 1
  • 15
  • 28
31
votes
8 answers

How to combine two 32-bit integers into one 64-bit integer?

I have a count register, which is made up of two 32-bit unsigned integers, one for the higher 32 bits of the value (most significant word), and other for the lower 32 bits of the value (least significant word). What is the best way in C to combine…
bei
  • 933
  • 3
  • 9
  • 17
31
votes
8 answers

How to hide leading zero in printf

The following outputs 0.23. How do I get it to simply output .23? printf( "%8.2f" , .23 );
Christian Mann
  • 8,030
  • 5
  • 41
  • 51
31
votes
3 answers

Printf variable number of decimals in float

I found interesting format for printing nonterminated fixed length strings like this: char newstr[40] = {0}; sprintf(newstr,"%.*s", sizeof(mystr), mystr); So I think maybe is there a way under printf command for printing a float…
Wine Too
  • 4,515
  • 22
  • 83
  • 137
30
votes
3 answers

Why does gcc -Wall give warning about zero-length format string?

I searched around a little bit for information on this but didn't find anything satisfactory. Is there some special behavior to the function call sprintf(someString, ""); that explains why this is warning (on gcc with -Wall)? I only managed to…
SirGuy
  • 10,660
  • 2
  • 36
  • 66
30
votes
1 answer

Why is 0 (zero) printed without leading "0x" with C printf format "%#x"?

Background: I have a number of scripts that parse log files looking for hex numbers by finding the leading "0x". Our embedded C library changed to a new printf. The new printf is more standards compliant than our previous and my scripts broke. On a…
David Poole
  • 3,432
  • 5
  • 34
  • 34
30
votes
6 answers

Performance of variable expansion vs. sprintf in PHP

Regarding performance, is there any difference between doing: $message = "The request $request has $n errors"; and $message = sprintf('The request %s has %d errors', $request, $n); in PHP? I would say that calling a function involves more stuff,…
elitalon
  • 9,191
  • 10
  • 50
  • 86
30
votes
3 answers

printf() \t option

When you are printing a tab character to the standard output using printf in C, it outputs some space which is apparently 4 characters in length. printf("\t"); Is there a way by which I can control the tab width in the above case?
liv2hak
  • 14,472
  • 53
  • 157
  • 270
30
votes
4 answers

How to print a char array in C through printf?

This results in segmentation fault. What needs to be corrected? int main(void) { char a_static = {'q', 'w', 'e', 'r'}; char b_static = {'a', 's', 'd', 'f'}; printf("\n value of a_static: %s", a_static); printf("\n value of b_static:…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
30
votes
3 answers

Unsigned values in C

I have the following code: #include int main() { unsigned int a = -1; int b = -1; printf("%x\n", a); printf("%x\n", b); printf("%d\n", a); printf("%d\n", b); printf("%u\n", a); printf("%u\n", b); …
rvillablanca
  • 1,606
  • 3
  • 21
  • 34
30
votes
3 answers

printf adds extra `FFFFFF` to hex print from a char array

Consider the following simplified code bellow. I want to extract some binary data/stream from a file and print it to the standard output in Hexadecimal format. I got extra 3 bytes 0xFFFFFF. What's wrong? From where did the extra bytes…
user.dz
  • 962
  • 2
  • 19
  • 39
30
votes
6 answers

printf format for unsigned __int64 on Windows

I need to print a ULONGLONG value (unsigned __int64). What format should i use in printf ? I found %llu in another question but they say it is for linux only. Thanks for your help.
Virus721
  • 8,061
  • 12
  • 67
  • 123