Hello Word Program in C with Code::Blocks

In my last tutorial on C, we discussed about C compiler and how to install Code::Blocks IDE since we are going to use it for the remaining part of the tutorials.

In today’s tutorial, we are going to write our first C program which is “Hello Word” program. 

Hello word program is generally accepted program a beginner in every language should start with.

For readers, that have experience with other programming languages, this might not be a big issue to crack.

The content of this tutorial will not only focus on one line of code that prints hello word rather we will learn about C program structure and how to navigate through Code::Blocks.

Navigating Through Code::Blocks

We did not explore code::blocks the last time after our installation.

It is important for us to learn about few functions we will constantly use whenever we work in code::blocks.

It will saves us time and enhance our productivity.

Now, lets return back where we left our Code::Blocks after installation.

1. Create a new C Project

There two ways to create a new C project in codeblocks.

  1. In the start here page above, there is a link called create a new project. You can click on this link to initiate creation of new project.
  2. Go to the top most left side menu, click on File to open a dialog.
  3. Navigate through File > New > Project > Console Application > Select C > Fill in your project details > Complete the new project creation.

The steps above and represent with images to aid in better understanding.

Click on File menu
Click on Project Submenu
Select Console Application
Select C language
Enter your project title and path

Open our new C project

Open your new project. If you can’t see main.c file open in the work space, go to your project source folder and double clicked to open its content.

Double click on the main.c file to open the content of the in work space.

We will go though the template code provide for us by Code::Blocks.

C program structure

Take a moment and look at the code above and check if you understand what is going on there.

If you do understand it is good news but if not, don’t panic. Remember that professional today are once beginners yesterday.

We will from C header file and move down until we get to the end.

1. What is C header File

If you remember our tutorial on introduction to C programming, we said that C is a very small language which cannot necessary stand on its own.

In other for C language to be meaningful and used to solve real problems, C basically depends on built-in libraries.

These libraries contribute to the complexity of C language.

Header files can be built-in or user define files use to extend the functionalities of C.

C header files has the .h file extension. They contain prototypes and preprocessor directives. If you are familiar with Java, adding C header files is similar to importing java libraries in Java program.

To include a header file in C, we use the #include preprocessor, we have briefly explained C preprocessor in our previous tutorial but don’t worry we will still go into details in future tutorial.

Lets go back to our code to see the header files present.

#include <stdio.h>
#include <stdlib.h>

So what it implies is we are making use of the C standard input and output header file and C standard library.

These header files will give us access to many methods we can just call or use in our project.

An example in our code is printf() method.

printf("Hello world!\n");

We did not create the printf() method but we get access to it from the include header files.

C main() method

The main() function in C programming is the entry point or starting point of C program execution. 

Every code enclosed in the main() function curly blanket must be executed.

The main() function has int data return type. We will learn about data types and return types in our future tutorial.

For a C project to run, it must contain a main() function in one of its c files

Now that we have an understanding of what C main method is about, let us process to look at what we have inside this method.

return 0 or return EXIT_SUCCESS

At the end of the main() function in C program, this always this line of code below.

return 0;

Since the main() function has a return type int, so an int must be return at the end of the function.

The int value represents the status of the application.

0 signifies that the application has been terminate without any issues while 1 signifies and encountered error.

In a more readable form, the 0 can also be replace with EXIT_SUCCESS.

printf() function

Finally, let talk about the last line of code in our program – printf() function.

print() is a C library function we can use to output information to the screen. 

Like we said when we discusses header file, printf() function is a part of stdio.h file we included at the beginning of our file.

The content of the text to display is wrapped with a double quote.

Our complete C hello word program

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Hello world!\n");
    return 0;
}

Run C Program in Code::Blocks

You can choose to run your C program using the command line interface. This tutorial will not cover the user of command line interface for C program execution.

It is very easy and simple to run your C program in Code::Blocks. Below is an image of the run icon. It requires a single button click.

The circled triangular is the run button while the little triangular button besides it is for compile and run.

If you have change your code you will need to recompile it and run again. 

If not you can just hit on the run button to run the code.

Below is the result of the code.

Things to keep in mind

As you continue your journey of becoming a great C or embedded C developer, there are few things that should serve as your guiding principles

  1. Always make sure you end a statement with semi-colon
  2. Avoid any kind of typo specially for library functions
  3. C is case-sensitive so lowercase and uppercase are different
  4. You will learn fast by practicing code examples. Do just focus on reading code.
  5. Do wait until when you understand every concept before you start programming.
  6. Look for someone good in C as a mentor
  7. Always follow a good programming practice
  8. Aspire to always make your code readable

Some programming exercise

I have added some programming exercise based on what we have learned. You should make out time for it, This is the best way to learn.

You should write a C program that will give the outputs below.

Summary

We have spent time trying to understand everything we need before digging deeper into C program.

We also mention important things a new developer should have in mind while learning C programming.

If you have any questions or suggestions, kindly contact me or drop a message in the comment box.

We are going to explore C Data Types in our next tutorial. If your are already, click the button bellow.

C Data Types

Add a Comment