Jump to content

a problem in C pre specifire

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
payam.a

payam.a

    Newbie

  • Members
  • PipPip
  • 12 posts
hello my dear friends;
I was reading part of a C code that i confronted with a question that i have copied and pasted for you.
my problem is in line 10 & 13 below. that I have mentioned them below.
what does a * do in this program? it has just one specifier like %d or %f but in front of it has two value. what has happened to the next one?
thanks if you answer me.


/* varwid.c -- uses variable-width output field */

#include <stdio.h>

int main(void)

{

    unsigned width, precision;

    int number = 256;

    double weight = 242.5;



    printf("What field width?\n");

    scanf("%d", &width);

   10     printf("The number is:%*d:\n", width, number);

    printf("Now enter a width and a precision:\n");

    scanf("%d %d", &width, &precision);

  13      printf("Weight = %*.*f\n", width, precision, weight);

    printf("Done!\n");



    return 0;

}
this is it's output
What field width?

6

The number is : 256:

Now enter a width and a precision:

8 3

Weight = 242.500

Done!

Edited by WingedPanther, 15 June 2008 - 04:22 AM.
add code tags


#2
James

James

    Newbie

  • Members
  • Pip
  • 1 posts
Referencing to this site: cplusplus.com/reference/clibrary/cstdio/printf.html

Looks as if

// %[flags][width][.precision][length]specifier  
// the flag specifier isn't used in either of these
// %*d the * means to take the next argument
// in this case width to printf as size of field
10 printf("The number is:%*d:\n", width, number);

printf("Now enter a width and a precision:\n");

scanf("%d %d", &width, &precision);

// %*.*f... * means same as above take width as size of field
// .* meaning to take the following argument precision to printf 
// to set the precision of the number specified by the variable set by the user
13 printf("Weight = %*.*f\n", width, precision, weight);

Edited by WingedPanther, 15 June 2008 - 04:23 AM.
add code tags


#3
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
The C89 Draft

Quote

An optional decimal integer specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left adjustment flag, described later, has been given) to the field width.

An optional precision that gives the minimum number of digits to appear for the d , i , o , u , x , and X conversions, the number of digits to appear after the decimal-point character for e , E , and f conversions, the maximum number of significant digits for the g and G conversions, or the maximum number of characters to be written from a string in s conversion. The precision takes the form of a period (.) followed by an optional decimal integer; if the integer is omitted, it is treated as zero.

[...]

A field width or precision, or both, may be indicated by an asterisk * instead of a digit string. In this case, an int argument supplies the field width or precision. The arguments specifying field width or precision, or both, shall appear (in that order) before the argument (if any) to be converted. A negative field width argument is taken as a - flag followed by a positive field width. A negative precision argument is taken as if it were missing.

Edited by dcs, 14 June 2008 - 07:34 PM.