BASICS of C
BASICS of C
BASICS of C
Below are few commands and syntax used in C programming to write a simple C program. Lets see all the sections of a
simple C program line by line.
2. A simple C Program:
Below C program is a very simple and basic program in C programming language. This C program displays Hello
World! in the output window. And, all syntax and commands in C programming are case sensitive. Also, each statement
should be ended with semicolon (;) which is a statement terminator.
1 #include <stdio.h>
2 int main()
3 {
4 /* Our first simple C basic program */
5 printf("Hello World! ");
6 getch();
7 return 0;
8 }
Output:
Hello World!
Structure of C program is defined by set of rules called protocol, to be followed by programmer while writing C program.
All C programs are having sections/parts which are mentioned below.
1. Documentation section
2. Link Section
3. Definition Section
4. Global declaration section
5. Function prototype declaration section
6. Main function
7. User defined function definition section
1 /*
2 Documentation section
3 C programming basics & structure of C programs
4 Author: fresh2refresh.com
5 Date : 01/01/2012
6 */
7
8 #include <stdio.h> /* Link section */
9 int total = 0; /* Global declaration, definition section */
10 int sum (int, int); /* Function declaration section */
11 int main () /* Main function */
12 {
13 printf ("This is a C basic program \n");
14 total = sum (1, 1);
15 printf ("Sum of two numbers : %d \n", total);
16 return 0;
17 }
18
19 int sum (int a, int b) /* User defined function */
20 {
21 return a + b; /* definition section */
22 }
Output:
Sections Description
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 */, wont be
Documentation
considered by C compiler for compilation process.These will be ignored by C compiler during
section
compilation.
Example : /* comment line1 comment line2 comment 3 */
Link Section Header files that are required to execute a C program are included in this section
Definition Section In this section, variables are defined and values are set to these variables.
Global declaration Global variables are defined in this section. When a variable is to be used throughout the
section program, can be defined in this section.
Function prototype Function prototype gives many information about a function like return type, parameter names
declaration section used inside the function.
Every C program is started from main function and this function contains two major sections
Main function
called declaration section and executable section.
User defined function User can define their own functions in this section which perform particular task as per the user
section requirement.