PROGRAM STRUCTURE

C LANGUAGE:

  • C language program starts with the defining/including the libraries header file. Header file is denoted by the extension “.h”. All these are pre-processor commands.

Ex:  #include <stdio.h>

What are these library functions?

Library functions in C language are inbuilt functions which are grouped together and placed in a common place called library. Each library function performs specific operation. These library functions are used to get the pre-defined output instead of writing our own function for these outputs.

When we include header files in our C program using “#include<library_name.h>” command, all C code of the header files are included in C program. Then, this C program is compiled by compiler and executed.

Some mostly used library functions are described below:

LIBRARY HEADER FILEDESCRIPTION
stdio.hThis is standard input/output header file in which input/output functions are declared
conio.hThis is console input/output header file
math.hAll maths related functions are defined in this header file
stdlib.hThis header file contains general functions used in C programs
time.hThis header file contains time and clock related functions
string.hAll string related functions are defined in this header file
errno.hError handling functions are given in this file
ctype.hAll character handling functions are defined in this header file
stdarg.hVariable argument functions are declared in this header file
  • Next it starts with the definition of function main (). It states the name of the function (main), the type of this function arguments if left empty means void meaning none. The type of the value that this main function returns depends on the data type declaration for this function.

Ex: int main ()     – This returns value of int data type       

main ()          – This returns value of void data type

Program execution starts in the main function.

  • After declaration of main function, flower brackets start. Start of the flower brackets indicate starting of the function and end of the flower brackets indicate the end of the function. The curly braces are used in pairs to indicate where a block of code begins and ends.

Inside these flower brackets you can display something or you can do some calculation etc. depending on your requirement. These statements that we write inside these flower brackets must be terminated by semicolon at each end of the statement.

If you declare your main function as any data type but not as void, use return statement to return your value to the function.

Ex: return 0;

     return a;

Generally, if there is nothing to return to the function, you can use return 0 or return 1 to indicate that the program is exited successfully. After the return 0 or return 1 statement, the execution process will terminate.

In C language single line comments are done using // In C language multi line comments are done using /* at the beginning of start line comment and */ at the ending of the end line comment.

Example Program:

//This program will display "Hello VizagEdu” on your display

#include <stdio.h>
int main()
{
    printf("Hello VizagEdu");
    //printf() function will be discussed in next topic 
    return 0;
}

Execute the above program by clicking here.

C++ LANGUAGE:

C++ program structure is almost similar to C. The program structure of C++ for the above same example program written in C is as below.

Example Program:

//This program will display "Hello VizagEdu” on your display

#include <iostream>
int main()
{
    std::cout << "Hello VizagEdu";
    return 0;
}

Execute the above program by clicking here.

This program can be written in another way “using namespace std”

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello VizagEdu"; 
    return 0;
}

Execute the above program by clicking here.

You can use “std::endl or endl” after end of all statements in program. This is optional.

#include <iostream>
int main()
{
    std::cout << "Hello VizagEdu" << std::endl;
    return 0;
}

Execute the above program by clicking here.

#include <iostream>

It is a pre-processor directive that includes the content of the standard C++ header file iostream. iostream is a standard library header file that contains definitions of the standard input and output streams. These definitions are included in the std namespace, explained below. The standard input/output (I/O) streams provide ways for programs to get input from and output to the terminal.

main()

This function is similar to C language.

std::cout

It is the standard output stream object, defined in iostream, and it prints to the standard output(stdout).

<<

This symbol in this context is called stream insertion operator, because it inserts an object into the stream object.

std::endl

It is a special I/O stream manipulator object, also defined in file iostream. Inserting a manipulator into a stream changes the state of the stream.

Comments in C++ are similar to C comments.

PERL:

In Perl, there is no specific program structure because these are interpreted languages.

We can directly write the code of our requirement and can execute on the terminal.

Semicolon should be declared at each end of the statement.

Single line comments are done using # before each line

Multi line comments are done using declaring “=begin” before the start line and “=end” after the end of lines and declare “=cut” next to the “=end” declaration.

Example Program:

#This program prints “Hello VizagEdu” on the display

print "Hello VizagEdu";

Execute the above program by clicking here.

You can also print this using another method by assigning the string to a variable and printing the variable. “$” symbol that we are going to use to declare variables indicates scalar. This will be explained in later concepts.

$a = "Hello VizagEdu"; 
print "$a";

Execute the above program by clicking here.

#multi line comment program example
$a = "VizagEdu"; 
=begin
This is first line of comment.
This is second line of comment.
=end
=cut
print "$a";

Execute the above program by clicking here.

PYTHON:

As Python is also an interpreted language, we don’t have any particular program structure here. It is similar to Perl.

There is no need to declare semicolon at the end of each statement in Python.

Single line comments are done using # before each line.

Multi line comments are done using either by declaring “”” or ”’ at the start and end of lines to be commented.

Example Program:

#This program prints “Hello VizagEdu” on the display

print ("Hello VizagEdu")

Execute the above program by click here.

You can also print this using another method by assigning the string to a variable and printing the variable.

a = "Hello VizagEdu"
print (a)

Execute the above program by clicking here.

#multi line comment program example using """
a = "VizagEdu" 
"""This is first line of comment.
This is second line of comment. """
print (a)

Execute the above program by clicking here.

#multi line comment program example using 
a = "VizagEdu" 
'''This is first line of comment.
This is second line of comment. '''
print (a)

Execute the above program by clicking here.

I hope you liked the content. Please subscribe on the website to receive the mails whenever new content is released on the website.

Thank you,

VizagEdu Team

Published by vizagedu

We are group of professionals and young graduates working together to fill the technical gap in the current education system

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: