Visit Our UNIX and Linux User Community

type conversion C, atoi()

Thread Tools Search this Thread
Top Forums Programming type conversion C, atoi()
# 1  
Old 12-07-2011
type conversion C, atoi()

In the book "The C programming language"; second edition, chapter 2.7 there is a snippet which is supposed to:
"convert a string of digits into its numeric equivalent".
int atoi(char s[])
    int i, n;

    n = 0;

    for ( i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
        n = 10 * n + (s[i] - '0')

    return n;

What is the purpose to run
10 * n

if n is 0 ?

# 2  
Old 12-07-2011
Simplifies the code. If not done this way, you would have to have extra code to handle the n = 0 case.
This User Gave Thanks to fpmurphy For This Post:
# 3  
Old 12-08-2011
Ok, thanks. What i don't understand: Is there a difference between
0  * 0 
 1 * 0 
10 * 0

in C ? Different: is the 10 arbitrary, or is it chosen for a reason?
# 4  
Old 12-08-2011
Hi friend,

The code is not only for n=0, its in a for loop. Let me explain with an example.

If s contain a string "5270", then the loop continue for 4 times:

1st : 10*0 + 5 = 5
2nd : 10*5 + 2 = 52
3rd : 10*52 + 7 = 527
4th : 10*527 + 0 = 5270
which is the n value and a number instead of string.

Try to use some debugging tools and check the variables, u will understand it clearly...
This User Gave Thanks to siva shankar For This Post:
# 5  
Old 12-08-2011
Thanks to both of you, i understand it much better now (now fully, but i am on my way). Yes, siva shankar, what you just explained is exactly what i did not see.

Previous Thread | Next Thread
Test Your Knowledge in Computers #702
Difficulty: Medium
The MySQL LIKE operator is a logical operator that tests whether a string contains a specified pattern.
True or False?

9 More Discussions You Might Find Interesting

1. Programming

Logical Error With Type Conversion In C

So, I'm into about 650 lines of some code I'm working on. So I'll try to explain instead of flooding this post. Say I have some code like this: int main() { int i, j; char data; printf("Gimme something: "); fgets(data, INPUT_BUFF, stdin); for (j = 0; j < data; j++){... (6 Replies)
Discussion started by: Azrael
6 Replies

2. Shell Programming and Scripting

Military type format date/time conversion

Hello All, I have a requirement to convert a 12 hour format to 24 hour time format and the sample input /out put is below Input Time format : Nov 2 2011 12:16AM Out Put Format : Nov 2 2011 0:16 Input : Nov 2 2011 4:16PM Out Put: Nov 2 2011 16:16 I have done this using a... (6 Replies)
Discussion started by: jambesh
6 Replies

3. Programming

help with atoi and macros in C

I have a PORT_NUM macro (10 digits long number) in a server file, if i do htons(PORT_NUM) i get warning: this decimal constant is unsigned only in ISO C90 warning: large integer implicitly truncated to unsigned type whats wrong with this? (2 Replies)
Discussion started by: omega666
2 Replies

4. UNIX for Advanced & Expert Users

.so to .sl conversion ?

Hi all, I have one file ( which I got from a third party ). We use shared library . Is there any way to convert the .so file to .sl file ? Thanks in advance - M (3 Replies)
Discussion started by: kanu_kanu
3 Replies

5. Shell Programming and Scripting

Help in conversion ......

Hi, I have a requirement to capture file time stamp and compare with current system time. I am using HP-AUX K-shell. Below is what i have done Getting current date into myfile2 --------------------------------- date +%Y%m%d%H%M%S > myfile2 20091110132800 Getting the file date into... (5 Replies)
Discussion started by: chinniforu2003
5 Replies

6. Programming

array type has incomplete element type

Dear colleagues, One of my friend have a problem with c code. While compiling a c program it displays a message like "array type has incomplete element type". Any body can provide a solution for it. Jaganadh.G (1 Reply)
Discussion started by: jaganadh
1 Replies

7. Shell Programming and Scripting

Does KSH support data type conversion?

Hello,everyone here. I'm coding with KSH to achieve exploring the disk space and judging whether it closes to overflow.But It seems that no one way to convert a string variable to integer. df | read A B C D E F G H I J K L print ${L} Can I convert L to integer type? Thanks for... (2 Replies)
Discussion started by: joshuaduan
2 Replies

8. Shell Programming and Scripting

String type to date type

Can one string type variable changed into the date type variable. (1 Reply)
Discussion started by: rinku
1 Replies

9. Programming


i know what is the use of atoi function.... converts string to int. but whenever i use that it gives me 0.... could any one help in this issue.. eg. int i; char str; str="name"; i=atoi(str); i gives me 0. why? (3 Replies)
Discussion started by: bankpro
3 Replies

Featured Tech Videos