
We use atoi to convert a numeric string to his integer value. atoi is a c library function and it takes a string as an argument and returns its integer value.In this article, we will see the atoi implementation in c and its important features.
Syntax:
1 | int atoi(const char *pszString); |
A simple program to describe the functionality of atoi()
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include<stdio.h> #include<string.h> int main () { int iValue=0; char buff[]="1234"; iValue=atoi(buff);//Convert numeric string to his integer value printf("%d\n",iValue); return 0; } |
OutPut: 1234
Some Important Points of atoi()
- If an alphabetic character comes in between of a numeric string then conversion only perform till the alphabetic character.
In below example ‘a’ occur between the string. So atoi() convert only till ‘a’.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<stdio.h> #include<string.h> int main () { int iValue=0; char buff[]="12a34"; iValue=atoi(buff);//Convert numeric string to his integer value printf("%d\n",iValue); return 0; } |
OutPut: 12
- If an alphanumeric character comes at the beginning of string then atoi() return 0.
In below example, an alphabetic character ‘a’ come at the beginning of the string.We already know that if alphabetic characters occur in between of the string then atoi()function immediately stop the conversion.So in this example atoi() return 0
1 2 3 4 5 6 7 8 9 10 11 12 | #include<stdio.h> #include<string.h> int main () { int iValue=0; char buff[]="a1234"; iValue=atoi(buff);//Convert numeric string to his integer value printf("%d\n",iValue); return 0; } |
OutPut: 0
- If the length of numeric string greater than the range of integer then you couldn’t get a real value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<stdio.h> #include<string.h> int main () { int iValue=0; char buff[]="100000000000"; iValue=atoi(buff);//Convert numeric string to his integer value printf("%d\n",iValue); return 0; } |
OutPut: 1215752192
If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.
Program to implement own atoi() in C
Source Code 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include<stdio.h> #include<stdint.h> #define Is_NUMERIC_STRING(d) (*(char*)d >= 48) && (*(char*)d<= 57) uint32_t StringToInt(const char *pszBuffer) { uint32_t u32Number=0; while( Is_NUMERIC_STRING(pszBuffer)) { u32Number=(u32Number*10)+ (*pszBuffer-48); pszBuffer++; } return u32Number; } int main() { uint32_t d; d=StringToInt("1230"); printf("%u\n",d); return 0; } |
OutPut: 1230
Source Code 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include<stdio.h> #include<stdint.h> #define Is_NUMERIC_STRING(d) (*(char*)d >= 48) && (*(char*)d<= 57) #define CONVERSION(d,k) ((d<<3) +(d<<1))+(*(char*)k-48); uint32_t StringToInt(const char *pszBuffer) { uint32_t u32Number=0; while( Is_NUMERIC_STRING(pszBuffer)) { u32Number=CONVERSION(u32Number,pszBuffer); pszBuffer++; } return u32Number; } int main() { uint32_t d; d=StringToInt("1230"); printf("%u\n",d); return 0; } |
OutPut: 1230
Source Code 3
The above methods do not handle the negative number and invalid pointer, so below another simple method that handles negative number and the invalid pointer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include<stdio.h> #define Is_NUMERIC_STRING(d) (*(char*)d >= 48) && (*(char*)d<= 57) int StringToInt(const char *pszBuffer) { int result=0; // variable to store the result int sign = 1; //Initialize sign as positive if(pszBuffer == NULL) //If pointer is null return 0; //If number is negative, then update sign if((*pszBuffer) == '-') { sign = -1; ++pszBuffer; //Increment the pointer } while( Is_NUMERIC_STRING(pszBuffer)) //check string validity { result = (result*10)+ (*pszBuffer-48); pszBuffer++; //Increment the pointer } return (sign * result); } int main() { int d; d = StringToInt("-1230"); printf("%d\n",d); return 0; } |
OutPut: -1230
Hello sir, I need interview questions on C and Microcontroller, so can you provide me? Thank yopu
Link contains some important quiz, http://aticleworld.com/c-quiz/