C Language Data Strucutres
Structure of a c program
by Shivu on Jan.26, 2010, under C Language Data Strucutres
Structure of a c program
C is a structured programming language . It follows everything in a particular order form top to bottom. Basically a C program contains a header file section , global declaration, main part and user defined function.
Header file section
global declarations
main ()
{
declaration part;
statements ;
}
user defined functions /* this is a paragraph comment */
// this is a line comment
Let me show you with an example , a small program to display “Hello World!” on screen.
#include
void main()
{
// To print a “Hello world on screen”
printf(“Hello world!”);
}
Header file section :
This is the section where we include the necessary header files, functions which we need to use in our program. We use # include to include a header file. # is called compiler directive symbol and include is a preprocessor directive
Global declaration:
C follows top to bottom technique, so when we want to use any variables in our program we need to declare it before we write any statements .C can have many functions. It may be user defined one or standard library function. If you want to access a variable throughout a program irrespective of the function we need to declare it above the main function.
Main function:
Execution of every c program starts from the main function . Main is a user defined function.
Declaration part:
Here we need to declare a variable which we are going to use in the main block. This type of declaring a variable in a function is called local declaration.
Statements:
Statements are nothing but valid c expressions, functions , operations. Each statement is terminated by ;
User defined functions:
Not all functions that are required for programming are written in standard library functions / header files . C gives the flexibility to user to write his own functions for his purposes . Such functions are called user defined functions.
Usually there is no rule that functions should be written below the main block . They can be written anywhere in the program , or another file which we need to include . But for our convenience and good appearance we write them below the main block.
Comments :
We can write comments in a program which when executed are ignored by compiler. These are only for our convenience. We can write a program without comments .There are two types of comments . The first one is // this is called a line comment. When you place it in the beginning of a line it tells the compiler that the entire line is a comment . You can use it after the end of ; . The second one is a multi line comment /* comment */ “/*” specifies the starting of a comment and “*/” specifies the ending of the comment.
Tips:
Let me show you the two examples.
Example A:
#include<stdio.h>
void main()
{
// To print a “Hello world on screen”
printf(“Hello world!”);
}
Example B:
#include<stdio.h>
void main()
{
// To print a “Hello world on screen”
printf(“Hello world!”);
}
Example A looks much neater than Example B .You will find it more helpful when you are writing loops and programs with more than one function ; code will be clearer and more readable. Even though this is not a must, it is a good practice to follow indentations. Blank lines in C are removed by compiler during compilation, so use them frequently to make code readable. When you go to higher languages like Python , you need to follow indentations very strictly.
Commenting is one more good programming practice. When you look at your code after a few months you will not be able to recognize the logic you have written .When developing a project in C you will be sharing your work with your team-mates and it will be helpful for your team-mates to understand your logic and code.
- At the beginning of the program you can comment out the name of your company / college , your name and purpose of the program.
/* www.btechadda.com This program is for printing "Hello world ! " on screen Author: Sivaram Nyapathi Email :sivaram@btechadda.com - Before including header file ,mention function which you are going to use so that if you want to use a new function you can see the available functions in header file and use the function which serves your purpose . If you are not able to find one you can include another header file . This approach saves you some memory as well as coding time.
/* Including header file stdio.h to use printf();*/ - Before writing the function body we can comment out , why we are using the function what its return type arguments, etc so that it will be helpful while debugging. Also code will be more understandable by others.
- Use line comments as frequently as you can in program to tell the logic you are using .
- If you are writing loops , nested loops you can use comments to tell where the loop begins and ends . This will help you in troubleshooting.
Though it takes time always try to comment, while programming it will be very easy for you to prepare documentation .
Compiler and interpreter
by Shivu on Jan.20, 2010, under C Language Data Strucutres
Compiler:
Compiler is a system software , which checks the whole programs for errors (mainly syntax errors ) , and then converts them into machine level language. This processes is like translating a book into another language. It takes time to debug while converting high level language / middle level language to machine level language. Once the program is converted , it can be executed very quickly.
Interpreter :
Interpreter is also a software program which converts the program from high level/middle level language to machine level language . The interpreter will check the program line by line for errors , if there is no error then the line gets executed. Whenever foreign delegates come to our country , human interpreters will help them to converse with each other , they translate what is told by one person to another on the spot. The execution of interpreter is slow , debugging is very easy ; since the errors are shown on every line basis debugging will be easy.
Introduction to C
by Shivu on Jan.20, 2010, under C Language Data Strucutres
Introduction to C
In 1970′s computers were very few and mostly found only in research Labs of universities. There were a few programming languages like FORTRAN , BASIC and assembly language . FORTRAN was efficient for scientific application , but was a bad system software . Basic was a very easy language to master but it lacked many features .Assembly language was very efficient and very fast , but the problem was learning it. In the early 70’s,computer revolution was slowly taking shape.Many universities , education institutions were working hard to create better computer languages and computers were being used in many places apart from university research labs . There was a huge demand for software / computers.
Dennis Ritchie developed and implemented the C language . The C language was first implemented on PDP-11 . C language development started with BCPL (B language) , Dennis Ritchie slowly started working on the drawbacks of B language and made history. This marked the beginning of the modern age of computer languages. C language was very easy to learn , and had many powerful features. C language was a middle level language , it had features of both high level language and low level language. C compiler would convert the programs written by user into machine level language .The language which took years to master had become very easy because of c . Execution was vey fast and efficient as we execute the machine level code.
This language was first used in writing unix , and it was part of unix standard distribution. Programming in c language was relatively easy when compared to other languages. C quickly attracted many followers . In short we can say C is made by programmers for programmers. This is the only language which has changed very very little in past 2 decades .C was formally standardized in December 1989, when the American National Standards Institute (ANSI) standard for C was adopted.