From the course: Secure Coding in C

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Converting strings to numbers

Converting strings to numbers - C Tutorial

From the course: Secure Coding in C

Start my 1-month free trial

Converting strings to numbers

- [Instructor] Values input as text must be converted from characters into their proper binary form, either integers or real numbers. Now, one of the traditional functions used to accomplish this task is atoi, shown here at line 11, this code, the ASCII to integer function. This function swallows a text string and returns an integer. And it works. Now I'm going to try it again, but I'm going to just text as input, and it doesn't work because I didn't type in the value of zero. So how can you legitimately tell when zero was input and text was input? The solution is to use a more sophisticated function, in this case, on line 12, the strtol function, string to long. This function has three arguments. The first is the string to interpret. The second is a pointer variable p, and the third is the base of the input, base 10 in this case. The value is translated and stored in variable a, and then pointer p is tested…

Contents