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
140
votes
9 answers

Using colors with printf

When written like this, it outputs text in blue: printf "\e[1;34mThis is a blue text.\e[0m" But I want to have format defined in printf: printf '%-6s' "This is text" Now I have tried several options how to add color, with no success: printf '%-6s'…
Jernej Jerin
  • 3,179
  • 9
  • 37
  • 53
138
votes
16 answers

Avoid trailing zeroes in printf()

I keep stumbling on the format specifiers for the printf() family of functions. What I want is to be able to print a double (or float) with a maximum given number of digits after the decimal point. If I use: printf("%1.3f",…
Gorpik
  • 10,940
  • 4
  • 36
  • 56
134
votes
6 answers

Using printf with a non-null terminated string

Suppose you have a string which is NOT null terminated and you know its exact size, so how can you print that string with printf in C? I recall such a method but I can not find out now...
whoi
  • 3,281
  • 7
  • 36
  • 44
132
votes
13 answers

Padding characters in printf

I am writing a bash shell script to display if a process is running or not. So far, I got this: printf "%-50s %s\n" $PROC_NAME [UP] The code gives me this output: JBoss [DOWN] GlassFish …
cordish
  • 1,321
  • 2
  • 9
  • 3
131
votes
3 answers

How does the below program output `C89` when compiled in C89 mode and `C99` when compiled in C99 mode?

I've found this C program from the web: #include int main(){ printf("C%d\n",(int)(90-(-4.5//**/ -4.5))); return 0; } The interesting thing with this program is that when it is compiled and run in C89 mode, it prints C89 and…
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
128
votes
8 answers

Printing hexadecimal characters in C

I'm trying to read in a line of characters, then print out the hexadecimal equivalent of the characters. For example, if I have a string that is "0xc0 0xc0 abc123", where the first 2 characters are c0 in hex and the remaining characters are abc123…
Rayne
  • 14,247
  • 16
  • 42
  • 59
128
votes
4 answers

printf format specifiers for uint32_t and size_t

I have the following size_t i = 0; uint32_t k = 0; printf("i [ %lu ] k [ %u ]\n", i, k); I get the following warning when compiling: format ‘%lu’ expects type ‘long unsigned int’, but argument has type ‘uint32_t’ When I ran this using splint I…
ant2009
  • 27,094
  • 154
  • 411
  • 609
125
votes
5 answers

The "backspace" escape character '\b': unexpected behavior?

So I'm finally reading through K&R, and I learned something within the first few pages, that there is a backspace escape character, \b. So I go to test it out, and there is some very odd behavior: #include main () { printf("hello…
OregonTrail
  • 8,594
  • 7
  • 43
  • 58
120
votes
6 answers

How to format strings using printf() to get equal length in the output

I have two functions, one which produces messages like Starting initialization... and another which checks return codes and outputs "Ok", "Warning" or "Error". However, the output that is produced is of the different length: Starting…
psihodelia
  • 29,566
  • 35
  • 108
  • 157
117
votes
5 answers

Python's many ways of string formatting — are the older ones (going to be) deprecated?

Python has at least six ways of formatting a string: In [1]: world = "Earth" # method 1a In [2]: "Hello, %s" % world Out[2]: 'Hello, Earth' # method 1b In [3]: "Hello, %(planet)s" % {"planet": world} Out[3]: 'Hello, Earth' # method 2a In [4]:…
gerrit
  • 24,025
  • 17
  • 97
  • 170
111
votes
1 answer

How does Haskell printf work?

Haskell's type safety is second to none only to dependently-typed languages. But there is some deep magic going on with Text.Printf that seems rather type-wonky. > printf "%d\n" 3 3 > printf "%s %f %d" "foo" 3.3 3 foo 3.3 3 What is the deep magic…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
109
votes
13 answers

Integer ASCII value to character in BASH using printf

Character to value works: $ printf "%d\n" \'A 65 $ I have two questions, the first one is most important: How do I take 65 and turn it into A? \'A converts an ASCII character to its value using printf. Is the syntax specific to printf or is it…
user14070
108
votes
5 answers

In C can a long printf statement be broken up into multiple lines?

I have the following statement: printf("name: %s\targs: %s\tvalue %d\tarraysize %d\n", sp->name, sp->args, sp->value, sp->arraysize); I want to break it up. I tried the following but it doesn't work. printf("name: %s\t args: %s\t value…
neuromancer
  • 53,769
  • 78
  • 166
  • 223
108
votes
9 answers

Clean code to printf size_t in C++ (or: Nearest equivalent of C99's %z in C++)

I have some C++ code that prints a size_t: size_t a; printf("%lu", a); I'd like this to compile without warnings on both 32- and 64-bit architectures. If this were C99, I could use printf("%z", a);. But AFAICT %z doesn't exist in any standard C++…
Justin L.
  • 3,957
  • 3
  • 31
  • 29
107
votes
11 answers

Why is printf with a single argument (without conversion specifiers) deprecated?

In a book that I'm reading, it's written that printf with a single argument (without conversion specifiers) is deprecated. It recommends to substitute printf("Hello World!"); with puts("Hello World!"); or printf("%s", "Hello World!"); Can someone…
StackUser
  • 1,530
  • 3
  • 13
  • 17