Elaboration the First Program in C/C++


In the previous section, I have discussed how to download and install Visual Studio 2010. In this section, I'll elaborate the program line by line. So you will get a clear concept on every line of code and get the basic concept about any C/C++ source code. Let's try...............


The smallest C/C++ Program

C/C++ provides the easiest starting programming concept. You have to do nothing but a few line of codes. Actually it contains only one line code.  Look the below code attentively......  

                                                      void main()
                       {

                       }

If you compile/debug the above program, you will find no error but no output. You are thinking why? Yes, I'll give you the answer. But before that you should try it. So, go to your installed editor and put this code according I told previous lecture. Now press Ctrl+F5 to Run/ Built the program.  You will find a output like below figure... 


Output


You can see nothing but Press any key to continue...  Don't worry, this text is supplied by Visual Studio. Without it nothing is found but compiler is found no error. Why? The answer is discussed here line by line.

void keyword 

The first line contains void main(). The first word void is a keyword in C/C++ language which indicates  nothing to return. I'll discuss details about this keyword when discussion about data type will come to the later section. Here just remember that it return nothing.  

main() Function

The main() is a function. I'll elaborate later about function when function is discussed. C compiler knows only this function when program is executed. If you don't use this function, compiler shows an error because the doesn't contain main() function. So every C/C++ program  should have the main() function otherwise the program will not be executed. The curly brac ( {} ) indicates the body of the function. Anything you want to do must write in this body otherwise that will not be executed.

What will happen when the program is executed

When you run the program, C/C++ compiler will try to find main() function.When main() funtion is found, compiler calls this funtion and tells the main() funtion to execute its body and to call other other functions written into its body. Remember that program is executed line by line. The programming code which will come first in the main() function's body will execute first. So,the code which you want to execute first should put first in this body. 


First C Program to Write a Message to the Screen

At this moment, you will see a little and successful complete program which will display a message to the screen. To try it, edit this code with visual studio 2010 according to the first program and compile to get result.  

                                                #include<stdio.h>

                                                void main()
                                                  {
                                                       printf("Welcome to C/C++ Programming\n");
                                                  }


The below figure containing the compiling result.

Output result

To display an output we have used a C/C++ build-in function printf() which will display anything written between quotation (" ") in the parenthesis. As we write Welcome to C/C++ Programming, we can see this message on the screen. Another line above the main() function is also written here. Why do we write it and what is it? This is called header file and we use it because we have used a build-in function what definition is written in stdio.h file. You must include this line because if you don't use this, the compiler cannot find the definition of printf function. So you will find an error and program don't execute. Here we have used an escape code \n which is used to go to new line.


First C++output program

The above discussed program is written as basic C programming style. The C++ compiler contains not only all the features written in C but also its native features including  object-oriented concept which will be discussed later. Now we shall see a small C++ native program.

                                             #include<iostream>
                                               using namespace std;
                                              void main()
                                                 {
                                                    cout<<"Welcome to C/C++ Programming"<<endl;
                                                 }


The output program is shown below-


C++ Output Program




Now we shall be familiar with the newly coming syntax in this program.
   
#include<iostream>  
The line which starts with a hash(#) sign is directives for the preprocessor. Its an indication of compiler's preprocessors. This line tells the compiler to include iostream file which contains standard input-output file that will be used in this program.

using namespace std ; 
The namespace used in C++ program contains all the element of the standard C++ library. Hence we have used a namespace named std which contains standard input-output library. Without it, you will find an error.

cout<<"Welcome to C/C++ Programming"<<endl;
This is C++ stylish program statement. cout indicates standard output stream used in C++ program. Anything written after two greater than (<<) sign will be displayed on the screen. At last we've used a C++ command named endl that is used to make a new line like \n scape code in C language. We also see a semicolon (;) that indicates the end of a statement and must be used to make error free code.

In this section, you learned the basics of first program both in C and C++ Programming. Now you are able to write any error free program. In the next section, you will know the Data Type and Variable concept used in C/C++ programming language

 

1 comment: