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

  • Home
  • SEARCH
  • 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 174585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:32:55+00:00 2026-05-11T13:32:55+00:00

I need to align a series of numbers in C with printf() like this

  • 0

I need to align a series of numbers in C with printf() like this example:

-------1 -------5 ------50 -----100 ----1000 

Of course, there are numbers between all those but it’s not relevant for the issue at hand… Oh, consider the dashes as spaces, I used dashes so it was easier to understand what I want.

I’m only able to do this:

----1--- ----5--- ----50-- ----100- ----1000 

Or this:

---1 ---5 --50 -100 1000 

But none of this is what I want and I can’t achieve what is displayed on the first example using only printf(). Is it possible at all?

EDIT:
Sorry people, I was in a hurry and didn’t explain myself well… My last example and all your suggestions (to use something like ‘%8d’) do not work because, although the last number is 1000 it doesn’t necessarily go all the way to 1000 or even 100 or 10 for that matter.

No matter the number of digits to be displayed, I only want 4 leading spaces at most for the largest number. Let’s say I have to display digits from 1 to 1000 (A) and 1 to 100 (B) and I use, for both, ‘%4d’, this would be the output:

A:

---1 .... 1000 

Which is the output I want…

B:

---1 .... -100 

Which is not the output I want, I actually want this:

--1 ... 100 

But like I said, I don’t know the exact number of numbers I have to print, it can have 1 digit, it can have 2, 3 or more, the function should be prepared for all. And I want four extra additional leading spaces but that’s not that relevant.

EDIT 2: It seems that what I want, the way I need it, it’s not possible (check David Thornley and Blank Xavier answers and my comments). Thank you all for your time.

  • 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. 2026-05-11T13:32:56+00:00Added an answer on May 11, 2026 at 1:32 pm

    Looking this up in my handy Harbison & Steele….

    Determine the maximum width of fields.

    int max_width, value_to_print; max_width = 8; value_to_print = 1000; printf('%*d\n', max_width, value_to_print); 

    Bear in mind that max_width must be of type int to work with the asterisk, and you’ll have to calculate it based on how much space you’re going to want to have. In your case, you’ll have to calculate the maximum width of the largest number, and add 4.

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

Sidebar

Related Questions

Need to vertically align images within a containing box like this: <span class=image-box><img src=/1_thumb.jpg></span>
This is my markup, I need to align gray label under the blue label
Need to apply a filter to a file like this: TUPAC_0006:1:1:2554:2356#0/1 0 * 0
I have this page (code below) which contains a series of 5 checkboxes, all
I have a simple css3 button and I need to align this button inside
I figured this would be simple, I need to align both of the inside
I need to align the text inside the p tag to vertically middle. Since
I need to align two divs next to each other, so that each contains
I need help to align CSS buttons. I tried many different variations and I
I need to list all filenames which is having alien.digits digits can be anytime

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.