This tutorial describes the structure of programs in C , C++ and Java programming languages through "Hello World!" program example. This will help the new programmers a lot to understand the syntax of programs in these programming languages through this comparitive study.
As you know, The Turbo C++ or Dev C++ etc. compilers can be used for coding C & C++ Programs. We save the programs as name.c and name.cpp for C and C++ programs respectively.
Netbeans IDE or Eclipse platforms can be used for making applications in Java. Both provides a good environment for developing java applications.
The ' Hello World! ' program simply prints a line displaying Hello World! as output.
Let`s get the comparative study through "Hello World!" Program Example :
Hello World! Program in C :
#include<stdio.h>
#include<conio.h>
main()
{
printf("Hello World!"); /*This is a statement */
getch();
}
- The inclusion of the statements #include<stdio.h> and #include<conio.h> at the beginning uses the input/output library functions.These stands for standard input output and console input output respectively.The lines beginning with # are the directives for the pre-processor.
- We included #include<conio.h> for the getch() which stands for getcharacter.
Note : If we use return 0 instead of getch() , there is no need to include #include<conio.h> library function.
- Always remember , there is only one main() function in every C Program.The opening brace '{' marks the beginning of the main function and closing brace '}' indicates the ending of the function.
- The lines between these /*...............*/ are comment lines.Comments have no effect on the execution time.
- Every statement should end with a semicolon ( ; ) mark.
Hello World! Program in C++ :
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
cout<<"Hello World!"; // This is a statement.
getch();
}
- #include <iostream> includes the declaration of standard input-output library in C++.
- All the elements of standard C++ library are declared within a namespace,the namespace with the name std is used in C++ programs.
- The execution of the program starts from the main() function in C++. ' ( ) ' is the pair of parenthesis.
- cout is named for the standard output stream in C++. cout is declared in iostream standard file.
- We included #include<conio.h> for the getch() , If we use return 0 instead of getch() , there is no need to include #include<conio.h> library function.
- Every statement should end with a semicolon ( ; ) mark.
- The lines after the ' //.............. ' are comment lines. // is used for comments in C++. /*...............*/ can also be used.
Note : //............... works only for single line comment while /*...............*/ are used for block comments means a no. of lines will be considered as comments between /*...............*/ .Comments have no effect on the execution time.
Hello World! Program in Java :
/** This is a simple application in java to
* print Hello World! message */
{
public static void main (String[] args)
{
System.out.println("Hello World!");
}
}
- Hello is the class_name in this program. i.e. class class_name is the declaration.
- The main method is similar to the main function in C and C++.It is the main entry in the program where the execution of the program begins.
The main method accepts a single argument. i.e. an array of elements of type String
The modifiers public and static can be in any order. i.e. public static or static public.
System.out.println("Hello World!"); This uses the System class from the core library to print the message to standard output.
/*......block comment......*/ , //.....line comment...... and /**.....document comments.....*/
all are used in Java Programs.Comments have no effect on execution time.
- Every statement should end with a semicolon ( ; ) mark.
Here, I tried to explain a comparative study using Hello World! program so that you can easily understand the basic syntax ( structure of the program ) of the program in C , C++ and Java.
[Read] C vs C++ vs Java
Enjoy the Programming.
Have Fun!
No comments:
Post a Comment