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

printf and long double

I am using the latest gcc with Netbeans on Windows. Why doesn't long double work? Is the printf specifier %lf wrong? Code: #include int main(void) { float aboat = 32000.0; double abet = 5.32e-5; long double dip = 5.32e-5; …
gameboy
  • 1,505
  • 3
  • 15
  • 16
68
votes
11 answers

Why do I have to specify data type each time in C to printf() and scanf()?

As you can see from the code snippet below, I have declared one char variable and one int variable. When the code gets compiled, it must identify the data types of variables str and i. Why do I need to tell again during scanning my variable that…
Akki619
  • 2,386
  • 6
  • 26
  • 56
67
votes
2 answers

How to use printf with NSString

I need to use something like NSLog but without the timestamp and newline character, so I'm using printf. How can I use this with NSString?
node ninja
  • 31,796
  • 59
  • 166
  • 254
67
votes
2 answers

How to printf a memory address in C

My code is: #include #include void main() { char string[10]; int A = -73; unsigned int B = 31337; strcpy(string, "sample"); // printing with different formats printf("[A] Dec: %d, Hex: %x,…
varlotbarnacle
  • 821
  • 1
  • 6
  • 7
67
votes
4 answers

Arduino sprintf float not formatting

I have this arduino sketch, char temperature[10]; float temp = 10.55; sprintf(temperature,"%f F", temp); Serial.println(temperature); temperature prints out as ? F Any thoughts on how to format this float? I need it to be a char string.
Mistergreen
  • 1,052
  • 1
  • 8
  • 16
66
votes
1 answer

'printf' with leading zeros in C

I have a floating point number such as 4917.24. I'd like to print it to always have five characters before the decimal point, with leading zeros, and then three digits after the decimal place. I tried printf("%05.3f", n) on the embedded system I'm…
fred basset
  • 9,774
  • 28
  • 88
  • 138
65
votes
6 answers

Should I use %i or %d to print an integer in C using printf()?

I am just learning C and I have a little knowledge of Objective-C due to dabbling in iOS development. In Objective-C, I was using NSLog(@"%i", x); to print the variable x to the console. However, I have been reading a few C tutorials and they are…
Dummy Code
  • 1,858
  • 4
  • 19
  • 38
64
votes
8 answers

Which of sprintf/snprintf is more secure?

I wish to know which of these two options is the more secure one to use: #define MAXLEN 255 char buff[MAXLEN + 1] sprintf(buff, "%.*s", MAXLEN, name) snprintf(buff, MAXLEN, "%s", name) My understanding is that both are same. Please suggest.
Arpit
  • 4,259
  • 10
  • 38
  • 43
60
votes
1 answer

sprintf - repeating arguments

I want to format a string with sprintf but repeating many times an argument. see.. $str = "Str 1: %s - Str 2: %s - Str 2 again: %s"; Considering that string to format, I want to repeat the second arg two times. echo sprintf($str, "I'm string 1",…
user898741
60
votes
6 answers

What does the %*s format specifier mean?

In some code that I have to maintain, I have seen a format specifier %*s . Can anybody tell me what this is and why it is used? An example of its usage is like: fprintf(outFile, "\n%*s", indent, "");
Aamir
  • 14,882
  • 6
  • 45
  • 69
59
votes
4 answers

How do I print some text in bash and pad it with spaces to a certain width?

I'm echoing some text in a bash script with a variable in it, and want to pad that variable so it will always have the appropriate ammount of spaces to the right to keep the rest of the text aligned. Here's an example of what I want: Echoing random…
PHLAK
  • 22,023
  • 18
  • 49
  • 52
59
votes
9 answers

How to use C++ std::ostream with printf-like formatting?

I am learning C++. cout is an instance of std::ostream class. How can I print a formatted string with it? I can still use printf, but I want to learn a proper C++ method which can take advantage of all C++ benefits. I think this should be possible…
eonil
  • 83,476
  • 81
  • 317
  • 516
58
votes
4 answers

How can I display hexadecimal numbers in C?

I have a list of numbers as below: 0, 16, 32, 48 ... I need to output those numbers in hexadecimal as: 0000,0010,0020,0030,0040 ... I have tried solution such as: printf("%.4x", a); // Where a is an integer But the result that I got was: 0000,…
user188276
57
votes
7 answers

Can I display the value of an enum with printf()?

Is there a one-liner that lets me output the current value of an enum?
Pieter
  • 31,619
  • 76
  • 167
  • 242
57
votes
4 answers

Correct printf format specifier for size_t: %zu or %Iu?

I want to print out the value of a size_t variable using printf in C++ using Microsoft Visual Studio 2010 (I want to use printf instead of << in this specific piece of code, so please no answers telling me I should use << instead). According to the…
Patrick
  • 23,217
  • 12
  • 67
  • 130