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

wrapper printf function that filters according to user preferences

My program writes to a log and to stdout. Every message, however, has a certain priority and the user specifies in Preferences which priorities go to which stream (log or stdout). unsigned short PRIO_HIGH = 0x0001; unsigned short PRIO_NORMAL =…
wsd
  • 1,246
  • 4
  • 16
  • 22
29
votes
5 answers

Is there a way to have printf() properly print out an array (of floats, say)?

I believe I have carefully read the entire printf() documentation but could not find any way to have it print out, say, the elements of a 10-element array of float(s). E.g., if I have float[] foo = {1., 2., 3., ..., 10.}; Then I'd like to have a…
lindelof
  • 34,556
  • 31
  • 99
  • 140
29
votes
2 answers

What precisely does the %g printf specifier mean?

The %g specifier doesn't seem to behave in the way that most sources document it as behaving. According to most sources I've found, across multiple languages that use printf specifiers, the %g specifier is supposed to be equivalent to either %f or…
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
29
votes
8 answers

understanding the dangers of sprintf(...)

OWASP says: "C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking." sprintf writes formatted data to string int sprintf ( char * str, const char *…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
29
votes
7 answers

How to calculate the length of output that sprintf will generate?

Goal: serialize data to JSON. Issue: i cant know beforehand how many chars long the integer is. i thought a good way to do this is by using sprintf() size_t length = sprintf(no_buff, "{data:%d}",12312); char *buff = malloc(length); snprintf(buff,…
alknows
  • 1,972
  • 3
  • 22
  • 26
29
votes
3 answers

What's the difference between printf("%s"), printf("%ls"), wprintf("%s"), and wprintf("%ls")?

Consider this sample program: #include #include #include int main() { std::string narrowstr = "narrow"; std::wstring widestr = L"wide"; printf("1 %s \n", narrowstr.c_str()); printf("2 %ls \n",…
Display Name
  • 947
  • 2
  • 10
  • 18
29
votes
3 answers

How do I properly 'printf' an integer and a string in C?

I have the following code: char *s1, *s2; char str[10]; printf("Type a string: "); scanf("%s", str); s1 = &str[0]; s2 = &str[2]; printf("%s\n", s1); printf("%s\n", s2); When I run the code, and enter the input "A 1" as follow: Type a string: A…
user1420474
  • 301
  • 1
  • 3
  • 7
28
votes
4 answers

Execution of printf() and Segmentation Fault

#include int main() { char *name = "Vikram"; printf("%s",name); name[1]='s'; printf("%s",name); return 0; } There is no output printed on terminal and just get segmentation fault. But when I run it in GDB, I get…
Vikram
  • 1,999
  • 3
  • 23
  • 35
28
votes
4 answers

Printf a buffer of char with length in C

I have a buffer which I receive through a serial port. When I receive a certain character, I know a full line has arrived, and I want to print it with printf method. But each line has a different length value, and when I just go with: printf("%s",…
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207
28
votes
14 answers

Why is String's format(Object... args) defined as a static method?

I wonder why Java 5 and above provide a printf-style formatter using a static method in class String like this: public static String format(String format, Object... args) instead of public String format(Object... args) so that we can write…
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
28
votes
2 answers

How can I use C++20 std::format?

C++20 introduces std::format. What are the advantages over printf or std::cout? How can I use it and someone give an example of it?
Mert Köklü
  • 2,183
  • 2
  • 16
  • 20
28
votes
13 answers

String.Format for C++

Looking for an implementation for C++ of a function like .NET's String.Format. Obviously there is printf and it's varieties, but I'm looking for something that is positional as in: String.Format("Hi there {0}. You are {1} years old. How does…
DougN
  • 4,407
  • 11
  • 56
  • 81
28
votes
5 answers

What is the underlying difference between printf(s) and printf("%s", s)?

The question is plain and simple, s is a string, I suddenly got the idea to try to use printf(s) to see if it would work and I got a warning in one case and none in the other. char* s = "abcdefghij\n"; printf(s); // Warning raised with gcc…
Mikael
  • 969
  • 12
  • 24
28
votes
7 answers

printf("%p") and casting to (void *)

In a recent question, someone mentioned that when printing a pointer value with printf, the caller must cast the pointer to void *, like so: int *my_ptr = .... printf("My pointer is: %p", (void *)my_ptr); For the life of me I can't figure out why.…
zmbq
  • 38,013
  • 14
  • 101
  • 171
28
votes
2 answers

(GCC) Dollar sign in printf format string

I've seen the following line in a source code written in C: printf("%2$d %1$d", a, b); What does it mean?
polkovnikov.ph
  • 6,256
  • 6
  • 44
  • 79