How To Program in C

Simple Program "Hello World"

So you're interested in how to program in C. C is a programming language well known for its power, accessibility through hardware level (low-level programming), efficiency and speed in execution of programs, and its portability to be processed in different operating systems with little or no alteration. In this article I'll try my best to guide you step-by-step building a simple program, nothing too fancy; in this case, we'll just print characters to the screen, which globally is known as Hello World program.

  1. First, in order to program, we need a "place" or environment to type our code. Run an IDE C compiler such as (MSVC++, DevCpp, MinGW, etc); even though the compiler is specifically for C++, it's also support for C. Try a free IDE C compiler from BloodShed DevCpp.
  2. Make a project file. Click New, then choose to make a new project and name it as 'myproject' (the extension of file project varies with each compiler). Also check the parameter check box in the option of dialog box to clarify that this program will be a C code so it won't compile it as C++ code. Since different IDE compilers have their own interface on project options, it would be lots of things to be covered in this article alone. For example, with IDE MSVC++ (Microsoft Visual C++), we should configure the project options so it would make a console application that compiles as C code. This will make sure the compiler won't compile it to other code, like Win32, C++.
  3. Adding the project a C file. Every IDE has a hierarchy panel. Right click the root of hierarchy of project file then click new file, name it as 'main', and make sure you've chosen the option to make it a C file (it will make the file end with extension .c).
  4. Coding your C program. Ok now that we have an environment to code and the project file is set, next will be our main course. We will make a simple program that prints characters to the screen. How? Time to code:

           #include <stdio.h>
           int main()


           {

           printf("Hello World!");

           return 0;

           }

    I will explain what each code does.

    • '#include <stdio.h>' This is a preprocessor directive; with '#include <anyfile>' statement, the compiler will look at any special file inside the bracket (that is in our program 'stdio.h') at include directory of root IDE compiler's directory to be inserted in code at compiling state. The information contained in this file is essential for the proper functioning of the library function -- for example the function that used in this program is 'printf()'.
    • A file 'stdio.h' is a header file that needs to be included for functioning 'printf()', a common function that prints characters on screen.
    • Function 'main()' is where flow of program goes; the compiler will start compiling each instruction at the beginning of the first line and end it at the last line of instruction statement of function 'main()'.
    • A 'return 0', is a return value for function 'main()'; zero means there's nothing to be returned. This is also known as 'void' type.
  5. Compile and build the code as an executable file. In order to run it, the code needs to be compiled and built as an executable file. Look at the main toolbars search 'build' tab, then click 'compile'. The compiler will look at the code and analyze its syntax. If everything goes well, no errors were found and then it's ready to be built. Click 'build', and compiler will process every instruction on each line as machine code (binary code), then resulting in an executable file with extension .exe.
  6. Done. You can try running the program, look in the project directory where the .exe file is located. The output would just print 'Hello World!' in your console box application (MSDOS).

Well this is just a start for you. There many other subjects that you can learn more in depth to master programming C.

 

Share this article!

Follow us!

Find more helpful articles: