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
78
votes
2 answers

How do I print a non-null-terminated string using printf?

How can I print a non-null-terminated string using printf, assuming that I know the length of the string at runtime?
Mike
  • 23,892
  • 18
  • 70
  • 90
77
votes
7 answers

Strings and character with printf

I was confused with usage of %c and %s in the following C program: #include void main() { char name[] = "siva"; printf("%s\n", name); printf("%c\n", *name); } Output: siva s Why we need to use pointer to display a…
Aspire
  • 921
  • 1
  • 7
  • 15
75
votes
6 answers

find lacks the option -printf, now what?

I have not found a reason why Mac's find does not have the option -printf. Apple normally decides to take options out which are not orthogonal to the other commands? How can you reach the same result as the following command in Mac without…
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
75
votes
3 answers

Getting a weird percent sign in printf output in terminal with C

I have this printf statement at the end of my program: printf("%d", total_candies); total_candies is an int, and while I expect everything to work correctly, along with the actual number, I'm getting a weird percent sign at the end. Can anyone…
rounak
  • 9,217
  • 3
  • 42
  • 59
74
votes
7 answers

What is the purpose of the h and hh modifiers for printf?

Aside from %hn and %hhn (where the h or hh specifies the size of the pointed-to object), what is the point of the h and hh modifiers for printf format specifiers? Due to default promotions which are required by the standard to be applied for…
74
votes
7 answers

Where is `%p` useful with printf?

After all, both these statements do the same thing... int a = 10; int *b = &a; printf("%p\n",b); printf("%08X\n",b); For example (with different addresses): 0012FEE0 0012FEE0 It is trivial to format the pointer as desired with %x, so is there some…
Moeb
  • 10,527
  • 31
  • 84
  • 110
73
votes
6 answers

Is there a sprintf equivalent for node.js

Looking to do output formatting (sprintf type functionality) in node.js, but before I write it myself I was wondering if there's something similar built-in (I've trawled the docs to no avail) or if someone's already written a module. Many thanks
72
votes
6 answers

How to print an unsigned char in C?

I am trying to print char as positive value: char ch = 212; printf("%u", ch); but I get: 4294967252 How I can get 212 in the output?
Anton
  • 1,051
  • 1
  • 9
  • 21
71
votes
1 answer

Code for printf function in C

Possible Duplicate: source code of c/c++ functions I was wondering where I can find the C code that's used so that when I write printf("Hello World!"); in my C programm to know that it has to print that string to STDOUT. I looked in ,…
Rainer Zufall
  • 719
  • 1
  • 6
  • 3
71
votes
4 answers

Printf long long int in C with GCC?

How do I printf long long int and also unsigned long long int in C99 using GCC? I have searched the other posts which suggest to use %lld but it gives these warnings: warning#1: unknown conversion type character 'l' in format [-Wformat]| …
user963241
  • 6,758
  • 19
  • 65
  • 93
70
votes
5 answers

call printf using va_list

void TestPrint(char* format, ...) { va_list argList; va_start(argList, format); printf(format, argList); va_end(argList); } int main() { TestPrint("Test print %s %d\n", "string", 55); return 0; } I need to get: Test print…
Alex F
  • 42,307
  • 41
  • 144
  • 212
70
votes
2 answers

Why use asprintf() instead of sprintf()?

I'm having a hard time understanding why you would need asprintf. Here in the manual it says The functions asprintf() and vasprintf() are analogs of sprintf(3) and vsprintf(3), except that they allocate a string large enough to hold the output…
Brandon Ling
  • 3,841
  • 6
  • 34
  • 49
69
votes
1 answer

Why not use %v to print int and string

I know to print int we can use %d, and string we can use %s but we can still use %v to print them. So what if I always use %v to print them? What issue will happen if I do this?
harrycmfan
  • 1,055
  • 3
  • 9
  • 12
69
votes
8 answers

Why wasn't a specifier for `float` defined in `printf`?

It looks like it could have been, there are (at least in C99) length modifiers that can be applied to int: %hhd, %hd, %ld and %lld mean signed char, short, long and long long. There is even a length modifier applicable to double: %Lf means long…
skyking
  • 13,817
  • 1
  • 35
  • 57
68
votes
2 answers

Set variable text column width in printf

In order to determine the size of the column in C language we use %d. For instance, I can type %3d and it will give me a column of width=3. My problem is that my number after the % is a variable that I receive, so I need something like %xd…
Alaa M.
  • 4,961
  • 10
  • 54
  • 95