473,795 Members | 3,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8899


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.car leton.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("attempt ing 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("attempt ing 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("attempt ing to read as octal integer\n");
ua = ub = uc = 0;
sscanf(input, "%o %o %o", &ua, &ub, &uc);
printf("unsigne d decimal values: %u %u %u\n\n", ua, ub, uc);

printf("attempt ing to read as hex integer\n");
ua = ub = uc = 0;
sscanf(input, "%x %x %x", &ua, &ub, &uc);
printf("unsigne d 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
3509
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: http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#1 Copying a file: WRONG WAY: #include <fstream> std::ifstream IN ("input_file"); std::ofstream OUT ("output_file");
5
20605
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 conversion happens? Also on 32 and 64bit machines what happens? Can anybody explain?
10
15665
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 standard doesn't even care what a normal char is (because signed and unsigned have equal behavior). For example if someone does this: unsigned char a = -2; /* or = 254 */
17
1601
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. Cheers.
13
5060
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
12016
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 as ODBC, then i am getting
4
17663
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
2111
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 << static_cast<std::ostringstream &>(std::ostringstream() << "Hello world\n").str(); std::cout << static_cast<std::ostringstream &>(std::ostringstream() << 'P').str(); ---
9
9567
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 function _main assign2.obj Thanks a lot ! Here is the code:
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10436
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.