Posted by : Waqas Javed Monday, 13 October 2014

Writing your first program in C/C++

According to me programming is not something to learn it comes to you automatically by practicing. Writing program is more affectionate in this respect then becoming bookish.
So here is your first program to start with try writing it and understanding it.

1.            // my first program in C
2.            #include <stdio.h>
3.            void main () {
4.                            printf("My first program");
5.            }

1.            // my first program in C++
2.            #include <iostream>
3.            using namespace std;
4.            void main () {
5.                            cout  <<  "My first program";
6.            }

My first program

Explaining of program in C

Line 1:  // my first program in C++
// indicates the start of comment all the text written in the line next too this will be ignored as programming line and itt will juxt be added in programming as it is. This is used for adding a comment to tell the other programmer about any instruction or guiding him.It is also used for the programmer to remember its own program. In this particular case our comment is about the description of our program.

Line 2:   #include <stdio.h>
Lines that start with # are not the regular programming lines these are the directives for pre-processor to add the piece of code already made by different programmer which are most often used so they are already added to the software so that time may be saved and all programmer don’t have to write that same code every time. A programmer could also make its own directives.
In our particular case we have added <stdio.h>  which is standard input output header file. As we have to produce output on screen.
Line 3:  void main () {
Every program must have at least one function that is its main function. No program could run without a main function you may add multiple function in a program  but the program will start from its main function. Consider the construction of our main function in this program void main () {}
main is the name of our function followed by parenthesis () because it is the declaration of function.
In parenthesis we may define any parameter for our function they are empty because our program have no parameter. void is the return type of function return type of any function must be defined it is the expression or value which the function will return after its execution. We have used (void) which mean empty our function will not return any value after its execution. The next thing is {} anything written inside this will be the statement of our main program and our function consist of these instruction.
It is also called body of the function.
Line 4: printf("My first program");
This line is statement. A statement is expression that produces some effect. In our program this is the only line that produces some effect. printf() is command to take out put in C lang. inside parenthesis () we write what to output.  Anything written within ”” will be shown as it. printf is the function statement that comes from the <stdio.h> that is why we have included that file. If we want to take output in any other form other than standard output on screen then we have to use the different directive or make one of our own.
; is the most important part of the statement this is the indication of the end of the statement to the compiler every single statement must be followed by this semi colon otherwise there will be an error.
Line 5: }
Is the end of our main function body.
Explanation in C++ for same program.
All the things remains the same as in C. the changes are as follow.
·         Iostream is the standard input output directive in C++.
·         Instead of printf(“”);  structure  cout << “ ” ; syntax is used.
·         3rd line in C++ that is using namespace std;
All the elements of standard C++ library are stored in namespace std so most of source file will contain this statement in start as we want to use C++ element cout is one of them used in our program.

Try writing this program to check your self. Practice make a man perfect. Best of luck. Hope our blog will help you to reach to the destination of a good programmer.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post

Powered by Blogger.

- Copyright © BSCS -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -