Your First C Program

Posted by Jared Kipe on | 1 Comments

Tags: , ,

Practically, all modern languages descend from C, and thus use most of C's principles and basic structures. C is a perfect place to start learning about basic programming. C can be a launching platform for both more advanced C (like embedded operating systems), or, more likely, object oriented languages like C++ and Objective-C.

I'm not going to lie, this code isn't going to be exciting. But let's dissect the standard "Hello World" program in C.

#include <stdio.h>
int main(int argc, const char *argv[]) {
	printf("Hello World!");
	return 0;
}

Functions

There is a very popular metaphor that a function is like a toaster. A toaster is any device where you put in bread, push a button or lever, and at a pre-determined time, "toast" comes out. Functions work similarly. Sometimes you design, or make the function yourself, sometimes the function already exists (like the toaster) and you simply use it. Either way, the function needs to follow very specific rules on how you can use or "call" a function, what the various parameters or "arguments" of the function are and what the function gives back when it is done.

Lets concern ourself with a toaster that takes a single piece of bread. In C, this function (or toaster) would be prototyped like this:


toast toaster(bread slice, int time);

The first word is toast, this is the "return type" or what is given back to the user or "caller" when the function is done. toaster is the name of the function. "bread slice" is the type of the first argument (bread) and slice is the local variable name for that argument. "int time" is the type of the second argument (integer) and time is the local variable name for the second argument.

You don't really need to know the mechanism by which toaster makes the bread into toast, or how the time affects it. In fact you don't even need to include the word "slice" and "time" in the prototype, but their names are usually hints to the caller on what the arguments actually are.

The program itself is effectively a function named main(). Every program needs a main() function, which is the function that is run when the program itself executes. It needs to be named main, and for right now you shouldn't really concern yourself with the arguments or return type of main.

The C Preprocessor

The C preprocessor gets run on your files before compilation, and usually results in a fancy "search and replace" getting called on your document.  There are a handful of preprocessor directives, and I will go over them each time I use them.  All of these directives begin with the # symbol, usually near the start of a document and needs to be on its own line. Preprocessor directives do not end with a semicolon, as they are not actual C instructions and always get stripped out of the file before compilation. The preprocessor also strips out comments, anything after a double forward slash (//) or between the multiline comment block (/* blah blah */).

New C Preprocessor directives:

#include

#include effectively takes a single argument that is the name of a file, or path with file name, that is to be included in-line in the document right where the #include was. These are almost always header files (xxxx.h) that include things like function prototypes and struct declarations that are used in the current file (xxxx.c). Without the prototype of a function, the compiler may not be able to infer the correct information about the function when you call it in your own source code.

#include's can have the file name surrounded by less than/greater than symbols, this indicates it is a system header that you did not write and the preprocessor will look in special include folders to find it. If the file name is surrounded with double quotes the preprocessor will look in the same folder as the current file, use this when including your own header files in your source.

Examples:

// system header stdio.h
#include <stdio.h>
// personal header, probably in the same directory as the current file
#include "myFile.h"

New C Functions:

int printf(const char *format, ...);

declared in <stdio.h>

printf() is used to print to standard out (usually the console), format is a constant string that can include 0 or many special format arguments. The ... indicates that it can take a variable number of arguments, there will be 0 or many arguments to match the special format arguments in the format string. The integer return value is the number of special format arguments printf() matched.

The most common way of using printf() is to output an integer or double you have performed a calculation on. There are a number of format specifiers to achieve a wide array of output, click the function prototype to learn more.

Examples:

#include <stdio.h>
int i = 5 + 4;
printf("5 + 4 = %d", i);
/* %d is a signed integer, outputs...
5 + 4 = 9
*/

Post your comment

Comments

  • Gravatar for santa

    Great blog.

    Posted by santa, 30/07/2012 5:50am (6 years ago)

RSS feed for comments on this page | RSS feed for all comments