Tuesday, August 30, 2011

Coding Lessons: C and more variables (lesson 3)

So far we've covered that there is a way to print stuff to the screen.
And that you can set up variables, and that there are different types of variable for different types of data. We declared a variable, gave it a value, and wrote that value to the screen.

But so far we only dealt with 1 data type, (because I didn't want to make the lessons too long).

We know how to declare lots of data types, now lets look at using a few more data types

As before set up your workspace (create a new folder d:\coding\lesson3) and open your editor.

Just as before add any comments that you like, and put that header file in that'll let us deal with outputting data to the screen

#include <stdio.h>

And start your main function

int main()

This time I'm not going to declare the variables, of course you can if you want to, but what I'm going to do this time is use the variable place holders in the printf statement, then assign them hard coded values at the end.

Now, lets look at characters, (ones that you'd assign to the char variable type).
printf ("looking at characters: %c %c \n\r", 'a', 98);

there are going to be two characters printed on that line (there are two %c's) looking at the end, you can see that one of those characters is going to be a, and the other is going to be 97?

97 isn't a character!, but it is a place on an ASCII Character map!

If you look up the ASCII character values you'll see that this corresponds to the letter b.

the first 127 characters I'd pretty much agree, there is not a lot of point in printing them, but the extended ASCII, that's where it gets more interesting, these don't appear on the keyboard, but you can use them by just writing the number value.


The other thing that was introduced there was \n (new line) and \r (carriage return), and that's because if we just have two statements;
printf("some text");
printf("some more text");
They will appear on the same line as "some textsome more text"

now let's look at some integers (like we used in the first example).

printf ("integers: %d %ld\n\r", 1234, 650890L);
What we've done there is looked at a %d, (signed integer), and also looked at a much bigger number using a long integer %ld, you'll see that at the end of the large number there is a big L that specifies that it's a long.

So we're really getting into the hang of things now!

So let's look at some special formatting,
Say I'm writing a table, and I need to preserve the formatting, it should be easy right, I can just use spaces to pad out the cells of my output? (but how do I know how many spaces to put?) the variable changes? how do I know I'll need 8 blank spaces, what if I need 9, or 7?

Well, there is a way to tell the printf statement to print out that integer, and pad the space with blank spaces.

printf ("Padding with blanks: %10d \n\r", 123);
that 10 in front of the d in the %d place holder tells the program that that data takes up ten spaces. (so seven blank spaces are put into the front of the number 123)

If I wanted data to be displayed in a set number of characters, i could just as easily pad out the blank space with zeros instead I'd do that like this:

printf ("Padding with zeros: %010d \n\r", 1234);

I said at the start that these lessons would be geared towards programming micro controllers, what if I want to get a number to or from the chip, that number is a representation of 8 bits, I don't write 0111 1111 for off on on on on on on on whichis what I want my 8 output pins to read. I want to represent that in as simpler way as possible in my code, I want to use hex. where I can just write 7f, which is a little easier than working back to the base ten number of 127.

printf ("Using different bases: Decimal %d unsigned hex %x unsigned Octal %o formatted Hex %#x Formatted Octal %#o \n\r", 127, 127, 127, 127, 127);

But now what about floating point numbers, dealing with floats is pretty important, just about everything in this world can be expressed as a floating point number.
if I say to you that my chip measures the temperature of 0x4f it means nothing. but if there is the correct maths on my chip to write 23.5 degrees now that's much better!

printf ("floating point numbers: %10.5f %010.5f %2.2f %E \n\r", 23.517, 23.517, 23.517, 3344.1416);

I'm re-using a couple of the tricks from above. the first one %10.5f basically says, take up ten character spaces, use blank space at the start, have five places after the decimal point.

So 23.517 is displayed as [space][space][space]23.51700

The next example uses the same trick to write zeros instead of blank spaces (%010.5f)
so 23.517 appear as 00023.51700

The next is quite useful, I'm not adding any place holders, or extra information, I'm restricting the accuracy.
%2.2f means 2 places before the decimal place, 2 places afterwards.
so 23.517 gets neatly trimmed to 23.51, I gave the example earlier of a temperature probe, you probably don't need to worry about the thousandth of a degree!

And %E means display in scientific format.
so 3344.1416 becomes 3.3441416 x10(3)

That's floats pretty much covered, so lets move on.

I said earlier about setting the width of text by padding with zeros or blank spaces,
but, there is another way.
printf ("setting the width: %*d \n\r", 5, 14);

If you put a star after the % sign, you can then specify the width before the value.

And last but not least, because I know that you'll want to work with strings of characters so that you can send messages!

printf ("%s \n\r", "A silly message goes here");

%s is the place holder for strings, strings can contain numbers, (as in you can write)
printf("%s \n\r", "123");
But you cannot perform maths functions on strings!

Put it all together
The complete example is here:


int main()
printf ("looking at characters: %c %c \n\r", 'a', 98);

printf ("integers: %d %ld\n\r", 1234, 650890L);

printf ("Padding with blanks: %10d \n\r", 123);

printf ("Padding with zeros: %010d \n\r", 1234);

printf ("Using different bases: Dec %d unsigned hex %x unsigned Octal %o formatted Hex %#x Formatted Octal %#o \n\r", 127, 127, 127, 127, 127);

printf ("floating point numbers: %10.5f %010.5f %2.2f %E \n\r", 23.517, 123.517, 23.517, 3344.1416);

printf ("setting the width: %*d \n\r", 5, 14);

printf ("%s \n\r", "A silly message goes here");


so you need to save that in D:\coding\lesson3\
as variables.c

then navigate to that directory and type "tcc variables.c" hit return,

No comments: