Your First C Program

In this chapter you will learn how to write "Hello, World!" Program.

Before writing "Hello, World!", Let's take a look at what is a common program in the C programming language.

In C programming there are the following components / parts:

Pre-processor command
 Function
Statement and Expression
 Variable
 Comment

Why "Hello, World!" Program?
"Hello, World!" Is a very simple program that will show "Hello, World!" On the screen. Because it is a very common program, it is used to describe the syntax of the programming language.


It is used to introduce a programming language easily to beginners. So let's start

#include <stdio.h>
int main ()
{
    printf ("Hello, World! \ n");
    return 0;
}

How does the "Hello, World!" Program work?

Attach the stdio.h header file to your program.

Because C programming is small, it can not do much by itself. Some files will be required to run your program. stdio.h is the header file and the compiler knows the location of this file.

To use this header file in your program, you need to connect it with the # included preprocessor.


Why do you have to add the stdio.h file to this program?


In this program we have used the printf () function. Which will display the inner text of the quotation mark on the screen or in the console. In order to use the printf () function, you must attach your program to the stdio.h header file.



main () function

The code executes from the main () function in the C program. In this case, the main () function does not come in the beginning, but it does not go any way.

The inner code of curly braces {} is the body of the main function. The main () function in C program is mandatory.


int main () {
    // function body
}
The above program does nothing. But it's legitimate C program.


printf () function

The printf () function is the library function that outputs to the screen. In our program, on the screen, Hello, World! Give output

Remember, to do this, you need to add the stdio.h header file to your program.

Notice that the semicolon is given at the end of the print function. Semicolon is used to complete the statement.



return statement

return statement returns 0 and completes the function of the main () function. This statement is not mandatory. But it is very good to use it in programming.


The necessary rules should be kept in mind
All C programs start with the main () function and it is mandatory.
You can use the required header files that may be needed in your program. For example, you can use the sqrt () function to evaluate the square root of a number and use the pow () function for determining the power. You need to add the math.h header file to your program.
C is case-sensitive. That is, lowercase letters and capital letters are used in different ways.
The C program closes in the main () function whenever the C program reaches a return statement. However, the return statement in the main () function is not mandatory.
The statement ends in the C program via semicolon (;).