473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
Nov 13 '05 #1
3 8882


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
Nov 13 '05 #2
ling <sl***@chat.carleton.ca> 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
Nov 13 '05 #3
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

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steven T. Hatton | last post by:
I'm still not completely sure what's going on with C++ I/O regarding the extractors and inserters. The following document seems a bit inconsistent:...
5
by: radnus2004 | last post by:
Hi all, I am not clear what is the difference between signed and unsigned in C. Many say, unsigned means only +ve values and 0. Can I use unsigned int i = -3? What happens internally ? What...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
17
by: Radith | last post by:
In follow-up to my previous post; Would any one know how I can intake sentences of text (i.e. With space characters); because scanf("%s") causes input to terminate after a space character. ...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
3
by: nan | last post by:
Hi All, I am trying to connect the Database which is installed in AS400 using DB2 Client Version 8 in Windows box. First i created the Catalog, then when i selected the connection type...
4
by: marsarden | last post by:
code below: #include <stdio.h> int main(void) { char *aaa={"a","b","c"}; printf("%S\n",*aaa); /*the uppercase character S*/ getchar();
5
by: Michael Goldshteyn | last post by:
Consider the following two lines of code, the first intended to print "Hello world\n" and the second intended to print the character 'P' to stdout. --- std::cout <<...
9
by: Trent | last post by:
Here is the error while using Visual Studio 2005 Error 1 error LNK2019: unresolved external symbol "void __cdecl print(int,int,int,int,int,int,int,int)" (?print@@YAXHHHHHHHH@Z) referenced in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.