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;

}

 

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. #include is preprocessor directive, and stdio.h is Header file. Header files in C have a .h extension.

The #include directive can be used to include pre-existing header files or user-defined header files. the #include directive tells the preprocessor to include the contents of a specified file at the point where the directive appears. This allows the programmer to import header files into their program and access the functions and methods within them.

(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();

clrscr() is a library function (system defined 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.

 

44 thoughts on “Sample C Program | How to write a simple C Program?”

  1. Great – I should definitely pronounce, impressed with your web site. I had no trouble navigating through all the tabs and related info ended up being truly easy to do to access. I recently found what I hoped for before you know it at all. Quite unusual. Is likely to appreciate it for those who add forums or something, web site theme . a tones way for your customer to communicate. Nice task..

    Reply
  2. What¦s Happening i’m new to this, I stumbled upon this I have discovered It positively helpful and it has helped me out loads. I hope to give a contribution & assist different customers like its helped me. Good job.

    Reply
  3. Thank you for the sensible critique. Me and my neighbor were just preparing to do some research about this. We got a grab a book from our area library but I think I learned more clear from this post. I’m very glad to see such fantastic info being shared freely out there.

    Reply
  4. Attractive portion of content. I simply stumbled upon your website and in accession capital to say that I get in fact enjoyed account your blog posts. Anyway I’ll be subscribing to your augment or even I achievement you get admission to persistently rapidly.

    Reply
  5. After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.

    Reply
  6. magnificent put up, very informative. I wonder why the opposite experts of this sector do not understand this. You should continue your writing. I’m sure, you have a huge readers’ base already!

    Reply
  7. I have not checked in here for some time since I thought it was getting boring, but the last few posts are great quality so I guess I?¦ll add you back to my daily bloglist. You deserve it my friend 🙂

    Reply
  8. Hey very nice website!! Man .. Excellent .. Amazing .. I’ll bookmark your site and take the feeds also…I am happy to find so many useful info here in the post, we need work out more strategies in this regard, thanks for sharing. . . . . .

    Reply
  9. Hello very cool web site!! Guy .. Beautiful .. Wonderful .. I will bookmark your site and take the feeds additionallyKI am satisfied to search out numerous helpful info right here within the publish, we need work out extra strategies on this regard, thank you for sharing. . . . . .

    Reply
  10. I¦ve read a few good stuff here. Definitely worth bookmarking for revisiting. I surprise how so much attempt you place to create this type of great informative site.

    Reply

Leave a Comment