Sunday, August 13, 2017

Say Hello World in C


Welcome dears to say Hello World in C Programming Language with me.
To write code we can use any text editor but every text editor doesn't allow to build and compile or run C program code. Because GCC compilers are not integrated to each editor. So we need a compiler that will allow us to build, compile and run code beside editing.

One of them most popular is Code::Block IDE. You can also use Linux default Terminal to run and compile. Sublime Text Editor also allows to run C/C++ by installing some code.

Whatever, I recommend to use Code::Block IDE for beginners.
So first, download Code::Block from official website and install manually.If you face any trouble to install then you can put your problem through commenting.

How to Run C Program in Code::Block:

Say Hello World in C
It is obviously big challenge for beginners to run C program in Code::Block as a new programmer in programming world. To run C Program in Code::Block you have to follow the instructions step-by-step:
  1. Open or Launch your CodeBlock.
  2. After launching create a new file with Ctrl + Shift + N or manually from File → New → Empty File
  3. Paste the following code without any modification and save in C file with Ctrl + S and choose C/C++ File extension.
    #include <stdio.h>
    int main()
    {
        printf("Hello World\n"); 
        //This function will print the value within braces.
        return 0;
    }
    Before see the output of this program we have to do two works: Compile/Build and Run.
  4. Now compile the program by Ctrl + Shift + F9 or from Toolbar Build → Compile Current File
  5. Run the compiled program by Ctrl + F9 or from Toolbar Build → Run We can both two works (Step 4 & 5) together by F9.
You will see a pop box that is showing a result of ‘Hello World’.
Wow !  You have just printed Hello World. You have ordered computer to show Hello World !

Explanation: What say the above code
  1. In 1st Line, #include <stdio.h>
    This is called ‘Header Section’ - where Header files are included to enable related function to work. In this program, stdio.h (Standard Input Output Header File) is a Header file for printing outputs and scanning inputs. So under this header file printf(), scanf() are the built in functions.
    There has many header file as stdio.h, such as math.h, string.h, stdlib.h etc.
    *** Don’t put space between # and include.
  2. In 2nd Line
    Every C Program has one main function (main()) and what we want to do by program, we have to put everything within brackets. So the main function starts with { and ends with }.
  3. Example:
    int main()
    {
    
    }
    

    Question: Why int before main function?

    Ans: We have to declare before starting with any function in C of what type of result will be returned by this function. Int is for integer type function that will return an integer type value. By default, you should use int type function before main function using.
  4. Now focus on the following statement:
    printf("Hello World\n"); We have said before about printf() function that print outputs. The above statement will print a message of ‘Hello World’ with a newline. You can print whatever by putting sentence, word, number within "". You can try some funny message. Look, the statement finished with a semicolon. Yes, in C programming language, every statement finished with semicolon (;).

    Example:
    
    scanf(“%d”, x);
    x + y = 20;
    int x;
    

    Question:  Is it required to put a semicolon after each statement?

    Ans: Yes.
  5. Now focus on the next statement of the given program.
    //This function will print the value within braces.
    We can see a sentence start after // and for that this sentence is not shown in output result after compiling and running. This is called commenting and it is one of the commenting system know as line commenting. The other comment system is block comment. This comment system start after /* and end before */.

    Example:
    /*This is comment system.
    You can avoid it. 
    */
  6. return 0;

    Since main() is a function and we know every function returns a value. In this program we don’t want to do more after printing “Hello World” message. So we have to stop the main function and we have put another statement return 0; which is for stopping program.

Question: Can I put a comment within comment (Nested Comment) like below?

/*Outer Comment
/*Inner Comment*/
*/

Question: Does it take excess execution time of compiling comment?

Ans: No.  

Exercise for this lesson

1) Write a program to print your name.
2) Write a program to print your email.
3) Find the errors from the following code.

# include <sdio.h>
int main{}
[
    printf("Hello World\n")
    /This function will print the value within braces.
    return 1;
]
Show me the results by commenting below. You can upload screen-shot of your answers/codes.