printf and %i 
Author Message
 printf and %i

Could I ask what the position is on %i for printf? Why was this ever added
when %d is available? Is using %i depreciated?

Thanks

Stephen Howe



Sat, 02 Nov 2002 03:00:00 GMT  
 printf and %i


Quote:

> Could I ask what the position is on %i for printf?
> Why was this ever added when %d is available?
> Is using %i depreciated?

"%i" and "%d" are identical. The "%i" specification is sortof
depricated, but AFAIK this has not been officially declared by the
ANSI-C comittee. Even the new C99 standard supports "%i".

--
Stephan
bringing the c.l.c campaign against grumpiness into the next millennium
fight the bug, read the FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 02 Nov 2002 03:00:00 GMT  
 printf and %i

Quote:


>> Could I ask what the position is on %i for printf?
>> Why was this ever added when %d is available?
>> Is using %i depreciated?

>"%i" and "%d" are identical. The "%i" specification is sortof
>depricated, but AFAIK this has not been officially declared by the
>ANSI-C comittee. Even the new C99 standard supports "%i".

Not quite with scanf().  I had a problem once with %i and %d producing
different behavior.


Sat, 02 Nov 2002 03:00:00 GMT  
 printf and %i


Quote:
> On Tue, 16 May 2000 12:09:23 GMT, Stephan Wilms



> >> Could I ask what the position is on %i for printf?
> >> Why was this ever added when %d is available?
> >> Is using %i depreciated?

> >"%i" and "%d" are identical. The "%i" specification is sortof
> >depricated, but AFAIK this has not been officially declared by the
> >ANSI-C comittee. Even the new C99 standard supports "%i".

> Not quite with scanf().  I had a problem once with %i and %d producing
> different behavior.

Well, I was talking about "printf()" only (thta's what the OP asked
about). Maybe I should have said so. Now that you mention it I
explicitely looked up "scanf()" and found an interesting difference
between 'd' and 'i':

       d       Matches an optionally signed decimal integer,  whose
               format  is  the  same  as  expected  for the subject
               sequence of the strtol function with  the  value  10
               for  the  base argument.  The corresponding argument
               shall be a pointer to signed integer.

       i       Matches an optionally signed integer,  whose  format
               is  the same as expected for the subject sequence of
               the strtol function with the value 0  for  the  base
               argument.   The  corresponding  argument  shall be a
               pointer to signed integer.

Spot it ? "%d" corresponds to base 10 whereas "%i" corresponds to base 0
(ie. the base is implicitely taken from the string). The text was, BTW,
taken from my C9X draft, which serves me as a standard surrogate :-)

--
Stephan
bringing the c.l.c campaign against grumpiness into the next millennium
fight the bug, read the FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 02 Nov 2002 03:00:00 GMT  
 printf and %i

Quote:

> >"%i" and "%d" are identical. The "%i" specification is sortof
> >depricated, but AFAIK this has not been officially declared by the
> >ANSI-C comittee. Even the new C99 standard supports "%i".

> Not quite with scanf().  I had a problem once with %i and %d producing
> different behavior.

That is probably because the scanf family of functions conversion
specifications of "%i" and "%d" are not defined to be identical as
they are in the printf family of functions. With scanf there is a
difference. "%i" will handle conversion of hex and octal values if
they are correctly formatted.

#include <stdio.h>

int main(void) {
   char *hex = "0xff";
   int value;

   puts("Using %i in the sscanf function");
   sscanf(hex,"%i",&value);
   printf("Hex value \"%s\" = %i\n",hex,value);
   puts("\nUsing %d in the sscanf function");
   sscanf(hex,"%d",&value);
   printf("Hex value \"%s\" = %i\n",hex,value);
   return 0;
   }

--
Al Bowers
http://www.geocities.com/abowers822

C-faq: http://www.eskimo.com/~scs/C-faq/top.html
GIBCO Labs.
Tampa FL. USA.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 02 Nov 2002 03:00:00 GMT  
 printf and %i


Quote:
> Not quite with scanf().  I had a problem once with %i and %d producing
> different behavior.

And so they should. For printf() they are identical. For scanf(), %d will
match an integer in base 10 format whereas %i will match an integer in base
8, 10 or 16 C format. So

16
020
0x10
0X10

will all matched by

int iScan;
scanf("%i", &iScan);

I think Harbison and Steele's says that the differences between format
specifer langauges are sufficiently great enough that it is not worth
remembering on to use for the other.

Thanks for your help anyway.

Stephen Howe



Sat, 02 Nov 2002 03:00:00 GMT  
 printf and %i

Quote:


>> Could I ask what the position is on %i for printf?
>> Why was this ever added when %d is available?
>> Is using %i depreciated?



Quote:
>"%i" and "%d" are identical. The "%i" specification is sortof
>depricated, but AFAIK this has not been officially declared by the
>ANSI-C comittee. Even the new C99 standard supports "%i".

The C language I used before there was an ANSI C had no "%i" in
printf at all.  I assumed it was added to printf() to improve
(pseudo) symmetry with scanf().  Of course, printf() and scanf()
can never be fully symmetric for othe reasons, and if they are
supposed to be relatively pseudo-symmetric, it is odd that in C89,
"%lf" is undefined for printf().  (In C99, printf's %lf is a synonym
for plain %f -- improving that pseudo-symmetry again.)

In any case, %i in printf "means" exactly the same as %d.  In scanf,
however, it does not: after matching (and consuming) input of the
appropriate form, the %d scanf() directive acts much like a call
to strtol() with a "base" argument of 10, while the %i scanf()
directive acts much like a call to strtol() with a "base" argument
of 0 instead.  In other words, %i treats input like 0x12 as valid
(resulting in a value of 18), while %d treats it as the number
zero, leaving "x12" in the input stream.  Similarly, an input
sequence of 0123 converts to the value 123 with %d, but 1*64+2*8+3*1
or 83 with %i.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Sat, 02 Nov 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. replacing printf with my printf, then calling C lib's printf

2. (void)printf() vs. printf()

3. Re(2): Re(2): (void)printf() vs. printf()

4. Re(2): (void)printf() vs. printf()

5. printing % output in the printf statement like ( printf("i got a 15% raise"); )

6. return_value-printf("printf return's a value");

7. How to control the printf function ??

8. printf formats floats wrong?

9. printf() of death

10. printf 65 chars per line

11. printf and stdout

12. printf Crashing?

 

 
Powered by phpBB® Forum Software