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
57
votes
7 answers

Does C have a string type?

I have recently started programming in C, coming from Java and Python. Now, in my book I have noticed that to make a "Hello World" program, the syntax is something like this: char message[10] strcpy(message, "Hello, world!") printf("%s\n",…
rel-s
  • 6,108
  • 11
  • 38
  • 50
56
votes
4 answers

What primitive data type is time_t?

I do not know the data type of time_t. Is it a float double or something else? Because if I want to display it I need the tag that corresponds with it for printf. I can handle the rest from there for displaying time_t but I need to know the data…
thyrgle
56
votes
2 answers

What's the meaning of the %m formatting specifier?

The output for this code printed out ‘Success’. printf("%m\n");
Manuel
  • 976
  • 3
  • 9
  • 21
55
votes
2 answers

Is there a way to reuse an argument in fmt.Printf?

I have a situation where I want to use my printf argument twice. fmt.Printf("%d %d", i, i) Is there a way to tell fmt.Printf to just reuse the same i? fmt.Printf("%d %d", i)
425nesp
  • 6,936
  • 9
  • 50
  • 61
55
votes
2 answers

What is the correct printf specifier for printing pid_t

I'm currently using a explicit cast to long and using %ld for printing pid_t, is there a specifier such as %z for size_t for pid_t? If not what the best way of printing pid_t?
Bilal Syed Hussain
  • 8,664
  • 11
  • 38
  • 44
55
votes
4 answers

unsigned long long type printing in hexadecimal format

I am trying to print out an unsigned long long like this: printf("Hex add is: 0x%ux ", hexAdd); but I am getting type conversion errors since I have an unsigned long long.
Paul the Pirate
  • 767
  • 3
  • 7
  • 10
54
votes
2 answers

printf string, variable length item

#define SIZE 9 int number=5; char letters[SIZE]; /* this wont be null-terminated */ ... char fmt_string[20]; sprintf(fmt_string, "%%d %%%ds", SIZE); /* fmt_string = "%d %9d"... or it should be */ printf(fmt_string, number, letters); Is there a…
William Entriken
  • 37,208
  • 23
  • 149
  • 195
54
votes
4 answers

C complex number and printf

How to print ( with printf ) complex number? For example, if I have this code: #include #include int main(void) { double complex dc1 = 3 + 2*I; double complex dc2 = 4 + 5*I; double complex result; result = dc1…
gameboy
  • 1,505
  • 3
  • 15
  • 16
54
votes
2 answers

Extra leading zeros when printing float using printf?

I'd like to be able to write a time string that looks like this: 1:04:02.1 hours using printf. When I try to write something like this: printf("%d:%02d:%02.1f hours\n", 1, 4, 2.123456); I get: 1:04:2.1 hours Is it possible to add leading zeros…
shoosh
  • 76,898
  • 55
  • 205
  • 325
54
votes
3 answers

Alternatives to type casting when formatting NS(U)Integer on 32 and 64 bit architectures?

With the 64 bit version of iOS we can't use %d and %u anymore to format NSInteger and NSUInteger. Because for 64 bit those are typedef'd to long and unsigned long instead of int and unsigned int. So Xcode will throw warnings if you try to format…
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
54
votes
3 answers

C Programming: Forward variable argument list

I'm trying to write a function that accepts a variable number of parameters like printf, does some stuff, then passes the variable list to printf. I'm not sure how to do this, because it seems like it would have to push them onto the stack.…
Joshua Cheek
  • 30,436
  • 16
  • 74
  • 83
54
votes
4 answers

System.out.printf vs System.out.format

Are System.out.printf and System.out.format totally the same or perhaps they differ in somehow?
Rollerball
  • 12,618
  • 23
  • 92
  • 161
53
votes
3 answers

F# printf string

Im puzzled let test = "aString" let callMe = printfn test Why isn't this working? Throws below error at compile time: The type 'string' is not compatible with the type 'Printf.TextWriterFormat<'a>' This works fine: printfn "aString"
CodeMonkey
  • 3,418
  • 4
  • 30
  • 53
53
votes
9 answers

What does %s and %d mean in printf in the C language?

I don't understand what the %s and d% do in this C code: for (i=0;i
Simon Kiely
  • 5,880
  • 28
  • 94
  • 180
52
votes
7 answers

Determining sprintf buffer size - what's the standard?

When converting an int like so: char a[256]; sprintf(a, "%d", 132); what's the best way to determine how large a should be? I assume manually setting it is fine (as I've seen it used everywhere), but how large should it be? What's the largest int…
Dominic Bou-Samra
  • 14,799
  • 26
  • 100
  • 156