By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,330 Members | 1,862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,330 IT Pros & Developers. It's quick & easy.

printf() function

P: n/a
hello, i have a problem using printf() function, would u please help me?

i have a program to print numbers on screen, each number occupies five
spaces and the range of the number is from -9 to 9.
my question is, how can i place the number in the middle of the five spaces?
thanks!

here is my example (allow me to use underline "_" to represent space " ")

"__1__" <==positive number at the third position
"__-4_" <==the negative symbol at the third position, and the number at
the fourth position
Nov 12 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
babyip wrote:
hello, i have a problem using printf() function, would u please help me?

i have a program to print numbers on screen, each number occupies five
spaces and the range of the number is from -9 to 9.
my question is, how can i place the number in the middle of the five spaces?
thanks!

here is my example (allow me to use underline "_" to represent space " ")

"__1__" <==positive number at the third position
"__-4_" <==the negative symbol at the third position, and the number at
the fourth position

#include <stdio.h>
int main(void) {
int i;
for (i = -9; i < 10; ++i)
printf(" %d%s\n", i, i < 0 ? " " : " ");
return 0;
}

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.