From the course: Secure Coding in C (2019)
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Converting strings to numbers - C Tutorial
From the course: Secure Coding in C (2019)
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Authenticating numeric input2m 54s
-
(Locked)
Converting strings to numbers2m 23s
-
(Locked)
Using int values instead of char2m 32s
-
(Locked)
Reading input with fgets()4m 7s
-
(Locked)
Filtering string input4m 14s
-
(Locked)
Challenge: Confirming input1m 2s
-
(Locked)
Solution: Confirming input1m 54s
-
(Locked)
-
-
-