473,507 Members | 11,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is the difference between these two programs

48 New Member
#include <stdio.h>

int main(void) {
int i;
char a[3];
for(i=0;i<3;i++)
scanf("%d",a[i]);
printf("%d",atoi(a));
return 0;
}
this is giving the output as 0 but if i take input as a[3]={'1','2,'3'} it is giving correct what is difference in usage of atoi
Jan 30 '15 #1
2 1145
weaknessforcats
9,208 Recognized Expert Moderator Expert
Have you tried %c in the scanf since you have a char array? The %d is for int.
Jan 30 '15 #2
donbock
2,426 Recognized Expert Top Contributor
The "%d" argument tells scanf to store an integer value, but you tell it to store that integer value in a char. An integer is too big to fit in a char so scanf ends up writing past the end of array a. Something weird is going to happen.

The second argument to scanf is the address where the integer is to be stored. Your second argument is a[i]. That isn't an address. Something weird is going to happen.

The argument to atoi is a null-terminated string. Array a does not contain a string, and it isn't null-terminated. If array a doesn't happen to contain a null character (0), then atoi will read past the end of the array. Something weird is going to happen.

What do you want this program to do?
Jan 30 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2963
by: fowlertrainer | last post by:
Hello ! I see these things in many sources, under wxPy: def __init__(self,params,**kw,**args) What are these parameters, and what's the meaning of the double * ? Thanx for any info.
2
3646
by: diadia | last post by:
string s = "hello"; const char *p = s.begin(); cout << p << endl; // print hello s = ""; char *p2= s.begin(); cout << p2 << endl; // print hello why?????
1
1253
by: Dmitry Tkach | last post by:
Hi, everybody! I have been running a 7.3 database for a couple of weeks... and it has grown by more than 20Gig on me within that interval... I have a copy of the original database (I just copied...
2
1577
by: arthurzzk | last post by:
<bean id="sesFactoryWeb" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"><ref local="XXXXXX"/></property> <property...
5
10416
by: bhushanbagul | last post by:
Hi All Please let me know exactly what difference i and g makes in oracle version i.e. when we say oracle 10g or oracle 11i Thanks Bhushan
10
1530
by: CC | last post by:
Hi: I have considerable C and assembly language experience. However, these are mostly on embedded microcontrollers since I moved away from PC programming all the way back in 1988 :-O I wish...
2
1345
by: dellmacj | last post by:
hi, could someone explain to me in non-tech speak these programs? I'm a novice and C is a bit harder than i thought...how do you write them? 1.an integer is said to be square free if its factors...
10
9479
LazyPrinzez
by: LazyPrinzez | last post by:
Hello! I've just learned Java for about a month and a half. It's our focus for this semester. Now my teacher said that for our project, we are to make useful programs with GUI. She said that games...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7372
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...
1
7029
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...
0
5619
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,...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3190
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...
0
1537
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 ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.