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
35
votes
5 answers

Pointer will not work in printf()

Having an issue with printing a pointer out. Every time I try and compile the program below i get the following error: pointers.c:11: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘int *’ I'm obviously missing something simple…
Chris
  • 7,996
  • 11
  • 66
  • 98
35
votes
2 answers

How do I pad a printf to take account of negative signs and variable length numbers?

I'm trying to output some numbers in a log file and I want to pad a load of floats via the printf function to produce: 058.0 020.0 038.0 -050.0 800.0 150.0 100.0 Currently I'm doing this: printf("% 03.1f\n", myVar); ...where myVar is a…
Jon Cage
  • 36,366
  • 38
  • 137
  • 215
34
votes
3 answers

Why is #include not required to use printf()?

Session transcript: > type lookma.c int main() { printf("%s", "no stdio.h"); } > cl lookma.c Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86 Copyright (C) Microsoft Corporation. All rights…
Constantin
  • 27,478
  • 10
  • 60
  • 79
34
votes
4 answers

Format specifiers for implementation-defined types like time_t

I want to make my code more platform-/implementation-independent. I don't know what a time_t will be implemented as on the platform when the code is being compiled. How do I know the type of t to determine what format specifier to use? ... time_t t…
user198736
34
votes
8 answers

Printing chars and their ASCII-code in C

How do I print a char and its equivalent ASCII value in C?
Chris_45
  • 8,769
  • 16
  • 62
  • 73
33
votes
10 answers

Cross platform format string for variables of type size_t?

On a cross platform c/c++ project (Win32, Linux, OSX), I need to use the *printf functions to print some variables of type size_t. In some environments size_t's are 8 bytes and on others they are 4. On glibc I have %zd, and on Win32 I can use %Id.…
twk
  • 16,760
  • 23
  • 73
  • 97
33
votes
2 answers

how can I put a breakpoint on "something is printed to the terminal" in gdb?

I would like to know from where inside a huge application a certain message is printed. The application is so big and old that it uses all conceivable ways of printing text to the terminal; for example printf(), fprintf(stdout, ...) etc. I write to…
martin
  • 1,106
  • 2
  • 9
  • 11
33
votes
1 answer

How do I print a hexadecimal number with leading 0 to have width 2 using sprintf?

I try to convert a number between 0 and 255 to hexadecimal format. If I use sprintf("%X", 1) I get 1, but I need the output always to have width 2 (with leading 0s) instead of one. How can this be done?
Marius Hofert
  • 6,546
  • 10
  • 48
  • 102
32
votes
13 answers

Using floats with sprintf() in embedded C

Guys, I want to know if float variables can be used in sprintf() function. Like, if we write: sprintf(str,"adc_read = %d \n",adc_read); where adc_read is an integer variable, it will store the string "adc_read = 1023 \n" in str (assuming that …
aditya
  • 952
  • 3
  • 16
  • 33
32
votes
4 answers

Double % formatting question for printf in Java

%s is a string in printf, and %d is a decimal I thought...yet when putting in writer.printf("%d dollars is the balance of %s\r\n", bal, nm); ..an exception is thrown telling me that %d != lang.double. Ideas?
D. Spigle
  • 541
  • 2
  • 5
  • 15
32
votes
4 answers

Cross platform/compiler consistent sprintf of floating point numbers

We have a game that needs to be deterministic as it is part of its multiplayer model. We also use Lua, which uses the sprintf internally (the format is %.14g). The problem arises when it prints number like 0.00001. In some cases it prints 1e-05 and…
kovarex
  • 1,766
  • 18
  • 34
32
votes
2 answers

Awk print string with variables

How do I print a string with variables? Trying this awk -F ',' '{printf /p/${3}_abc/xyz/${5}_abc_def/}' file Need this at output /p/APPLE_abc/xyz/MANGO_abc_def/ where ${3} = APPLE and ${5} = MANGO
Glad
  • 321
  • 1
  • 4
  • 7
32
votes
4 answers

Java printf using variable field size?

I'm just trying to convert some C code over to Java and I'm having a little trouble with String.printf. In C, to get a specific width based on a variable, I can use: printf("Current index = %*d\n", sz, index); and it will format the integer to be a…
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
32
votes
10 answers

Centering strings with printf()

By default, printf() seems to align strings to the right. printf("%10s %20s %20s\n", "col1", "col2", "col3"); /* col1 col2 col3 */ I can also align text to the left like this: printf("%-10s %-20s %-20s",…
Pieter
  • 31,619
  • 76
  • 167
  • 242
32
votes
5 answers

How to print multiple variable lines in Java

I'm trying to print the test data used in webdriver test inside a print line in Java I need to print multiple variables used in a class inside a system.out.print function (printf/println/whatever). public String firstname; public String…
Harshini
  • 353
  • 2
  • 6
  • 12