Writing your first C Program

Writing your first C program

You have finally arrived at this point in your software engineering journey. You might have countless years of programming experience behind you in other languages. You might even be completely new to the entire topic. Not that it matters. This article will be kept as short as possible and it will be easy.  The only assumption made when writing this is that you already have your environment set-up so you can write code.

Source code. At a glance

When writing a program in C, the most common file type you will come across is the “C source code file”. You can actually write these files in most simple text editors. You will usually be able to distinguish these files by their file extension (.c). Let’s take a walk through the way you might wish to write such a file.

Try to get into the habit of placing a comment at the top
Although it is optional, when you create a new .c file, it is always useful to add comments at the very top. This can help to explain what the code in the file is actually for, it could even contain some copyright or warranty information, along with any disclaimers or legal statements you may wish to add.

/* 
 * Program which prints a simple message to the screen. 
 * I have not patented this code, reuse it as you wish.
 * I am not responsible for any damage you may do with
 * Any information, ever.
 */

In the example above. There are a few things you should note the following :

  •  The start of a comment block is denoted with the characters /*
  •  The * characters in the middle lines are actually optional, they are purely for decoration.
  • The end of a comment block is denoted with the characters */
  • All text in between the start and end of comment characters are comments. None of this is functional code.

Writing actual C code.
If you read the actual comment example above, you will likely have guessed by now that the goal for this tutorial is to show you, the programmer, how to print a simple message to the terminal window. Please see the example below. It contains comments which are used to show you what the code is doing.

/* Program which prints a simple message to the screen.
 * I have not patented this code, reuse it as you wish.
 * I am not responsible for any damage you may do with
 * Any information, ever.
 */

/* Contrary to the big reputation it has, C is actually a tiny
 * language in terms of actual functional content. In fact, without 
 * including external libraries you can almost do nothing at all. 
 * For standard terminal input - output you need to include stdio.h. 
 * The include syntax is '#include'
 */

#include <stdio.h>

/* If you are writing the entry point of your program, it must be 
 * called main. That is how the computer will know which part of the 
 * program to run first. It is considered good practice to give it a 
 * "return type" of "int" as shown below. "int" is short for integer,
 * and having a return type of int just means that when the function 
 * ends, you will be given an integer as the result. Which is useful 
 * for error checking, for example.
 */

int main(void)
{
    /* printf is part of the functionality provided by stdio.h
     * The message inside the braces is what we want to replace.
     * Feel free to replace it with your own custom message.
     * the "\n" is used to tell the computer running the application
     * that you
     * wish to take a new line.
     */
    printf("Hello World\n");

    /* Very little can break in this example, return 0 for success */
    return 0;
}

Code complete

You have written your first C program. What you have here is simple output to a terminal. It will print a message to the terminal window that you execute it in. You have taken your first look at the main() function of C, and you have had a (brief) introduction to basic output via the stdio.h header file. Future articles will build on this knowledge to help you learn more C. Feel free to compile and run your code. An example of compiling this code with gcc can be found in my article, “Compiling and running a C program

Some more information on what this article covered

If you are new to programming, I realize you may have questions. Whilst I can’t wait to hear questions and answer to the best of my knowledge, I recommend that you read this little section to get more understanding, as it might just answer anything you have to ask right now.

 A better look at int main()

When you compile a C program you get a binary which can be ran on a computer. When you attempt to run that binary the code which runs first is the main() function. Simply put, if you don’t have a main() function, the program will not run.

The main function in my example code above, explains that it has a return type of int. This means that when that function completes, the computer will get the result back at the end in the form of an integer (or number, to normal people like you and I). The standard in C programming is that the computer can use this number to decide if the program terminated with a successful result, or if it failed in its mission. The example returns 0. When a program returns 0, that means it completed successfully. If we had some failure scenarios in our code, you could return any other value, such as -1, to denote an error. But we will talk about that in another article. Lets not get ahead of ourselves.

A better explanation of what the printf() function does

printf() is used when you wish to display a form of formatted output. In the example shown in this article I have only shown it outputting a simple screen, but if you want to check out my in depth explanation of printf() you can click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>