Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > What's the difference between the 'd' and 'i' conversion character?

Reply
Thread Tools

What's the difference between the 'd' and 'i' conversion character?

 
 
ling
Guest
Posts: n/a
 
      08-12-2003
Hi,

I'm trying to do fscanf from a file with integers.
I've tried using both %d and %i hoping that I would figure out their difference.

When I checked the man pages, the only difference I could tell was

d: Matches an optionally signed decimal integer ...
i: Matches an optionally signed integer ...

What's the difference between an integer and a decimal integer?

Thanks!!!

Ling
 
Reply With Quote
 
 
 
 
Kurt Watzka
Guest
Posts: n/a
 
      08-12-2003


ling wrote:
>
> Hi,
>
> I'm trying to do fscanf from a file with integers.
> I've tried using both %d and %i hoping that I would figure out their difference.
>
> When I checked the man pages, the only difference I could tell was
>
> d: Matches an optionally signed decimal integer ...
> i: Matches an optionally signed integer ...
>
> What's the difference between an integer and a decimal integer?


The integer is not necessarily in decimal notation. 27, 033, and 0x1B
represent the
same integer, and scanf("%i", &intVariable) will use the proper base
conversion if
oneof those representations can be read from stdin.

Kurt Watzka
 
Reply With Quote
 
 
 
 
Alex
Guest
Posts: n/a
 
      08-12-2003
ling <(E-Mail Removed)> wrote:
> Hi,


> I'm trying to do fscanf from a file with integers.
> I've tried using both %d and %i hoping that I would figure out their difference.


> When I checked the man pages, the only difference I could tell was


> d: Matches an optionally signed decimal integer ...
> i: Matches an optionally signed integer ...


> What's the difference between an integer and a decimal integer?


A 'decimal integer' means an integer in base 10.

Hence, *scanf will expect a decimal integer with the 'd' conversion
specifier and will attempt to detect the base first with 'i'.

Alex
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      08-12-2003
ling wrote:
> Hi,
>
> I'm trying to do fscanf from a file with integers.
> I've tried using both %d and %i hoping that I would figure out their difference.
>
> When I checked the man pages, the only difference I could tell was
>
> d: Matches an optionally signed decimal integer ...
> i: Matches an optionally signed integer ...
>
> What's the difference between an integer and a decimal integer?


Notice below that 0423 is an integer, but is interpreted differently with
%d (decimal value 423) and %i (octal 0423 = decimal 275) and 0x46 is 0 with
%d (the scan stops at the non-digit 'x') but hex 46 = decimal 70 with %i.

If you want decimal values with possibly leading zeroes, you must use %d
since %i will interpret it as octal.

#include <stdio.h>

int main(void)
{
char input[] = "375 0423 0x46";
int a, b, c;
unsigned ua, ub, uc;

printf("The input string is \"%s\"\n\n", input);
printf("attempting to read as integer\n");
a = b = c = 0;
sscanf(input, "%i %i %i", &a, &b, &c);
printf("decimal values: %d %d %d\n\n", a, b, c);

printf("attempting to read as decimal integer\n");
a = b = c = 0;
sscanf(input, "%d %d %d", &a, &b, &c);
printf("decimal values: %d %d %d\n\n", a, b, c);

printf("attempting to read as octal integer\n");
ua = ub = uc = 0;
sscanf(input, "%o %o %o", &ua, &ub, &uc);
printf("unsigned decimal values: %u %u %u\n\n", ua, ub, uc);

printf("attempting to read as hex integer\n");
ua = ub = uc = 0;
sscanf(input, "%x %x %x", &ua, &ub, &uc);
printf("unsigned decimal values: %u %u %u\n\n", ua, ub, uc);
return 0;
}


The input string is "375 0423 0x46"

attempting to read as integer
decimal values: 375 275 70

attempting to read as decimal integer
decimal values: 375 423 0

attempting to read as octal integer
unsigned decimal values: 253 275 0

attempting to read as hex integer
unsigned decimal values: 885 1059 70





--
Martin Ambuhl

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 04-15-2011 04:00 AM
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
difference between between these "char"s arnuld C++ 33 03-05-2007 03:11 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments