From the course: Secure Coding in C

Unlock the full course today

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

Using int values instead of char

Using int values instead of char - C Tutorial

From the course: Secure Coding in C

Start my 1-month free trial

Using int values instead of char

- [Instructor] The getchar and putchar functions sound like they deal with single characters, and they do, but their variable types are defined as integers and there's an important reason why. So, in this code, the getchar function of line 10 uses character variable ch, and the code builds with no errors and appears to run fine. So, I'll build, check the error log, no errors, no warnings, and let's run it. Okay, so type text to stop. Test, and then we'll type the tilde, and it works. Now, in a command prompt window, I'm going to test the code with input redirection. I renamed the files to a, and I'm going to redirect the contents of the test.txt file into this function, and the program goes crazy. Press Control + C to terminate it. In this update to the code, the variable type is changed at line five to an integer. Further, at line 11, the EOF in the file constant is tested. This assures that input redirection is…

Contents