Simple C Program or Basic Structure of a Program in C++

A look at simple C Program -1

Line 1:     // My First C Program
Line 2:     #include<stdio.h>
Line 3:     int main()
Line 4:          {
Line 5:           printf("Welcome To Your First Program");
Line 6:           return 0;
Line 7:          }

A look at simple C++ Program -2

Line 1:     // My First C++ Program
Line 2:     #include<iostream.h>
Line 3:     int main()
Line 4:          {
Line 5:           cout<<"Welcome To Your First Program";
Line 6:           return 0;
Line 7:          }

The program is the simplest program and includes some of basic elements that every program mainly have.

Lets have a look on these basic elements –

Line 1:   //My First C++ Program or My First C Program

This is a comment. lines begin with // are comments. These are the pieces of code that compiler never executes.
Purpose- It allows the programmer to insert some notes or descriptions to enhance readability or understandability of the program.

There are two ways in C or C++ to insert comments –
  1. Single Line Comments – By // . Compiler simply ignores  any line begining with // in that same line.
  2. Multiline or block comments – Everything between /* …… */ is considered as a comment. These are the block comments i.e. everything that falls between /* ….. */ is considered a comment even though it is spread across many lines.

For example,

int main()
     cout<<"Say Hello to Your First Program";     /* This is a simple 
                                                      program having one 
                                                      output statement 
                                                      for the purpose of
                                                      explaining comments. 
     return 0;

Line 2: #include<stdio.h> and #include<iostream.h> 

Simple C Program

stdio.h → Standard Input Output, where .h is the extension of header file
iostream.h → Input Output Stream, where .h is the extension of header file
stdio.h and iostream.h are header files which allows the program to perform IO operation in C and C++ respectively.

Line 3: int main()

Simple C Program

Line 4 to Line 7 :

Simple C Program

In c++, to print a statement, “cout” statement is used,
while in C programming language, the “printf();” statement is used to perform to print the statement.

Previous Home Next
Basic Introduction of C (Day1) Basic input output in C++ -( Day3)


Leave a Reply