Understanding printf() and its Formatting Options

  • Thread starter transgalactic
  • Start date
In summary, the conversation discusses the use of the %05.5f specifier in the printf function, which specifies a total field of 5 characters with zero padding and 5 digits after the decimal point. The output will also include a space and a comma. The other 5 after the decimal point means to print the float number in position 5 after the decimal point. The conversation also mentions the use of flags in the printf function and provides a reference for further information.
  • #1
transgalactic
1,395
0
what mean this part of the printf

Code:
 printf("calloc1[%d] holds %05.5f, ", i, calloc1[i]);

Code:
%05.5f, "

there is %05.5f
whats that?

and there is a space after a comma
what this thing does?
 
Last edited:
Technology news on Phys.org
  • #2
You first question: %05.5f means that a total field of 5 characters,and use Zero pad, and the last 5 will hold the decimal part.
Google search give a lot of result:
see here: http://www.cplusplus.com/reference/clibrary/cstdio/printf.html

"a space and a comma" means this will output directly.

Can you test your code?
 
  • #3
this is th output of the printf line:
Code:
calloc1[0] holds 0.00000, malloc1[0] holds -431602080.00000
i can't see your words in this output

can you mark those part or something?
 
  • #4
calloc1[0] holds 0.00000
the dot "." For e, E and f specifiers: this is the number of digits to be printed after the decimal point.
so, there are five zeros.
 
  • #5
ok 0.5 means persition till the 5th place

but there 5.5

whats the other 5 for?
 
  • #6
The First "5" means : Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

All the words I copied from the referenced web page I suggest.
 
  • #7
so the first 5 means that we need to print at least five chars in this float number
and the second 5 means to print the float number in persition 5 place after the point
 
  • #8
Yes, you are right.
 
  • #9
there is a space after a comma
what this thing does?
 
  • #10
it says there

%[flags][width][.precision][length]specifier

but in my printf i get the flag at the last place
 
  • #11

What is printf() and why is it important?

Printf() is a function in the C programming language that is used to print formatted output to the screen or other output devices. It is important because it allows for precise control over the formatting of the output, making it easier to read and understand for both the programmer and the end user.

What are the basic formatting options in printf()?

The basic formatting options in printf() include %d for integers, %f for floating-point numbers, %c for characters, %s for strings, and %p for pointers. These options allow for the proper conversion and display of different data types.

How do I include special characters in printf() output?

To include special characters in printf() output, you can use the backslash (\) followed by the corresponding escape sequence. For example, \n for a newline, \t for a tab, and \a for an alert sound. You can also use the ASCII code for the character using the %c format specifier.

What is the purpose of precision in printf() formatting?

Precision in printf() formatting refers to the number of digits or characters that will be displayed after the decimal point or in a string. It allows for more control over the output and can be useful for rounding numbers or limiting the length of a string.

How can I align the output in printf()?

You can align the output in printf() by using the flags - for left alignment, + for right alignment, or 0 for zero padding. These can be placed before the width value in the format specifier. For example, %10d will result in a number being displayed in a field of 10 characters, with extra spaces added to the left if necessary.

Similar threads

  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
667
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
16K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top