Sample C Program | How to write a simple C Program?

This sample program will print Welcome to C Programming


// Sample C Program

#include<stdio.h>

#include<conio.h>

int main()

{

clrscr();

printf(“ Welcome to C Programming”);

getch();

return 0;

}




Output:




Brief explanation of each line of the program is given below:                                

(1)   // Sample C Program

This is a comment line. Comments are used to document the program. It is like a remark from the programmer. They can appear in any part of the program. We write comments for better understanding of the program. The comments are ignored by the compiler. Comments are useful for software maintenance.


(2)   #include<stdio.h>

This command includes standard input/output header file, stdio.h from the C library in the program before compiling. The stdio.h file contains functions such as scanf() and printf() to take input and display output respectively.


(3)   #include<conio.h>

This command includes console input/output header file, conio.h from the C library in the program. The conio.h file contains functions such as clrscr() and getch() to clear the output  screen and  pause the output screen.


(4)   int main()

Here main() is the function name and int is the return type of this function. It is a compulsory part of every C program. It is a special function used to inform the compiler that the program starts from there. Execution of every C program starts from main() function. Logically every program must have exactly one main() function.


(5)   {  and }

The opening curly bracket ‘{’ indicates the beginning of the main function and the closing curly bracket ‘}’ indicates the end of the main function.


(6)    clrscr();

It is a library function used to clear the console screen. It is defined in conio.h header file. Using of clrscr() function in C is always optional . If the function is to be used, then it should be placed after variable or function declaration only.

 

(7)  printf(“Welcome to C programming”) ;

This command tells the compiler to display the message “Welcome to C programming”. printf()  is a library function used in C to display the output onto the screen. The text to be printed is enclosed in double quotes.


(8) Use of semicolon “;”

The semicolon serves as the statement terminator. Semicolon character at the end of the statement is used to indicate that the statement is ending there. Each executable statement should have a ‘;’ to inform the compiler that the statement has ended. Thus, in ‘C’ the semicolon terminates each statement.


(9)  getch();

getch() function pauses the output console screen until a key is pressed. It is defined in conio.h header file. This function is used to hold the output screen for some time until the user presses a key from the keyboard.


(10) return 0;

The return statement is used to return a value from a function and indicates the finishing of a function. The value ‘0’ means successful execution of this function. This statement is basically used in functions to return the results of the operations performed by a function.







Post a Comment

0 Comments