Tuesday 16 July 2013

what is c programming ?

 The C programming language is also a high-level programming language developed by Dennis Ritchie and Brian Kernighan at Bell Labs in 1972 from an almost unknown language named B. The first major program written in C was the Unix operating system, and for many years, C was considered to be inextricably linked with Unix.


Now we are going to learn how to make a program in  C.

C – Basic Program:

We are going to learn a simple “Hello World” program in this section. Functions, syntax and the basics of a C program are explained in below table.

Output:

Hello World!
Let us see the parts of the program line by line.
S.noCommandExplanation
1#include<stdio.h>This is a preprocessor command that includes standard input output header file(stdio.h) from the C library before compiling a C program
2int main()This is the main function from where execution of any C program begins
3/* some comments */whatever is given inside /*   */ this command won’t be considered by C program for compilation and execution.
4printf(“Hello World! “);printf command prints the output onto the screen
5return 0;
This command terminates main function then returns 0

Compile and execute C program:

  • Compilation is the process of converting a C program which is user readable code into machine readable code which is 0′s and 1′s.
  • This compilation process is done by a compiler which is an inbuilt program in C.
  • As a result of compilation, we get another file called executable file. This is also called as binary file.
  • This binary file is executed to get the output of the program based on the logic written into it.

Steps to compile & execute a C program:

Step1
Type the above C basic program in a text editor and save as “sample.c”.
Step2To compile this program, open the command prompt and goto the directory where you have saved this program and type “cc sample.c” or “gcc sample.c”.
Step3If there is no error in above program, executable file will be generated in the name of “a.out”.
Step4To run this executable file, type “./a.out” in command prompt.
Step5You will see the output as shown in the above basic program.


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

S.NoSectionsDescription
1Documentation sectionWe can give comments about the program, creation or modified date, author name etc in this section. Comments can be given in two ways inside a C program.       

1. Single line comment – Syntax :

// Your Comments Here

2. Multi line comment – Syntax :

/*
comment line1
comment line2
comment line3
*/
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.
2Link SectionHeader files that are required to execute a C program are included in this section
3Definition SectionIn this section, variables are defined and values are set to these variables.
4Global declaration sectionGlobal variables are defined in this section. When a variable is to be used throughout the program, can be defined in this section.
5Function prototype declaration sectionFunction prototype gives many information about a function like return type, parameter names used inside the function.
6Main functionEvery C program is started from main function and this function contains two major sections called declaration section and executable section.
7User defined function sectionUser can define their own functions in this section which perform particular task as per the user requirement.

No comments:

Post a Comment