I am learning C and am trying to write a program that will take 2 value from a user and will check if these strings are binary values (composed of the characters 1 or 0 is how I approached it) before attempting to, according to user selection:
- Add the two values,
- Subtract input 2 from input 1, or
- Multiply the two values.
I can implement the functions to do the math when I assume each character in the string is a binary bit in the manner of char string1 = "0101";
, but it does seem a little silly to parse through the string a character at a time to do this kind of math (assembly would be much faster).
I am wondering what the most efficient way to do this sort of thing would be in C? I am also wondering if there would be a better way to deal with the user input than to scanf()
into a string and continue to use the data as a string after you've checked if the contents are numeric values of 1 or 0?
I did look around stackoverflow.com and did some web searches, but I didn't find anything that seemed obviously better to a C beginner such as myself. Any tips or suggestions would be appreciated, since I am trying to learn to be efficient in my code from early on.