1 – Syntax

Sit back, relax, grab that cup of coffee and let’s plow our way through a ‘Hello World’ application. Remember, you are not required to learn anything in this lesson, so just take it easy. This lesson is all about showing you the syntax of a computer language like C, so that you know what you are getting yourself into and what to expect.

What The Heck is ‘Hello World’ ?
Regardless of which programming language you are learning, the very first example you will be presented with, is a so-called ‘Hello World’ application. True to the tradition, this tutorial is no different. Hello World is an application, that basically prints a string onto your screen, usually “Hello, World.”. But I’m going to put a spin to it, and have it spit out “Hello, _YOUR_NAME_HERE_.” — fancy, huh?

You’re Killing Me, Let’s See Some Code! — Please?

I know, introductions are boring, but unfortunately a necessary evil. So let’s put some code up, and dissect it.

#include <stdio.h>
main()
{
char*myName=“Put Your Name Here”;
printf("Hello, %s!\n”,myName);
}

If you are experiencing any nausea right now, just relax — it’s all good. Now, before we dissect it, there is an important point I want to make by the following exercise. So, bare with me, as I attempt to teach you, probably the most important lesson you will ever learn in programming by example.

Exercise 1.1
Forget that you know what the program does. Read the code above through line by line, and try to make sense of it — take your time. Now, take a look at the code immediately below and do the same.

// 1 - include functionality we need in our program
#include <stdio.h>

// 2 - the main function
main ()
{
	// 3 - store my name in memory
	char * myName = “Put Your Name Here”;

	// 4 - print a formatted string with my name in it
	printf("Hello, %s!\n”, myName);
	
	/*
	 
	 This super awesome program was written by _YOUR_NAME_
	 
	 © 2010, all rights reserved.
	 
	 */
}

Yes, that is plain english in our code. Commenting your code is imperative, and makes all the difference between a good programmer and a great programmer. Code with no comments is poor programming practice, and often times result in that you —or someone else— will have to rewrite the whole thing, because you forgot what it did. Comments are helpful reminders, that you —and your potential collaborating developers— can relay what a particular segment of code does.

Single-line comments are defined by two consecutive forward slashes (eg. // this is a comment).

Multi-line comments are defined by two sequences of characters, the sequence /* marks the beginning of a multi-line comment and the sequence */ marks its end (eg. as shown in the example above).

Another point i want to make here, while we are demonstrating good programming practice, is that of formatting your code. C is a free-form language, meaning that you can put white-space and linebreaks anywhere you want. The compiler does not care for white-space and linebreaks, it simply ignores it, so use it to your advantage and format your code properly, such that it is clear and readable.

Dissecting The Code

Instead of taking it from the top-down, going through what each line of code does, I rather explain the different types of elements first, so that you can identify these elements as you learn — an analogy of this approach could be that of learning anatomy of the human body, you’d probably be introduced to the various organs, their placement and role, before being taught in their internal functionality. Note that you are not required to understand what the code does, but rather recognize the elements that make up the code — especially the instructions, which I will get to explaining shortly. In this example, I’ve used some slightly advanced concepts, that I do not expect you to learn until a lesson far beyond this one.

The first element #include , is a pre-processor directive. Pre-processor directives tells the compiler, that it needs to do something —in this case, include a file— before compiling our source code. Denoted by the # sign, pre-processor directives have many uses, but for now we will only be using it for including source/header files. The particular header we are including is a standard C library header, which contains functionality we are making use of further down in our code — the ’stdio.h’ header provides standard input/output functionality (such as the ‘printf’ function).

The second element I need you to recognize, is the main function. This function is the very first piece of code to be executed when your application launches. The compiler will be looking for this function, when you build your application, so it is very important that an application has this. By tradition, most applications main function is found in a source file called ‘main.c’ — not necessarily with the ‘.c’ extension though. We will be discussing functions later, for now just know that what is contained within the main function — denoted by the ‘{‘ marking its beginning and ‘}’ marking its end — is executed first, by every program.

The third element, is instructions — these are what make up the bulk part of any program. Instructions do not conform to one single use, but rather a concept, that applies to mostly everything in you will be doing in your application. This means that there are many different types of instructions (eg. calling a function, assigning a value to a variable, etc.), and these always end with a semi-colon. In our example above, we have two separate instructions; The first one ‘char * yourName = “Put Your Name Here”;’ —as the comment suggests—, stores your name in memory. The second one ‘printf(“Hello, %s!\n”, yourName);’ —again, as suggested by the comment—, prints a formatted string ’Hello, %s!’, replacing the ‘%s’, with our name, which we stored in the first instruction — This one is a function call to one of the functions that are contained within the ‘stdio.h’ header, which we included at the very top.

Exercise 1.2
If you understand the slightest bit of what goes on in the example above, I encourage you to experiment with it. Change it and see if you can do anything with it. This is true for any of the material I present to you, as experimentation is the only way of really learning how to program. The worst thing that can happen is that your computer starts to smoke and burn it won’t run.

Summary

If you did not get a heart-attack from this lesson, you should now be able to recognize the most basic elements of the C programming language. Following the next lesson, I will be expecting you to be able to recognize: pre-processor directives, the main function and instructions.

For the following lessons, I recommend that you use a stripped down version of our ‘Hello World’ example as a template. You can copy/paste the code below and use this as your template. Don’t worry about the new stuff in this code, we will get to that — this is just a more accurate representation of the main function. All you need to care about at this point is what goes on inside of the main function where the comment says “.. insert code here”.

#include <stdio.h>

int main (int argc, const char * argv []) {
	
	// .. insert code here
	
	return 0;	// return successful execution
}

 

Template Xcode Project

Download, unzip and double-click the template.xcodeproj file.

 

Previous: Introduction
Next up: Variables & Datatypes

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s