First C Program
Before starting with the C programming language you need to learn how to write, compile and run the first c program.
we will learn to create the first C program and then will understand its structure. First of all, let's have a look at how to write a simple and basic Hello World program in C language. Let's get started.
To write the first c program, open the C console and write the following code:
#include <stdio.h>
int main(){
printf("Hello C Language");
return 0;
}
To run the above code on your local machine, you will have to install a C language compiler on your Computer/Laptop.
- #include <stdio.h> includes the standard input output library functions.
- The printf() function is defined in stdio.h .
- int main() The main() function is the entry point of every program in c language.
- printf() The printf() function is used to print data on the console.
- return 0 The return 0 statement, returns execution status to the OS. The 0 value is used for successful execution and 1 for unsuccessful execution.
Understanding Structure of the C Program
Given below are some of the different parts of a C Program:
- Pre-processor
- Header file
- main() function
- Variables in C
- Statements & expressions in C
these are essential parts of a C language program.
Pre-processor:-
The #include is the first statement of any C program. It is known as a pre-processor. The task of a pre-processor is to initialize the environment of the program, i.e. to link the program with the header files required.
Header file:-
A Header file is a set or collection of built-in(readymade) functions, which we can directly use in our program.
Header files contain definitions of the functions which can be used in any C program by using pre-processor #include statement along with the name of the header file.
The main() Function:-
The main() function is a function that must be there in every C program.
Everything inside this function in a C program will be executed, hence the actual logic or the code is always written inside the main() function.
The printf() Function:-
The printf() is a function that is used to print(show) anything on the console as output. This function is defined in the stdio.h header file, which we have included in our C program.
Return Statement:-
A return statement is used to return a response to the caller function. It is generally the last statement of any C language function.
Semicolon:-
It is important to note that every statement in C should end with a semicolon(;). If you miss adding any semicolon, the compiler will give an error.
How to compile and run the c program
There are 2 ways to compile and run the c program, by menu and by shortcut.
By menu
Now click on the compile menu then compile sub menu to compile the c program.
Then click on the run menu then run sub menu to run the c program.
By shortcut
Or, press ctrl+f9 keys compile and run the program directly.
Escape sequence in C Programming
Escape sequence |
Description |
\n |
Newline. This will
place a cursor on the beginning of the second line. |
\t |
Tab. This will
insert horizontal tab. |
\\ |
Backslash. Insert
backslash in a string. |
\a |
Alert. This will
signify alert sign or sound without changing the position of cursor called escape
character. |