Questions tagged [format-specifiers]

format-specifiers refer to the syntax of the format string parameter of the *printf functions in C/C++, allowing special formatting of arguments.

418 questions
523
votes
5 answers

Correct format specifier for double in printf

What is the correct format specifier for double in printf? Is it %f or is it %lf? I believe it's %f, but I am not sure. Code sample #include int main() { double d = 1.4; printf("%lf", d); // Is this wrong? }
Leopard
  • 5,241
  • 3
  • 13
  • 4
403
votes
13 answers

How do you format an unsigned long long int using printf?

#include int main() { unsigned long long int num = 285212672; //FYI: fits in 29 bits int normalInt = 5; printf("My number is %d bytes wide and its value is %ul. A normal number is %d.\n", sizeof(num), num, normalInt); …
andrewrk
  • 27,002
  • 25
  • 87
  • 105
374
votes
7 answers

How to printf "unsigned long" in C?

I can never understand how to print unsigned long datatype in C. Suppose unsigned_foo is an unsigned long, then I try: printf("%lu\n", unsigned_foo) printf("%du\n", unsigned_foo) printf("%ud\n", unsigned_foo) printf("%ll\n",…
bodacydo
  • 63,809
  • 83
  • 206
  • 303
211
votes
4 answers

What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf)

What is the difference between %d and %i when used as format specifiers in printf and scanf?
Ayoub M.
  • 4,425
  • 8
  • 38
  • 50
154
votes
9 answers

How should I print types like off_t and size_t?

I'm trying to print types like off_t and size_t. What is the correct placeholder for printf() that is portable? Or is there a completely different way to print those variables?
Georg Schölly
  • 116,521
  • 48
  • 204
  • 258
125
votes
4 answers

What does "%.*s" mean in printf?

I got a code snippet in which there is a printf("%.*s\n") what does the %.*s mean?
Shaobo Wang
  • 3,144
  • 4
  • 23
  • 40
103
votes
11 answers

Why is printf with a single argument (without conversion specifiers) deprecated?

In a book that I'm reading, it's written that printf with a single argument (without conversion specifiers) is deprecated. It recommends to substitute printf("Hello World!"); with puts("Hello World!"); or printf("%s", "Hello World!"); Can someone…
StackUser
  • 1,264
  • 2
  • 10
  • 14
92
votes
3 answers

Platform independent size_t Format specifiers in c?

I want to print out a variable of type size_t in C but it appears that size_t is aliased to different variable types on different architectures. For example, on one machine (64-bit) the following code does not throw any warnings: size_t size =…
Ethan Heilman
  • 14,869
  • 10
  • 58
  • 88
62
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…
50
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
  • 13,616
  • 6
  • 40
  • 65
46
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
  • 22,097
  • 9
  • 57
  • 125
45
votes
2 answers

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

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

Read no more than size of string with scanf()

Edit: for my class I have to use scanf. So recommending other ways of input is not the solution I am looking for (if there is one that involves scanf). If I am reading in user input for a small project (for example, a game). Lets say I ask would…
MrHappyAsthma
  • 5,675
  • 8
  • 45
  • 76
36
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
  • 371
  • 1
  • 3
  • 5
28
votes
3 answers

What does an asterisk in a scanf format specifier mean?

So I stumbled across this code and I haven't been able to figure out what the purpose of it is, or how it works: int word_count; scanf("%d%*c", &word_count); My first thought was that %*d was referencing a char pointer or disallowing word_count…
Joel
  • 3,791
  • 1
  • 25
  • 41
1
2 3
27 28