[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


%06.6ld format specifier- what does it do?

Posted on 2006-05-08
Medium Priority
Last Modified: 2012-05-05
In the following program, I was trying to
experiment with the %06.6ld format specifier.
I was thinking that even if the number "passed"
to it was > 6 digits, it would cut off at 6, unlike
the %06ld specifier.

void main()
        char buff[26];

        long num;

        printf("Enter a long:\n");
        gets(buff); num=atol(buff);
        printf("num(06ld)=|%06ld|\n", num);
        printf("num(06.6ld)=|%06.6ld|\n", num);


that was not the case. For the number 1234567,

Enter a long:

So...does this mean that %06.6ld has no effect?
or, does it do something else?

St eve
Question by:Stephen Kairys
LVL 11

Assisted Solution

cup earned 200 total points
ID: 16633470
If your number is bigger than the format specified, the format is ignored and the machine normally does the best it can to give you the value that it has.

 If I told you to print 6 digits and gave you an 8 digit number, would you print the first 6, middle 6 or last 6?

If you only want 6 digits, no matter what the size, sprintf it to a character array first then truncate the array accordingly.
LVL 46

Accepted Solution

Kent Olsen earned 240 total points
ID: 16633480
Hi stevefromc,

This quirk has only come up in the C forum a couple of times.  :)  One of the little known behaviors of the 'd' format specifier is that it guarantees that all of the significant digits of the integer will be displayed.  Regardless of the modifiers, if the number contains 7 digits, 7 will be displayed even if the format spec calls for less.  If you want less displayed, you'll have to perform modulo arithmetic to ensure that the value is not longer than the number of digits that you want.

         printf("num(06.6ld)=|%06.6ld|\n", mod(num, 1000000));

Strings are a different matter.  The C library will truncate the string to the desired length, but not so with integers.

Good Luck!

Author Comment

by:Stephen Kairys
ID: 16633567
OK, so my compiler is not "buggy" in that it does not truncate to 6. Good to know. That being said, here's the crux of the matter:

I was actually MORE concerned about if
a 6-digit number would be handled correctly i.e.
if I pass it "123456" (intending that it display as "123456"),
will C behave "properly", or will it, under some
cases, cause MORE than 6 digits to be ouputted?
Reason I ask is that I'm tracking down a possible
memory oerwrite and wondered if

char buff[7];
sprintf(buff, "%06.6ld", some_number)

could ever overflow BUFF for a number that
I know won't ever exceed 999999....I was thinking
that if this format spec were somehow "illegal" that
I could be asking for trouble... :)


Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 240 total points
ID: 16633716
Hi stevefromc,

Well, if some_number can ever exceed 999999 then buff will certainly overflow.  But if you're sure that it can't exceed 6 digits then you're safe.  On the other hand, memory is cheap.  Defining the buffer as char buff[20] doesn't hurt a thing.

One of the little used features of sprintf() is that it returns the number of characters written to the output string.  In this case it would be trivial to check the return value and spit out an error message if it exceeds the buffer size.  But that does take CPU cycles and an entire program's worth may not be worth the trouble.  It's faster and more efficient to arbitrarily define a larger buffer.

That said, the return value of sprintf() is extraordinarily convenient for some functions.  Imaging trying to pack comma delimited values into a buffer when you don't know how many values there are:

  int    Array[100];
  char Buffer[1000];
  int    Length;
  int    idx;

  Length = sprintf (Buffer, "%d", Array[0]);
  for (idx = 1; Array[idx]; ++idx)
    Length += sprintf (Buffer + Length, ",%d", Array[idx]);

Array[Length] is always the next available character in the buffer so it's easy to append the new value without having to hunt for the end-of-string every pass.

Sorry for getting off track,
LVL 16

Assisted Solution

PaulCaswell earned 200 total points
ID: 16633733
Hi stevefromc,

If you can GUARANTEE that the number will never go beyond 6 digits then you are fine. I personally would surround it with 'if ( number < 1000000 )' and a comment, perhaps in capitals with stars around it, explaining the problem. Remember the programmers who are yet to work on your code. :-)

'scanf' is a different matter, scanf ( ..."%6.6ld"... ) will read only up to 6 characters and no more!


Author Comment

by:Stephen Kairys
ID: 16633995
OK, I think i'm good here. Thank you all for your help.

And, Kent, no apology neded.. very interesting (and useful) info about sprintf()!! Thank you.

Thanks as well to cup and Paul. Points split.

Author Comment

by:Stephen Kairys
ID: 16634031
..and increasing the total pts.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question