Introduction - Simple Basics

    We are going to begin with a simple 'Hello World' program. You might have heard of 'Hello World' before, as it serves it's purpose of being the most simple code you can program, which is functional.

    Example Code In Detail

    /*
    		HELLO WORLD     main.c      Reliant Code
    		The beginning of Coding
    */

    At the top, we can see a message which is encapsulated by /* and */, which allows you to write anything you want as it is called Commenting Block, and this is the part of the code which the compiler will ignore and it will not affect your program. As it uses /* and */, it can stretch it as a block to as many lines as you like; as long as you close it accordingly with */. If you want to comment just one line, begin with //. It is good practice to place comments, especially to document any work.

    #include <stdio.h>

    Now at the beginning of the code, there is a #include preprocessor. Don't panic, nothing too complex, as most of the hard work is done internally. A preprocessor is an instruction inside C code, which the linker and compiler does its work internally. Preprocessor Directives begin with a # character, which you will see declarations and calling predefined instructions running inside the compiler linking with the source/header code file or pre-declared functions it may refer to. In this case, we are using #include, which includes the header file which we will need for compiling the code. The header that is used for now is <stdio.h>, which is needed for the functions we are using in our example. The stdio.h file is linked on your hard drive in the directory where all your standard C library headers are. We will cover the importance of header files gradually over the tutorials.

    In C, to make a program run code, you have to call functions. Functions are a self-contained sections of code that perform the tasks you assign in the program. In this case, we are creating the entrance point, in which the programs begins at. That function which interfaces as the starting point is int main(); when the compiler begins, it usually looks for this function as the main entry, hence the name main(). As you can see, it begins with int, which is a standard data type keyword variable, which returns an integer value at the end of the function (Hence the return 0;, which will will cover return functions in more depth later). Then the function name, which in this case main is followed by the parenthesis (curved closed brackets). As the parenthesis is empty, it means that this function does not take any arguments into it's parameters (we will go into that later!).

    Now We will enter into the main function, the start and end of each function you create will be marked by curly braces ('{' and '}'). This is always the case, as C is a curly bracket programming language. Now we are using some real C code, at least the most simple. In C, the core functions of the language, which are declared in our linked header file stdio.h, the library interface link to the C Standard Input/Output Library (stdio). Within this library, there are many input and output function for you to use in your program, in this case we are using the statement call function printf.

    printf("Hello World \n");

    The beauty of most functions in the general C Standard I/O Library is it's useful flexible functions. There are plenty of general-purpose functions, which will be useful for a simple program. As you can see in our code, we are calling printf from stdio.h, as we depend on this function to display simple output in our program. Inside the parenthesis, you can see the 'Hello World \n' string inside inverted commas (""). This is because the parameter inside asks for a character string (char*) argument, which we provide here. Then it will print out the string on screen. As you have noticed, at the end of the Hello World string is followed by \n, which basically means newline. It is the equivalent of a single character that simply enters into a new line, but instead uses a backslash followed by a character (in this case it is a n for newline). These are part of what are known as Escape Characters (More on these later...). There are many other features inside printf, which will become more apparent in later C tutorials. At the end of a statement call, it is always followed by a semicolon ';'.

    getchar();

    The following statement function called getchar(), is needed to allow the user to close the application by pressing the Enter key. Without this, the program will exit before the user notices it beginning. In many IDE's and compilers, this is necessary for this reason. The main usage of the getchar function is that it reads an inputted char, so it has input capabilities which is accessible via stdio.h.

    return 0;

    Now it is the end of the program, as we stated earlier, it is important to end the int main function by calling return 0;. It is important in your function to use a returnable data type variable (in this case a int). So we return 0;. The number zero is best used, as it is usually means that the program has exited with success. Sometimes, in larger programs, if there is an error and it prompts the program to close abruptly, it would return another value, for example return -1;. More on that in later tutorials.

    Summary

    This is a beginning, which we can definitely build on. The Code accompanying this article will display the full source code.

    /*
    	HELLO WORLD     main.c      Reliant Code
    	The beginning of Coding
    */
    
    #include <stdio.h>
    
    int main()
    {
    	printf("Hello World \n");
    	getchar();
    	return 0;
    }
    
    blog comments powered by Disqus
    Download Files Here

    PDF Version Here

    Download this Article in PDF

    Feedback on Article

    Report Bugs, Errors or Send Feedback

    MD5 Hash Check : What is MD5?