Thursday, September 01, 2011

Coding Lessons: C and inputs (Lesson 4)

To start with we're only going to deal with fixed formatted inputs.

That is to say that we'll be assigning a variable type, (an integer for example), and we expect a user to be entering an integer.

We are leaving the system open to people inputting strings or floats or characters, and just trusting that they will enter a number.

Of course, if you were writing professional software, or software for anyone but yourself you'd be allowing input of strings, and then checking that this strings were actually numbers, and then converting them into numbers before allowing them to be treated as numbers.

To start, do the same as for any lesson, go into your coding folder and create a new folder called Lesson4, and open your editor.

you will of course be including the header file that we use to get either inputs or outputs from the console
#include <stdio.h>
And you'll be starting your main routine in the program.

int main()
{
As I said, we'll deal with numbers first, so lets declare a couple of integer variables:
int x, y;
I mentioned in an earlier lesson that you could declare two variables on the same line, that you just separated them by a comma. Well, you just did it!

now you need to tell the user of your program that you're waiting for an input, if you don't tell the user, then how are they going to know to do anything?

So print a message to the screen telling them to enter a number for the variable.
printf("enter a number for X:");
And now you need to actually get the variable.

To do this you use a function called scanf, scanf works pretty much the same as printf, but instead of printing to the terminal, you're scanning from the terminal.
The format of the function looks incredibly similar to the printf function, the same place holder for the variable types are used, but, you cannot put text into the statement, and instead of just writing the variable at the end, we have to prefix the variable name with an ampersand, like this.
scanf("%d", &x);
We tell the user to intput a value, and read that value for y from the keyboard in the same way.

Then we can display our variables, just like we did in earlier lessons.
printf("x=%d, y=%d", x, y);

You can now close your main routine and compile and run your program.

D:\coding\lesson4>tcc lesson4.c

D:\coding\lesson4>lesson4.exe
enter a number for X:99
Enter a number for Y:1
x=99, y=1
D:\coding\lesson4>

Of course, just getting and displaying inputs is a little bit boring, so lets look at some simple functions that we can do with numbers.

We can add them together.

If we'd declared another variable (Z?) then we could write;
z = x+y;
Then the variable z, would have the sum of variables x and y in it.

but we learned in our last lesson that we don't necessarily have to have a variable to display data.
we can just do the maths inside the printf statement.
printf("\r\n%d + %d = %d", x, y, x+y);

I'd recommend some caution here though, anything more complicated than simple maths functions are going to get a little bit difficult to work through, what I mean is that if they are in the end of a printf statement it's going to look messy, be difficult to read, and difficult to debug.

Anyway, add that line of code at the end of your program, and re-compile. and run again.
Testing
D:\coding\lesson4>tcc lesson4.c

D:\coding\lesson4>lesson4.exe
enter a number for X:99
Enter a number for Y:1
x=99, y=1
99 + 1 = 100
D:\coding\lesson4>

The numbers are input correctly, and the sum is calculated correctly.

Lets see if we can trip this program up, and expose some limitations.
D:\coding\lesson4>lesson4.exe
enter a number for X:-8
Enter a number for Y:4
x=-8, y=4
-8 + 4 = -4
D:\coding\lesson4>
It deals perfectly well with negative numbers, remember, integers are signed by default. so lets try something else.
D:\coding\lesson4>lesson4.exe
enter a number for X:1
Enter a number for Y:a
x=1, y=1245064
1 + 1245064 = 1245065
D:\coding\lesson4>
a, is not a number. the program doesn't crash, but the results are meaningless. Imagine if you were writing a program for a financial company, a simple mistake of hitting the wrong key could be disastrous!

Overflow error
Lets be a little less obvious about how to break this program now.
D:\coding\lesson4>lesson4.exe
enter a number for X:1
Enter a number for Y:2147483647
x=1, y=2147483647
1 + 2147483647 = -2147483648
D:\coding\lesson4>
How did that happen?!

If you remember in the first lesson on variables we talked about all the variable types, signed and unsigned, and we talked about how numbers were expressed in binary.
how a signed 4 bit number could count from -8 to +7
0111 = 7
1000 - -8

Well this happens with all signed numbers,
In this case the number has 32 bits, and all that happened was the variable was:
0111 1111 1111 1111 1111 1111 1111 1111
We added 1 to that, so it became.
1000 0000 0000 0000 0000 0000 0000 0000

If that were an unsigned number 2,147,483,647 would have become 2,147,483,648.
but because it is signed, (and has that signed bit) instead it became -2,147,483,648.

We've forced the computer to deal with a number larger than a 32bit integer, with consequences that we could expect, but could not deal with inside the program, because they were too big for the program to deal with!
Consider this:
D:\coding\lesson4>lesson4.exe
enter a number for X:214748364970
Enter a number for Y:214748364970
x=170, y=170
170 + 170 = 340
D:\coding\lesson4>

The numbers are nothing like what we entered, what we've started playing with is forcing the program to deal with numbers that it's just not equipped to. This is an overflow error.
(because the bits have overflowed out of the space that this variable can deal with), sometimes this just doesn't matter, (this program that we've made doesn't complain).
Other times it does matter

The complete code is here:
#include
int main()
{
int x, y;
printf("enter a number for X:");
scanf("%d", &x);
printf("Enter a number for Y:");
scanf("%d", &y);
printf("x=%d, y=%d", x, y);
printf("\r\n%d + %d = %d", x, y, x+y);
}

No comments: