Back to A Brief Guide to C


The Basics, Distilled


  • Whitespace doesn't matter
    So this:
    y = x + w;
    Is equivalent to this:
    y           = x
    +
    w;
    ...But the first one was a lot more readable.
  • Semicolons End Statements
  • Curly braces start and end functions, structures, multiline loops, and multiline if statements

A very basic program



Explanations for each line number is below. Note: The line numbers are not neccessary for a normal program -- in fact, they'll cause all sorts of problems. I've included them here in order to make line identification easier
01 #include <stdio.h>
02 #include "my.h"

04 #define FOO 42

06 const int naughty = 5;  /* this is a terrible thing to do*/

08 void main() {
09	int a,
	    b, 
	    c; /* you could also put these on the same line*/
13	char letter = 'c';
	
15	b = 3;
16	c = 5;
17	a = b * c;

19	if (a == FOO) {
20	   printf ("Howdy, World!");
21	   a++;
22	}
23	else
24	   printf ("Hello world!");

26	return();
27 }
First off, don't worry if you don't understand everything (or anything) about what the program is doing (mostly because it doesn't do anything important).

A line-by-line of the above program

  • line 01: This is a preproccessor directive. This means it isn't actually compiled into the program, but it gives the compiler instructions -- in this case, it tells the compiler to include the stdio header file in the following program. The brackets tell the compiler to look in the "normal place" for the library (yeah, I know that's confusing; hopefully it's predefined on your system and you won't have to fiddle with it)
  • line 02: This is another include statement, but this one is telling the compiler to include the my.h header file, but the quotes tell it to look in the current directory for the file. You can put a path here, if you have your headers in a subdirectory.
  • line 04: A define statement. This acts like a search-and-replace on this file. Before the file is compiled, any string matching the first arguement (in this case, 'FOO') will be replaced with the second arguement ('42').
  • line 06: A global constant. For the love of god, don't use globals. They are just bad. Trust me. This is a variable that's defined throughout the entire program, instead of just within functions. See C: Functions for more info on this.

    You can also see a comment on this line. It's the line between /* and */. It's ignored by the compiler, but can make your program more understandable.
  • line 08: main() - This is the function that must be in every program (well, okay, not really, but it will until you get much more advanced). In very simple programs, you can include everything in the main() function, but anything useful usually uses main as high-level program control and relegates all code to functions. The word "void" before the main() means the main function will not return any value. If you want to return something useful (in most operating systems, a program returns a '0' when it exits correctly, while a different exit value means something went wrong), say int main().

    Note the curly brace on the same line as the main () statement. This is something of a holy war (similar to the emacs vi disjunction -- in other words, silly). There are those who believe that the curly brace should be put on the next line, aligned with the same column as the start of the function / statement, like:
    void main ()
    {

    They are, of course, wrong :-). Of course, choose whichever looks better to you -- I prefer the compactness of same-line curlybracing, so I use that throughout the guide.
  • line 09: Variable declaration. This line declares a variable (that is, takes a chunk of memory and gives it a name for easy use) of type 'int' (see C: Data Types for a guide to these). The comma at the end of the line means "there's more to come."

    The variable must not start with a number, and may only contain letters, digits, and underscores.
  • line 10: Another variable.
  • line 11: The last variable in this statement (see the semicolon at the end?). I usually put multiple variables of the same type in one statement, but on different lines tabbed out to align with each other. Again, this is mostly personal preference (though in a job situation this may all be law rather than preference). Also another comment.
  • lines 15 through 17: Some assignments and mathematics. See C: Operators for more on these and other operators.
  • line 19: An if statement. Note the curly braces: If the test (a == FOO) returns '1' (or true), the code within the curly braces is executed; otherwise, everything until the closing curly brace (line 22) is ignored.
  • line 23: An else statement is executed only if the previous if() statement fails. In this case, there is only one line to be executed, so the curly braces are optional. I've omitted them here.
  • line 26: The return() statement forces the program to exit at this point. If the main function was written to return a value (see my description of line 08), you would put a value within the paren's which the program would return to the os. In this case, I created main as type void, so it cannot return anything.

Log in or registerto write something here or to contact authors.