Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-14-2008, 07:18 AM
payam.a payam.a is offline
Newbie
 
Join Date: Jun 2008
Posts: 12
Rep Power: 0
payam.a is on a distinguished road
Default a problem in C pre specifire

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.


Code:
/* 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!

Last edited by WingedPanther; 06-15-2008 at 08:22 AM. Reason: add code tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 06-14-2008, 07:38 PM
James's Avatar   
James James is offline
Newbie
 
Join Date: Jun 2008
Posts: 1
Rep Power: 0
James is on a distinguished road
Default Re: a problem in C pre specifire

Referencing to this site: cplusplus.com/reference/clibrary/cstdio/printf.html

Looks as if

Code:
// %[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);

Last edited by WingedPanther; 06-15-2008 at 08:23 AM. Reason: add code tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-14-2008, 11:32 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: a problem in C pre specifire

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.

Last edited by dcs; 06-14-2008 at 11:34 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A little problem with C Irfanerol C and C++ 4 07-25-2008 02:50 PM
Peculiar UI Problem Needs Tackling adriyel C# Programming 2 04-06-2008 08:46 AM
Problem read pwd protected Access2K dbase - CR9 & VB6 mrbar Visual Basic Programming 2 03-10-2008 05:50 AM
How to tackle a programming problem? TcM General Programming 10 01-07-2008 12:29 PM
A small problem in the output The_Master C and C++ 3 12-13-2006 01:04 PM


All times are GMT -5. The time now is 11:27 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads