CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problem char to long

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Alexou
Guest







Problem char to long
PostPosted: Wed Nov 16, 2005 6:41 pm     Reply with quote

My code :

int16 Ti_Result = 0;
char Ti[3];
....
gets(Ti);
Ti_Result = atoi(Ti);
...
printf("Inter : %Lu ", Ti_Result);

My problem is :

if gets(Ti) => 255 and printf is OK (255)

if gets(Ti) => 300 and printf is KO (44)

Why

Merci d'avance...

Alexou
Eric Minbiole



Joined: 10 Jun 2004
Posts: 16
Location: USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Nov 16, 2005 9:35 pm     Reply with quote

I see two problems:

1. The reason 300 "overflows" back to 44 is that "atoi()" only returns an 8 bit value. Even though you are assigning it to 16 bit Ti_Result, it has already overflowed. Use the "atol()" function instead.

2. Your string, Ti, is not long enough to hold your 3 character string + the terminating NUL character. It should be at least 4 characters if you are going to input a 3 digit number. This may not have been causing any immediate problems, but as your code grew, you would be overwriting whatever variable was after "Ti".

Hope this helps! (Bon de la chance? My French is rusty.)
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group