To start off the day, there's nothing better than review.
For those of you unfamiliar with C reading this, this is for you- think of it as that one thingamajig that you've got to read before you get to do the cool stuff. Want to play with micro-controllers like the Arduino or some of its counterparts? Maybe you'll find this useful. This is what you'll need to learn first-
the basics of C programming.
NOTE: If you have a good understanding of Java, the only section you'll really need to read is the one on pointers, further down the page. The rest is syntax or just knowing the basics of programming.
Here's a famous tidbit of C that those of us familiar to the language will know right off the bat:
1
2
3
4
5
| #include <stdio.h>
int main() {
printf("Hello, world!");
}
|
Let's dissect this, piece by piece.
1. This first line is what's called including a header file. By including a header file, we are able to make use of what is basically more tools to play around with, and this particular header file gives us the ability to use the function printf() (and much more).
3. This is the entry point of our program.
int is an integer- it always has to be next to the main() call, and it simply means that main() returns some integer value. When we compile our program (meaning we're shifting it from a text file into an executable, something that our CPU can work with) and execute/run it, the first thing we'll get is...
4. ... a "Hello, world!" statement. printf() here takes one argument- a string of characters that is "Hello, world!"- and prints it to our terminal/console/wherever
STDOUT is.
5. And we end out program with a closing bracket, to signal the end of the code block for the main() function.
I would now take this and make sure you understand exactly what this does before continuing. Don't worry too much about what's happening 'under the hood' of printf()- for now, just know what it's supposed to do.
Now, let's try something just a tad more complicated:
1
2
3
4
5
6
7
8
| #include <stdio.h>
int main() {
int a = 5;
int b = 6;
printf("%d", a + b);
}
|
Now for the same process, but without what we've already gone over:
4. &
5. Our first variables,
a and
b. Here, we assign
a and
b to numbers 5 and 6 respectively using the '=' operator. C is a statically typed programming language- meaning we need to specify exactly what type of value 5 and 6 are:
int for integer.
7. printf() looks a bit different now- instead of taking one argument, it's taking two. Not only that, but I've added a placeholder into the string. Essentially, all this will do is replace what is in '%d' with the value of
a +
b.
The way printf() takes arguments is really interesting. At first, one might think that printf() is able to take in a variable number of arguments. The way of looking at this isn't wrong, but isn't quite right either. It decides how many arguments there are by taking a look at the first argument- the number of arguments printf() will have received will always be the number of formatting characters + the first string argument. I would take a look at
this for more details.
Of course, there are different types of variables:
- We have char for character, floats and doubles for decimals.
- Strings, on the other hand, are a slightly different case. In languages like Java, strings are considered objects. Here, in C, there's less of an abstraction in that strings are not actual 'things'. They are actually just arrays of characters, and can be initialized like so:
1
| char *hello_world = "Hello, world!";
|
or, an alternative:
1
| char hello_world[] = "Hello, world!";
|
You may not be familiar with the two ways of initializing character arrays here. The asterisk '*' next to a variable denotes a pointer, so in this case,
char* is a pointer to a character. Similarly,
int* would be a pointer to an integer, and so on. Think of it as allocating a chunk of memory that holds an address to some value. In this case, that would look a little something like this:
 |
| Courtesy of Google Images, from tenouk.com |
Now, what's the difference between the two ways of initializing strings? Well, in the first example, something like:
would not work. This is because using pointers, the string "Hello, world!" is put into read-only memory (meaning parts of memory that we cannot 'write' into), and thus we cannot edit it. However, using the char array form, this is a valid statement- the string "Hello, world!" is put into read-only memory, and then copied into the array so that we are given the freedom to modify its characters.
Of course, we can also make integer pointers (
int*), float/double pointers, etc. Note that pointers simply hold an address to the actual value. To get the address of that value:
1
| char* p = &hello_world; //some hex number here signifying an address in memory.
|
and to get the actual value,
1
| char c = *hello_world; //the first character in the character array, 'H'
|
Note that the asterisk denotes the first character in the string- this is because the pointer will always be initialized to the address of the first character of the string.
We'll stop here for now. From this review, you should've come away with some knowledge of pointers, variables, and very, very basic C. To continue on, I would work on finding out how basic
flow control structures work.
Feel free to mess with those programs as you wish. Try, for instance:
- What happens if you make give main() a different return type? Or if you try to use printf() to print a variable you haven't yet initialized?
- How do you add more placeholders into printf(), e.g for strings, floats, etc? How does it react if you give it too few arguments or too many? Can you make a nicely formatted output with it? (Hint: YES.)
- Use characters, strings, integers, floats, etc and see how printf() reacts.
- How would I find the length of a string programmatically in C?
- Use the flow control structures to understand how they work and what they do.