format-specifiers refer to the syntax of the format string parameter of the *printf functions in C/C++, allowing special formatting of arguments.
Questions tagged [format-specifiers]
472 questions
584
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,851
- 3
- 15
- 4
462
votes
14 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
- 30,272
- 27
- 92
- 113
445
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
- 75,521
- 93
- 229
- 319
228
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,690
- 10
- 42
- 52
163
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?

StevenWang
- 3,625
- 4
- 30
- 40
159
votes
10 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
- 124,188
- 49
- 220
- 267
107
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,530
- 3
- 13
- 17
105
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
- 16,347
- 11
- 61
- 88
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…

R.. GitHub STOP HELPING ICE
- 208,859
- 35
- 376
- 711
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
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
57
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
- 6,332
- 9
- 48
- 78
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
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
31
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
- 5,732
- 4
- 37
- 65