Hello World My First C Program
My First C program
A C program must have a function called main function to execute. A function is a small piece of code used to perform a specific task. We will discuss functions in detail later but you must know that if we want to do anything then we will need to write a function for this.
C has a rich library having a lot of pre-defined functions but we will be required to write our own functions as well. All the predefined functions are saved in a related file called header file. We are required to include the header file before using the predefined function. There is no need to worry about the details of header files and functions.
These topics will be discussed in detail later. At the moment we just need to know that without functions we cannot write a single piece of code and all the predefined functions are stored in header files. A related header file must be included at the start of the program if we need to use a pre-defined function.
Structure of a C Program
A C program looks like below.
In the above C program stdio.h and conio.h are header files having built in functions. If we need to use a pre-defined function then we need to include the related header files as well. In the above program we are using printf()and getch()pre-defined functions. printf()is defined in stdio.h, so we need to include stdio.h header file before using printf() function. Similarly getch() is defined in conio.h, so we need to include conio.h as well.
As discussed earlier, every program must have a main function to execute. If a program does not have main function then it can be compiled but will not be able to execute.
If a program does not execute then we cannot get the output from the program. In the above program main function is defined at line 3. Don’t bother about the keyword void with the main function. It has a specific meaning to the compiler but will be discussed later.
The main function must have a starting and closing brace. The body of the function is written between these braces.
C is a case sensitive language
It is also important to note that C is a case sensitive language which means that care must be taken while writing the code. If we write a statement in different case then compiler will not be able to recognize the statement and as a result the code is not compiled.
Almost all the statements in C language are written is small case but this may not be always the case. Some statements are written in capital letters as well. Special care must be taken while writing the code.
Statement Terminator (Semicolon)
It is very important to know that all the statements must be terminated with a semicolon as we can see at the end of lines 5 and 6 above. Semicolon tells the compiler that the statement ends here.
Most of the statements in C language are single-lined but some statement may be spread over multiple lines. In this case semicolon plays an important role telling the compiler that the statement ends here.
How to Write, Compile and Execute a C Program
A C program must be compiled before execution. We can use any IDE (Integrated Development Environment) for this purpose. One of the most commonly used IDE is Turbo C IDE. We can also use Eclipse, Bloodshed C, Visual Studio, NetBeans or online compiler to write, compile and execute a C program.