Simple C Language Program Structure

 

Simple C Language Program



 

List of simple C Language programs with its source code is given in this article. Dear students please practice following programs by using Turbo C Editor. Compile programs, execute it and display output on screen.

 

1.

// Simple C Program to display “Hello Friends, What is going on”

//standard input output headerfile

#include<stdio.h>

//console input output headerfile

#include<conio.h>

// main function of C Program

void main()

          {

     // print() helps to display the message on screen

            printf(“Hello Friends”);

             printf(“What is going on”);

  // getch() helps to get or read user character

                   getch();

          } // Program end

 

Output of above program:

Hello Friendswhat is going on

 

In the above program #include directive gives instruction to the compiler or preprocessor to include the contents of file specified in the input stream to the compiler and then continue to the source file.

Stdio.h is standard input and out library it has information related to the input and output functions of C language.

conio.h this headerfile mostly used by MS DOS compilers like Turbo C. IT is console input output headerfile. The conio.h headerfile is used to support the getch() function( used to get user character) and clrscr() function( used to clear the console or screen). These are inbuilt functions of C language.

void main() - the void main() function indicates that the main() function will not return  any value but int main() can return integer type data to function.

When programmer the programmers program is simple and error free and not going to terminates before reaching it at the last line of the code in this case programmer can use void main().

The body of C program is written In opening and closing curly bracket ({ } ). The body of C program include variable declaration methods, input and output statements, processing statements, control structure of C program. The main() function followed by opening and closing curly bracket the actual c program execution starts from the main() function.

First printf() function is used to display the string “ Hello Friends” and second printf() function is used to display string  “What is going on” on the screen. Display string is always enclosed in double quotation.

Printf() function is terminated with semi colon(;). Every instruction in C program is terminated by semicolon.

After completion of writing C program source code, programmer can compile the program using Turbo C Compiler. The compiler first detect the errors. If program is error free compiler will shows zero errors. If any error is there in program it will shows list of errors. Then programmer has to find the errors and remove the errors. Now programer can execute i.e. run the error free program. As soon as user click on run compiler will create the object file from the source file. Source file is user written file which is only understood by programmer it should be converted into machine understandable form i.e. binary form. This translation or conversion from source code to object code is done by compiler. Object code get execute and based on the instructions and processing statements the output is generated and displays on screen.

// (Double forward slash) is used in above program to write the comment lines. Comment lines are non executable text. It is ignored by compiler. The single line comments are given by double forward slash(//) and multi line comment is written by  /*               and       */  

 

In above program two display statements i.e. printf() functions are used. When user execute program programmer will get output as  - Hello Friendswhat is going on.

Though there are two printf() functions but output will display on same line. Because programmer has not written the instruction to display strings on new lines. Compiler only executes the instructions written by programmer, it cannot cannot take any other action other than the program instructions.

To display the different display statements on every new line programmer can use

\n(Backword slash and n). It meaning is compiler transfer its pointer to new line and display next statement at new line.

 

The following shows how to display different strings on new lines.

2.

// Simple C Program to display “Hello Friends, What is going on”

//standard input output headerfile

#include<stdio.h>

//console input output headerfile

#include<conio.h>

// main function of C Program

void main()

          {

    //clrscr() used to clear the screen

               clrscr();

 // print() helps to display the message on screen

          printf(“Hello Friends\n”);

           printf(“What is going on\n”);

       // getch() helps to get or read user character

              getch();

          } // Program end

 

Output:

Hello Friends

What is going on

 

In the above program one another function is used i.e. clrscr() function which is used to clear the screen. To support the clrscr() function programmer should include conio.h headerfile. When user executes program code multiple times whatever the output of program that remain there on output screen. So to clear output screen we can use clrscr() in our program.

टिप्पणी पोस्ट करा

1 टिप्पण्या

कृपया तुमच्या प्रियजनांना लेख शेअर करा आणि तुमचा अभिप्राय जरूर नोंदवा. 🙏 🙏