Convert a character array to an integer in C

Basically to convert a string to an int type in C language we use the atoi() function that is present in stdlib.h header file.

The declaration for this will be:

 int atoi(const char* string);

  •  Parameters: The parameters passed to this funciton is a character array(Coz ther are no strings in C) 

  •  Return value: 

    • The return value is integer if the string contains integral values only . 

    • If the string contains alphanumeric characters then it returns 0. 

    • If the first n characters in the string are integers and rest are alphanumeric characters then the first n characters are converted into integer.

Below is the link to the code that descibes its functionality:

https://onlinegdb.com/OeBg8ELe- 

The following output is genrated:  

String s is converted into integer normally as there are no alphanumeric characters in there and is stored in variable a

String s1 is not converted into integer as the string starts with an alphanumeric character and thus 0 value is returned and stroed in b variable.

String s2 starts with integers, but is also accompanied by alphanumeric characters, and thus only initial characters in the string that represent the integers are converted into integers. Here 12345 is returned and stored in c variable.

 

Thus, we can see from this output, the value 10 is from variable a, 0 is from b, and 12345 is from c. and all these are integer types.

This is how you can convert strings into integers in C langauge.

 

Want to learn Python Programing language? Join our telegram channel to access our free and easy-to-understand content and master python programming in no time.


Hope you find this article helpful. Thank You For Reading!!

If you liked this article, please mention a comment below.

Comments

Popular posts from this blog

Python Programing and Machine Learning