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

L

ling

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
 
K

Kurt Watzka

ling said:
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
 
A

Alex

ling said:
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
 
M

Martin Ambuhl

ling said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top