Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6112475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:44:58+00:00 2026-05-23T14:44:58+00:00

I came across some code with a line looking like: fprintf(fd, %4.8f, ptr->myFlt); Not

  • 0

I came across some code with a line looking like:

fprintf(fd, "%4.8f", ptr->myFlt);

Not working with C++ much these days, I read the doc on printf and its ilk, and learned that in this case 4 is the “width”, and 8 is the “precision”. Width was defined as the minimum number of spaces occupied by the output, padding with leading blanks if need be.

That being the case, I can’t understand what the point of a template like “%4.8f” would be,
since the 8 (zero-padded if necessary) decimals after the point would already ensure that the width of 4 was met and exceeded. So, I wrote a little program, in Visual C++:

// Formatting width test

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    printf("Need width when decimals are smaller: >%4.1f<\n", 3.4567);
    printf("Seems unnecessary when decimals are greater: >%4.8f<\n", 3.4567);
    printf("Doesn't matter if argument has no decimal places: >%4.8f<\n", (float)3);

    return 0;
}

which gives the following output:

Need width when decimals are smaller: > 3.5<
Seems unnecessary when decimals are greater: >3.45670000<
Doesn't matter if argument has no decimal places: >3.00000000<

In the first case, the precision is less than width specified, and in fact a leading space is added. When the precision is greater, however, the width seems redundant.

Is there a reason for a format like that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:44:58+00:00Added an answer on May 23, 2026 at 2:44 pm

    The width format specifier only affects the output if the total width of the printed number is less than the specified width. Obviously, this can never happen when the precision is set greater than or equal to the width. So, the width specification is useless in this case.

    Here’s an article from MSDN; the last sentence explains it.

    A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
I was looking through some code and came across a require 'etc' line. Of
I was just looking at some example code and came across a line, I
I was looking at some code and I came across this line: Machine drink[]
I was looking through some code for work, and came across this line: eval(\$element
When looking through some code I took over, I came across this line: my
I came across this line in some code and can't find the syntax defined
Context I came across some code, like this: if( Some_Condition ) throw 0; I
I came across some legacy code that contains a function like this: LPCTSTR returnString()
I am working on someone else's code. I came across a line of code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.