Basic structure of C program
Basic structure of C program:
- Documentation section
- Link Section
- Definition Section
- Global declaration section
- Function prototype declaration section
- Main function
- User defined function definition section
C Hello world program:
A C program basically consists of the following parts:
- Preprocessor Commands
- Functions Variables
- Statements & Expressions
- Comments
Let us look at a simple code that would print the words "Hello World":
#include <stdio.h>
int main()
{
/* first program in C */
printf("Hello, World! \n");
return 0;
}
The first line of the program #include <stdio.h> is a preprocessor command, which tells a C compiler to include stdio.h file before going to actual compilation.
The next line int main() is the main function where program execution begins.
The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program.
The next line printf(...) is another function available in C which causes the message "Hello, World!" to be displayed on the screen
The next line return 0; terminates main() function and returns the value 0.
Compile & Execute Program C:
in terbo c ++ editor press ctr +F9 to compile and to run press alt +F9
In linux :
- first save file with file name .c
- Type gcc hello.c and press enter to compile your code.
- If there are no errors in your code, the command prompt will take you to the next line and would generate a.out executable file.
- Now, type a.out to execute your program.
- You will be able to see output.
$ gcc hello.c
$ ./a.out
Hello, World!
note:Make sure that gcc compiler is in your path and that you are running it in the directory containing source file hello.c.
#include<stdio.h>
The # is a directive that says "handle this with the precompiler". The inlcude statement means "make everything in the provided file available to this program". stdio.h is a header file that describes the interface to the stdio library, which contains basic IO utilities needed by almost every program.
The angle brackets tell the compiler to search for stdio.h in the path specified by the INCLUDE environment variable first. This allows newer libraries to supercede older ones without having to change make files
"stdio" stands for "standard input output". As it is a header file so .h extension is there.It contains some stanrd functions related to input and ouput such as "printf", "scanf".You will find other header files as "math.h" which includes all mathematical library functions.
The file is called a header file and you will find several different header files on the source disks that came with your C compiler. Each of the header files has a specific purpose and any or all of them can be included in any program.
Your C compiler uses the double quote marks to indicate that the search for the "include" file will begin in the current directory, and if it not found there, the search will continue in the "include" directory as set up in the environment. It also uses the "less than" and "greater than" signs to indicate that the file search should begin in the directory specified in the environment.
The "main()" function comes after the preprocessor directives. It indicates the beginning of the actual C program. It is the point at which execution of program is started.
clrscr(); :- This is used for clearing the output screen i.e console
suppose you run a program, alter it and run it again you may find that the previous output is still stuck there itself, at this time clrscr(); would clean the previous screen.
return 0:
return from main() is equivalent to exit
the program terminates immediately execution with exit status set as the value passed to return or exit.
return ()in an inner function (not main) will terminate immediately the execution of the specific function returning the given result to the calling function.
getch(); :- getch is used to hold the screen in simple language, if u don't write this the screen
Description for each section of a C program:
Description for each section of a C program:
- If you want to create, compile and execute C programs by your own, you have to install C compiler in your machine. Then, you can start to execute your own C programs in your machine.
- You can refer below link for how to install C compiler and compile and execute C programs in your machine.
- Once C compiler is installed in your machine, you can create, compile and execute C programs as shown in below link.
- Let us see about each section of a C basic program in detail below.
- Please note that a C program mayn’t have all below mentioned sections except main function and link sections.
- Also, a C program structure mayn’t be in below mentioned order.
S.No | Sections | Description |
1 | Documentation section | We can give comments about the program, creation or modified date, author name etc in this section. The characters or words or anything which are given between “/*” and “*/”, won’t be considered by C compiler for compilation process.These will be ignored by C compiler during compilation. Example : /* comment line1 comment line2 comment 3 */ |
2 | Link Section | Header files that are required to execute a C program are included in this section |
3 | Definition Section | In this section, variables are defined and values are set to these variables. |
4 | Global declaration section | Global variables are defined in this section. When a variable is to be used throughout the program, can be defined in this section. |
5 | Function prototype declaration section | Function prototype gives many information about a function like return type, parameter names used inside the function. |
6 | Main function | Every C program is started from main function and this function contains two major sections called declaration section and executable section. |
7 | User defined function section | User can define their own functions in this section which perform particular task as per the user requirement. |
Comments
Post a Comment