Guides:C/C Crash Course/Language Overview

From CoderGuide

Jump to: navigation, search

Back to TOC

Language Overview

Don't get discouraged if this section is confusing to you, it'll all make sense in later sections as we begin writing programs. Think of this more as a dictionary description of a word, then, in the later sections, we'll start showing you how to use it.

A C program consists of a few basic elements: keywords, operators, code blocks, functions, and compiler directives. The C language also comes with something called The Standard C Library which provides a standard way of talking to the operating system and other hardware in a computer, and also performs many many useful tasks.

A keyword performs many tasks, whether it means telling the compiler to reserve some space for a variable, performing some evaluation, looping, and any number of tasks. The following is a list of C keywords:


We'll be talking more about some of these later, but we won't be covering all of them is beyond the scope of this crash course.

Code blocks contain C statements, and can exist be created anywhere within a function. Variables can only be defined at the top of a code block before any other C statements are used, and outside of all code blocks to defied a global variable. An open brace '{' marks the beginning of a code block, and closed brace '}' marks the close of a block.

{ /*marks the beginning of a code block*/
	int var; /*define some variable*/
	/*Look at me I'm a code block!*/
} /*marks the end of a code block*/

Compiler directives begin with a pound sing '#' and exist only on a line by themselves. They tell the compiler to parse the code a differently, or to do something else different during compiling. For instance, the #include tells the compiler to another parse a file as if it were part of the current file. In effect, it includes, or inserts a file at that spot. There are also many others.

Functions are an important part of C. In fact, you couldn't do much with C at all without them. One function, the main() is a very special function that you define: it is the first function that is called when your program is run. It basically is the starting point of your program. It is also the only function that does not require a function prototype.

/*A definition for the main function*/
int main(){
/* a prototype for a user-defined function */
int func(int a);
/*a user-defined function*/
int func(int a){

Whenever you start a code block, you should always type the closing brace on the next line, then go back up and fill in the rest, this way, you're to not forget a closed brace. Forgetting to close a code block is a very tough syntax error to find and fix, especially large.

C comments are started with a /* and ended with a */. It is important to include these in your program to explain to others, and yourself when you go back to look at your code, what exactly you're trying to do in that part of your program. All comments are ignored by the compiler. The ANSI C standard does not allow comments to be nested:

/* This comment is okay*/

so is this  one

/* This one /* is not */ okay */

Every statement in C, with the exception of function definitions (not their prototypes), and compiler directives (which are not C statements), end with a semicolon ';'. A good way to think of statements as sentences in in C, and the semicolon marks the end of a sentence, while code blocks mark the paragraphs of the C language.

Now it's time to write our first C program, and to make sure we can compile our C code. Start up your favorite ASCII text editor (NOT WORDPAD, OR ANY OTHER WORD PROCESSOR) and type in the code below (you can leave out the comments):

Caution:C is case sensitive, that means, an upper case letter and a lower case letter might as well be entirely different letters to C.

  1. /* This is a C comment, and this is our first C program */
  3. #include <stdio.h> /* include  some function prototypes from the standard
  4. 		      library*/
  6. int main(){ /* this line defines a function, and the '{' indicates a
  7. 	       start of a code block */
  9. 	/* call the printf function in the standard library to print 
  10. 	   the message on the screen. The '\n' is an escape sequence
  11. 	   that the compiler translates into a newline character. */
  13. 	printf("Hello World!\n");
  15. } /* This line indicates the end of a code block, in this case, the
  16. end of the main() function*/

Note: Don't type in the line numbers. They are provided for reference only and are used when we present a fully functional program, or a section of code we wish to explain by line numbers.

Notice that I indent everything within a code block by a tab? This is a good programming practice and will make your code a lot easier to read. If you had a code block within a code block, it should also be indented as well, like this:

	Some statements;
		more statements;
			Even more statements;

It is helpful to add a comment at the end of each closed brace that marks the end of a code block that lets you know which code block it belongs to, otherwise you could loose track very quickly in your program.

Some editors default to using 4, or even 3 spaces for indentation when your press the tab key. This can make code hard to read, so I, and many other programmers prefer to use the standard 8 spaces for a tab. We also use editors that default to using the tab character (such as Vim), rather than just using spaces, to make editing easier. When an editor encounters a tab, it will jump right to the next tab stop, rather than having to move back or forward so many spaces.

You should also try and keep your lines shorter that 70 characters, and definitely no longer that 80. If you don't then you'll have trouble printing out your code, and it will be harder to read. jEdit has a box that surrounds your text, when you begin to type outside of it, then you know your lines are longer that 80 in length. Vim, when it starts up, defaults to an 80x24 line display. This is one of many reasons why you should use one of these editors, or another generic programmer's editor for your code.

Now that you've entered in your code and saved it to disk as first.c. If you're using MinGW, then you want to save in c:\msys\1.0\home/[your_login] (if you're using a newer version of MSYS, then you'll have to replace the "1.0" with your version number). On MacOS X, you'll want to save it in /Users/[your_login] (you'll also want to add the "Terminal" program to your application bar below). To compile your program, open a terminal (All Unix systems, and Mac OS X), MSYS terminal (For MinGW/MSYS users), and type the following to compile your program (everyone else, refer to your compiler documentation first):

gcc first.c -o first

The -o parameter tells GCC to save the compiled binary as "first." It is very important that you don't ever type something like this:

gcc first -o first.c

This will cause your source file to be overwritten, because, instead of using "first" as your output file, you told gcc to use "first.c" the output file, which is your code! Yes, I've made this mistake a few times... Be careful. Later, we'll talk about how to use Makefiles which will greatly reduce the chance you'll make this kind of boo-boo.

Now, to run our program, all you need to do is type:


That's it! All done! If you're using MSYS, you'll want to use the MSYS shell for compiling your programs, but you'll want to use Windows command shell (cmd.exe, or, to run your programs due to some bugs in MSYS. Here's a few things you need to know about the the Windows command shell, if you're going to use it:

  • Use the backslash '\' to separate directory names, rather than the forward slash '/' (also the ? key on QWERTY keyboards).
  • cd command changes directories
  • dir lists the contents of the current directory
  • exit leaves the command shell

So, if you're using the Windows command shell, you'll want to type in the bold text to move you into the correct directory, and run your program (some directories will have different names on your system, so take that into account as you type):

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\aragorn>cd \msys

  Volume in drive C has no label.
  Volume Serial Number is F09B-B76D
 Directory of C:\msys

06/02/2005  02:26 AM    <DIR>          .
06/02/2005  02:26 AM    <DIR>          ..
06/02/2005  02:36 AM    <DIR>          1.0
               0 File(s)              0 bytes
               3 Dir(s)  19,877,498,880 bytes free

C:\>cd 1.0\home

 Volume in drive C has no label.
 Volume Serial Number is F09B-B76D

 Directory of C:\msys\1.0\home

06/02/2005  02:27 AM    <DIR>          .
06/02/2005  02:27 AM    <DIR>          ..
06/02/2005  02:41 AM    <DIR>          aragorn
               0 File(s)              0 bytes
               3 Dir(s)  19,877,482,496 bytes free

C:\msys\1.0\home>cd aragorn

Hello World!


Naturally, you'll want to replace "aragorn" with the name that shows up in your directory listing, same goes for "1.0". Once you're in this directory, you'll stay there until you exit the command shell.

To learn more about COMMAND.COM/CMD.EXE used by Microsoft operating systems, check out: Guides:Shells/COMMAND.COM-CMD.EXE

For the bash shell used by MSYS, Linux, and Mac OS X (Versions 10.2 and earlier default to using tcsh instead, but commands are similar unless writing shell scripts), see Guides:Shells/sh-bash.

Personal tools