Thursday, November 20, 2008

write printf( ) so that the width of a field can be specified at runtime

32. How do I write printf( ) so that the width of a field can be specified at runtime?

Ans: This is shown in following code snippet.
main( )
{
int w, no ;
printf ( "Enter number and the width for the
number field:" ) ;
scanf ( "%d%d", &no, &w ) ;
printf ( "%*d", w, no ) ;
}
Here, an '*' in the format specifier in printf( ) indicates that an int value from the argument list should be used for the field width.

No comments:

Blog List