An Introduction to C by Daniel Mishler

In this guide, I would like you to come away with knowledge of:

  • How do I make a file?
  • How do I get started writing a program?
  • What is the difference between a program and a function?
  • How do I compile and run my program?
  • How do I know that my program is actually running?

    How do I make a file?

    For purposes of this lesson, a file is something on the computer that stores data, information, or commands. In our case, it stores the purest of all information: C Code.

    We make files in the terminal with vim.

    [dasmish@silo E111] vi firstProgram.c

    You could equivalently type:

    [dasmish@silo E111] vim firstProgram.c

    They're aliased to exactly the same thing.

    How do I get started writing a program

    We're going to write a function called main. Here is how we'll do it:
    int main(void)
    {

    }
    Of course, C is whitespace independent, so the computer would also understand this:

    int main(void) {

    }
    Even though C understands both functions, the former is the correct way to write the function and if you choose to do your braces in the style of the latter, you are a heathen and God shall strike you down for your sins.

    Jokes aside, what do the three words mean?

  • The first word, in this case int, is the data type of the value the function returns. In this case, it is an integer. But other valid words include (but are not limited to) double for a real number or void for nothing.
  • The second word, in this case main, is the name of the function.
  • The third word (or set of words), in this case void, are the function arguments . void means that no arguments are passed to this function, but other functions do often have arguments, and more than one. We'll cover that later when we look more in depth into functions.

    What is the difference between a program and a function?

    I just said I was writing a program, but I just told you about functions. What gives?

    A program is a file that contains a function called main.

    Please do whatever you need to do to remember that.

    There are a few special things about the main function:

  • It is always an int function. This return value is meant to signify to the parent program whether something went wrong. We will almost always have the function "return" 0.
  • You have two options for arguments of main:
    int main(void)
    or
    int main(int VariableNameA, char ** variableNameB)

    We will almost always use the former option until we start working with more advanced topics.

    So, let's finish our program. All we need to do is make sure that our function has a return value.

    int main(void)
    {
      return 0;
    }
    The return statement here is 3 spaces from the side (this is our coding style, but if you choose 4 spaces no one is going to get upset), and returns 0, meaning that no errors occurred during execution (later on, you might write more complicated programs that might return, say, 1 on an error).

    This means your program is done and ready to run. Time to save your work and run the code!

    Save in vim by typing (while not in insert mode) :w and then quit with :q. Or, you can save and then with with :wq.

    Now, time to compile and run our code. The compilation step is necessary because the computer can't "run" a C file. Perhaps you've coded in languages that the computer can "run" like Python, but there's a lot going on beneath the surface there! C does not hide it from you. You have to compile your C file into something that the computer can run. Do it like this:

    [dasmish@silo E111] gcc -ansi -g -Wall -Wpedantic -o firstExecutable firstProgram.c

    Each of these flags does something special for us, and I would highly recommending using all of the flags. Every time.

  • gcc is the compiler. It is the program you're running.
  • -ansi tells the compiler that our code complies with ANSI, and to compile according to that standard.
  • -g tells the compiler to include debugging information with our executable. Without it, we couldn't run ddd!
  • -Wall Enables compiler warnings. This is often incredibly helpful. Just because your code compiles doesn't mean that it does what you think it does, and warnings help you find those bugs!
  • -Wpedantic Enables warnings demanded by strict ISO C: these are your ANSI related warnings.
  • -o firstExecutable names the output file "firstExecutable". The "-o" flag means "the next argument will be the name of the executable." It's really helpful, since without it everything just gets named "a.out" on Silo, and it gets difficult to keep track of which executable is which.
  • firstProgram.c is the program we're compiling. The last argument is (at least for our purposes) always the program we're compiling.

    Okay, now our program is compiled. Don't believe me? Do an ls and see it for yourself.

    Now let's run our program! We can run it by typing:

    [dasmish@silo E111] ./firstExecutable

    Great, did it run?

    What's that? You didn't see anything? Well, that's exactly what you should expect, because our program doesn't do anything. Let's change that so you can see something.

    Open the file again

    [dasmish@silo E111] vi firstProgram.c

    And write

    #include <stdio.h>
    int main(void)
    {
      printf("Hello, World!");
      return 0;
    }

    Don't worry too much about the syntax of printf. You'll get the full lesson about it later. For now, just understand that you need it to communicate with the terminal and that in order to use it, you must have that "include" statement before main. Compile your program again

    [dasmish@silo E111] gcc -ansi -g -Wall -Wpedantic -o firstExecutable firstProgram.c

    And run it again (by the way, now is a good time to mention that you can use the up arrows to cycle through old commands)

    [dasmish@silo E111] ./firstExecutable

    Did you see something this time? Great! Looks like you've got your first program under your belt.

    E111 Homepage