We now know how to run a C program in linux. So its time to understand the inner workings of a program. For this purpose we will take a small program and dissect and understand it one part at a time. The whole program may look confusing but understanding each component in isolation will make things simple and clear.
So lets take this example. A simple program that prints a line on the screen.
/* Title: code that prints a line. Author: Roshan Tirkey */ #include<stdio.h> int main(){ // no need for local variables printf("This is my first program\n"); return 0; }
**Its important that you understand the basics correctly, hence if there is anything that you find confusing or needs to be explained in detail please comment below. Finally the intent of the blog is to make things simpler for you to understand, and anything that deviates us from doing that should be removed.
The code above can be broken down into five parts, each of which you will be able to reuse in your programs. Each component is listed below and discussed in detail.
- The comments
- Single line comments
- Multi line comments
- The preprocessor statement
- The main function
- The printf statement
- The return statement
The Comments
Comments are never processed. As a matter of fact when the code is converted into binary code the comments and all the white-spaces are neglected, since the compiler only understands the commands.
The reason why one should include comments in his programs is that as the complexity of the program increases it becomes more and more difficult for other programmers to understand. Hence is a good practice to include comments, starting with your first program.
Single-line comments
A single line comment is used to comment out only one line of code. Anything written after two backslashes "//" will be considered a comment. Hence you can write a line of code followed by // and explain it.
Example:
printf("Hello\n"); // prints Hello followed by new line
You can also use comments to keep some line of code from executing. This technique comes handy when debugging your code, as you can comment out some part of the code and check if the error has disappeared. The example below will show how its done.
//printf("Hello User\n"); printf("Welcome User\n");
Multi-line comments
You can make use of multi-line comments to add a description for your program, describing attributes like author, date of creation, title of the program along with a short description of what it does.
Alternately you can also used it to comment out a block of code. This way you can un-comment it whenever you want. This comes in handy while debugging. Let me demonstrate how its done with this example
#include<stdio.h> int main(){ /* printf("Welcome\n"); printf("Good Morning\n"); */ printf("Hello\n"); printf("Good Afternoon\n"); return 0; }
The proprocessor statement
The pre-processor statements allow us to make use of functions that are already implemented. The pre-processor statements are made of two parts
- include - The include keyword
- stdio.h - The file where the functions are already implemented
When the compiler reads this statement, the file is fetched and the functions that we require are linked to our program. That way our program becomes independent of other files.
The main function
The main program is the entry point for the execution of our program. Each line written within the main program block is executed sequentially. Hence a C program cannot run without a main program.
The printf statement
The printf statement is the functionality we import from stdio.h. This function allows us to print a string(a message) on the terminal(stdout). Later as we learn data types we will use it to format text as well.
**The "\n" is a placeholder for a new line.
The return statement
The return statement, as the name suggests returns a value. Since the main function has a return type of int, it is mandatory for us to return an integer value. A main function returning a value 0 is considered to have executed successfully.
**Note that anything written below the return statement, in that block, will not be executed since the execution of that function is stopped and the value returned.
*Indentation
As you can see the code we considered was easy to read, and one can easily figure out which lines of code belong within the main function. Making use of tabs allows us to describe the hierarchy withing the program. This process of formatting the program is called indentation. Incorporating indentation will not only make your code look beautiful but it will also convey more meaning and hence become easier to read.
**Indentation comes in handy when debugging the code.
Do it yourself
- Try to print "Hello Everyone".
- Place return 0; above printf and see what happens.
- Comment the printf command ( type // before it ) and see how the programs works.
No comments:
Post a Comment