tiny question on format of scanf and printf 
Author Message
 tiny question on format of scanf and printf

I'm ignorant with the format of scanf and printf,say:

1.if source data as
1212,343,54,664,654654,...
how to apply scanf to get these data separated by ","?

2. double d1 = 1.9999,d2=1.356,...;
   printf("%1.2f,%1.2f",d1,d2);
results in 2.00,1.36...
How to make the floating NOT rounding, like
1.99,1.35?

Thanks lots

John



Sat, 02 Apr 2005 23:56:41 GMT  
 tiny question on format of scanf and printf


Quote:
> I'm ignorant with the format of scanf and printf,say:

> 1.if source data as
> 1212,343,54,664,654654,...
> how to apply scanf to get these data separated by ","?

Use a comma in the format string.

Quote:
> 2. double d1 = 1.9999,d2=1.356,...;
>    printf("%1.2f,%1.2f",d1,d2);
> results in 2.00,1.36...
> How to make the floating NOT rounding, like
> 1.99,1.35?

Surest way is probably to use the floor() function to truncate the value
(some thought needs to be given as to how you want to handle negative
numbers.

printf("%1.2f,%1.2f", floor(100.0*d1)/100.0, floor(100.0*d2)/100.0);



Sun, 03 Apr 2005 00:14:31 GMT  
 tiny question on format of scanf and printf
On Tue, 15 Oct 2002 10:14:31 -0600, in comp.lang.c , "William L. Bahn"

Quote:



>> 2. double d1 = 1.9999,d2=1.356,...;
>>    printf("%1.2f,%1.2f",d1,d2);
>> results in 2.00,1.36...
>> How to make the floating NOT rounding, like
>> 1.99,1.35?

>Surest way is probably to use the floor() function to truncate the value
>(some thought needs to be given as to how you want to handle negative
>numbers.

Some thought also needs to be given to numbers that are already at the
required precision. For instance 1.21 might be internally stored as
1.20999945 because of floating point errors, and would this print out
as 1.20 instead of 1.21.

Quote:
>printf("%1.2f,%1.2f", floor(100.0*d1)/100.0, floor(100.0*d2)/100.0);

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


Sun, 03 Apr 2005 04:11:18 GMT  
 tiny question on format of scanf and printf

Quote:

> On Tue, 15 Oct 2002 10:14:31 -0600, in comp.lang.c , "William L. Bahn"



> >> 2. double d1 = 1.9999,d2=1.356,...;
> >>    printf("%1.2f,%1.2f",d1,d2);
> >> results in 2.00,1.36...
> >> How to make the floating NOT rounding, like
> >> 1.99,1.35?

> >Surest way is probably to use the floor() function to truncate the value
> >(some thought needs to be given as to how you want to handle negative
> >numbers.

> Some thought also needs to be given to numbers that are already at the
> required precision. For instance 1.21 might be internally stored as
> 1.20999945 because of floating point errors, and would this print out
> as 1.20 instead of 1.21.

float roundtodigits(float v, unsigned int n) /* untested */
{
   int   i;
   float d;

   for (i = 0, d = 0.0; i < n; i++) d = d * 10.0F;{
   v = floorf(d * v + 0.5);
   return (v / d);

Quote:
} /* roundtodigits UNTESTED */

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!


Sun, 03 Apr 2005 06:58:46 GMT  
 tiny question on format of scanf and printf

Quote:

> On Tue, 15 Oct 2002 10:14:31 -0600, in comp.lang.c , "William L. Bahn"


> >> 2. double d1 = 1.9999,d2=1.356,...;
> >>    printf("%1.2f,%1.2f",d1,d2);
> >> results in 2.00,1.36...
> >> How to make the floating NOT rounding, like
> >> 1.99,1.35?

> >Surest way is probably to use the floor() function to truncate the value
> >(some thought needs to be given as to how you want to handle negative
> >numbers.

Couldn't you get around neg numbers with fabs() and then re-neg it if it was
a neg number?

Quote:
> Some thought also needs to be given to numbers that are already at the
> required precision. For instance 1.21 might be internally stored as
> 1.20999945 because of floating point errors, and would this print out
> as 1.20 instead of 1.21.

> >printf("%1.2f,%1.2f", floor(100.0*d1)/100.0, floor(100.0*d2)/100.0);

Sean


Sun, 03 Apr 2005 08:44:43 GMT  
 tiny question on format of scanf and printf
Thank you.

but how about the following case:

int a,b;
char *c;

if the format is scanf("%d,%d,%s) and the contents in string-data may
contain "," , like

1,2,this is a comma,not separatro,3,4,again

how can I let the scanf know that "," is a separator or not? surpose
the source-data can be modified, like I can insert "\" before ","
which is real a comma in the same data-field.

John


Quote:
> printf("%1.2f,%1.2f", floor(100.0*d1)/100.0, floor(100.0*d2)/100.0);



Sun, 03 Apr 2005 09:56:34 GMT  
 tiny question on format of scanf and printf

Quote:
> Thank you.

> but how about the following case:

> int a,b;
> char *c;

'c' needs memory allocation before use.

Quote:
> if the format is scanf("%d,%d,%s) and the contents in string-data may
> contain "," , like

> 1,2,this is a comma,not separatro,3,4,again

> how can I let the scanf know that "," is a separator or not? surpose
> the source-data can be modified, like I can insert "\" before ","
> which is real a comma in the same data-field.

You can do the following:

scanf("%d,%d,%s", &a, &b, c);

assuming that you have allocated memory to c. Note that the "," can be
changed to other characters to suit your input format.

I would actually prefer:

scanf("%d,%d,", &a,&b);
fgets(c, buffer_length, stdin);

where buffer_length is the length of the "c" array. This will avoid buffer
overflow.

Sean



Sun, 03 Apr 2005 16:45:32 GMT  
 tiny question on format of scanf and printf

Quote:
>I'm ignorant with the format of scanf and printf,say:

>1.if source data as
>1212,343,54,664,654654,...
>how to apply scanf to get these data separated by ","?

Did you consider curing your ignorance by reading the relevant chapter(s)
from your favourite C book?

Quote:
>2. double d1 = 1.9999,d2=1.356,...;
>   printf("%1.2f,%1.2f",d1,d2);
>results in 2.00,1.36...
>How to make the floating NOT rounding, like
>1.99,1.35?

You have to perform the truncation yourself, before submitting the
values to printf.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Sun, 03 Apr 2005 21:06:07 GMT  
 tiny question on format of scanf and printf
you can use the "strtok" function to extract the tokens from the
string.
just check for the last character in the token if it is the special
character that you have defined, for example in this case as '\'. If
it is the case, combine it with the next token.
Quote:

> Thank you.

> but how about the following case:

> int a,b;
> char *c;

> if the format is scanf("%d,%d,%s) and the contents in string-data may
> contain "," , like

> 1,2,this is a comma,not separatro,3,4,again

> how can I let the scanf know that "," is a separator or not? surpose
> the source-data can be modified, like I can insert "\" before ","
> which is real a comma in the same data-field.

> John


> > printf("%1.2f,%1.2f", floor(100.0*d1)/100.0, floor(100.0*d2)/100.0);



Sun, 03 Apr 2005 21:32:07 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Examples of format strings for scanf/printf ?

2. printf/scanf format character(s) for 64 bit ints ??

3. questions about sprintf, sscanf, printf, scanf, fscanf, fprintf!

4. Simple scanf/printf question

5. Beginner question - printf and scanf

6. A printf format specifier question:

7. printf format question

8. printf format/data representation: question

9. printf() float format question

10. Tiny C compiler, tiny OS

11. Tiny C compiler, tiny OS

12. skipped printf/scanf ???

 

 
Powered by phpBB® Forum Software