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
52
votes
8 answers

sprintf() with automatic memory allocation?

I'm searching for a sprintf()-like implementation of a function that automatically allocates required memory. So I want to say char *my_str = dynamic_sprintf("Hello %s, this is a %.*s nice %05d string", a, b, c, d); and my_str receives the address…
the-shamen
  • 521
  • 1
  • 4
  • 3
52
votes
9 answers

mixing cout and printf for faster output

After performing some tests I noticed that printf is much faster than cout. I know that it's implementation dependent, but on my Linux box printf is 8x faster. So my idea is to mix the two printing methods: I want to use cout for simple prints, and…
Jabba
  • 19,598
  • 6
  • 52
  • 45
51
votes
2 answers

Problem with System.out.printf command in Java

I am new to programming so I apologize beforehand. I am running a program and am having a problem with the System.out.printf method in Java. I know everything else in the program is working correctly (tested it). However this does not seem to…
Spencer
  • 21,348
  • 34
  • 85
  • 121
51
votes
8 answers

sprintf() without trailing null space in C

Is there a way to use the C sprintf() function without it adding a '\0' character at the end of its output? I need to write formatted text in the middle of a fixed width string.
zaratustra
  • 8,148
  • 8
  • 36
  • 42
50
votes
1 answer

How does printf and co differentiate between float and double

Since it isn't strongly typed I thought it just picked the right memory size and interpreted it based on the type of argument. But float and double both use %f and they are different sizes. P.S. I can see how promotion via copying the value to a…
Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141
49
votes
3 answers

Scanf/Printf double variable C

Let's say I have this following bit of code in C: double var; scanf("%lf", &var); printf("%lf", var); printf("%f", var); It reads from stdin variable 'var' and then prints twice in stdout 'var'. I understand that's how you read a double variable…
Dragos Rizescu
  • 3,380
  • 5
  • 31
  • 42
48
votes
3 answers

Use printf to format floats without decimal places if only trailing 0s

Is it possible to format a float in C to only show up to 2 decimal places if different from 0s using printf? Ex: 12 => 12 12.1 => 12.1 12.12 => 12.12 I tried using: float f = 12; printf("%.2f", f) but I get 12 => 12.00 12.1 => 12.10 12.12 => 12.12
epignosisx
  • 6,152
  • 2
  • 30
  • 31
48
votes
2 answers

How to print binary number via printf

Possible Duplicate: Is there a printf converter to print in binary format? Here is my program #include int main () { int i,a=2; i=~a; printf("a=%d\ni=%d\n",a,i); return 0; } The output is a=2 i=-3 I want this to print…
Registered User
  • 5,173
  • 16
  • 47
  • 73
48
votes
3 answers

SPRINTF in shell scripting?

I have an auto-generated file each day that gets called by a shell script. But, the problem I'm facing is that the auto-generated file has a form of: FILE_MM_DD.dat ... where MM and DD are 2-digit month and day-of-the-month strings. I did some…
Manu R
  • 806
  • 1
  • 8
  • 13
48
votes
4 answers

Format specifier %02x

I have a simple program : #include int main() { long i = 16843009; printf ("%02x \n" ,i); } I am using %02x format specifier to get 2 char output, However, the output I am getting is: 1010101 while I am expecting it to…
user2717225
  • 491
  • 1
  • 4
  • 5
47
votes
3 answers

warning: format not a string literal and no format arguments

I want to remove the warning that i get on this line of the code, FILE *fil; char *imp; (...) fprintf(fil,imp); the thing is when i do this it writes on the file exactly what i want, but if i apply the format %s it doesn't, like this fprintf(fil,…
Unzi
  • 509
  • 1
  • 5
  • 5
46
votes
5 answers

What do \t and \b do?

I expect this simple line of code printf("foo\b\tbar\n"); to replace "o" with "\t" and to produce the following output fo bar (assuming that tab stop occurs every 8 characters). On the contrary I get foo bar It seems that my shell…
cimere
  • 667
  • 1
  • 8
  • 22
46
votes
5 answers

Convert string to hexadecimal on command line

I'm trying to convert "Hello" to 48 65 6c 6c 6f in hexadecimal as efficiently as possible using the command line. I've tried looking at printf and google, but I can't get anywhere. Any help greatly appreciated. Many thanks in advance,
Eamorr
  • 9,872
  • 34
  • 125
  • 209
46
votes
7 answers

padding with sprintf

I have a dummy question. I would like to print an integer into a buffer padding with 0 but I cannot sort it out the sprintfformat. I am trying the following char buf[31]; int my_val = 324; sprintf( buf, "%d030", my_val ); hoping to have the…
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
46
votes
6 answers

what do these symbolic strings mean: %02d %01d?

I'm looking at a code line similar to: sprintf(buffer,"%02d:%02d:%02d",hour,minute,second); I think the symbolic strings refer to the number of numeric characters displayed per hour, minute etc - or something like that, I am not entirely certain.…
dRef90
  • 471
  • 1
  • 4
  • 4