First C Program
Let’s write and understand our first simple C program step-by-step. We'll also learn why we use each line.
Example: Simple C Program
#include<stdio.h> // Standard Input Output header
#include<conio.h> // Console Input Output header (used for clrscr and getch)
void main() {
clrscr(); // Clears the output screen (works in Turbo C)
printf("Hello, World!"); // Prints the message
getch(); // Waits for a key press before closing the screen
}
How to Save a C Program:
C programs should be saved with a .c extension.
Example: hello.c, program1.c, abc.c
Output:
Hello, From ShikshaSanchar!
Explanation:
#include<stdio.h>: This is a header file that allows the use ofprintf()andscanf()functions.#include<conio.h>: This is an optional header file (mostly used in Turbo C) that provides functions likeclrscr()andgetch().clrscr();: Clears the screen before printing the output (optional; works in Turbo C).printf();: Used to display output on the screen.getch();: Waits for a key press so that the output window doesn’t close immediately after execution.void main(): Starting point of the program. It can also be written asint main()(standard and recommended).return 0;: Used only when we writeint main(). It tells the operating system that the program ended successfully.
Things to Remember:
- Every C program starts with
main()function. - Statements inside the function are ended with a semicolon (
;). - Curly braces
{ }are used to define the block of code. - Header files must be included to use built-in functions like
printf().
Note:
- In modern compilers (like GCC),
int main()is preferred and required. conio.h,clrscr(), andgetch()may not work in modern compilers like GCC or Code::Blocks. They're mostly used in Turbo C.
Summary:
#include<stdio.h>:Required for input-output functions.#include<conio.h>:Used forclrscr()andgetch().int main(): Preferred in standard compilers; returns 0 on success.clrscr(): Clears the screen (optional).getch(): Holds the output screen until a key is pressed.- File Extension: Save with
.c