Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

BASICS of C

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

1.

C programming basics to write a C Program:

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.

C Basic commands Explanation


This is a preprocessor command that includes standard input output header file(stdio.h) from the
#include <stdio.h>
C library before compiling a C program
int main() This is the main function from where execution of any C program begins.
{ This indicates the beginning of the main function.
whatever is given inside the command /* */ in any C program, wont be considered for
/*_some_comments_*/
compilation and execution.
printf(Hello_World!
printf command prints the output onto the screen.
);
getch(); This command waits for any character input from keyboard.
return 0; This command terminates C program (main function) and returns 0.
} This indicates the end of the main function.

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!

3. Basic structure of a C program:

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

Example C program to compare all the sections:


You can compare all the sections of a C program with the below C program.

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:

This is a C basic program


Sum of two numbers : 2

Description for each section of the C program:

Let us see about each section of a C basic program in detail below.


Please note that a C program maynt have all below mentioned sections except main function and link sections.
Also, a C program structure maynt be in below mentioned order.

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.

You might also like