-->

Friday, January 8, 2016

Program x to power y in C Language

Introduction 

The meaning of x to the power of y is , a variable x is multiply for y time. Lets take a simple example a variable x is hold 2 and y hold 5. Now the answer is 2 multiply for 5 times such as..
z=2*2*2*2*2;
z=32

Method-1: 

Design a Algorithm in c

Step-1 : Take two value in variable x and y by user input.
Step-2 :  Use pow function in  "C" , which is available in math.h header file.
Step-3 :  Take another variable Z for holding answer of the x to the power of y.
Step-4 : Print z 
Step-5 : End of the program.

Program :

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int x,y,z;
printf("enter X value");
scanf("%d",&x);
printf("Enter Y value");
scanf("%d",&y);
z=pow(x,y);
printf("output of the x power y is%d",z);
getch();
}

Output
Program x to power y in C


Method-2 : Using recursion

Algorithm of the program

Step-1: Step-1 : Take two value in variable x and y by user input.
Step-2: Create a function  and pass values of x and y  to the function.
Step-3 : Call same function for y times 
Step-4 : Return function 

Program

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int x,y,z;
clrscr();
printf("enter X value");
scanf("%d",&x);
printf("Enter Y value");
scanf("%d",&y);
z=fun(x,y);
printf("output of the x power y is%d",z);
getch();
}
int fun(int a,int b)
{
if(b>=1)
{
return a*fun(a,--b);
}
else
return 1;
}




Output
Program x to power y in C

Method -3:Using third variable

Algorithm:
Step-1: Take two value in variable x and y by user input.
Step-2: Run for loop for y times.
Step-3 : Take third variable z with 1 value
Step-4 : Multiply x with z for y times in for loop
Step-5 : Take output value in z variable
Step-6 :  End of the program

Program


#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int x,y,i,z=1;
clrscr();
printf("enter X value");
scanf("%d",&x);
printf("Enter Y value");
scanf("%d",&y);
for(i=0;i<y;i++)
{
z=x*z;
}
printf("output of the x power y is%d",z);
getch();
}

Output

Program x to power y in C

Structure of a C program

The structure of a C program is nothing but the way of framing the group of statements while writing a C program. We put the general structure of a C program first as shown below:

[preprocessor directives]
[global declarations]

returning_type main( )
{
[Declaration Section (local)]

[Executable Section]
}

[User defined functions]

Note : The bold faced characters such as main( ) in one line along with the left parenthesis '(' and the right parenthesis ')' should be typed as they are. The Declaration section and Executable section enclosed within '{' and '}' is called the body of the main function. The data or value returned by the function main( ) is indicated by' returning_type'. Normally main( ) doesn't return any value. So, the returning type void is used. The function main( ) can be preceded by global declarations and preprocessor directives.





Preprocessor directives : The preprocessor statements starts with '#' symbol. These statements instruct the compiler to include the specified file in the beginning of the program. One important point about preprocessor directives is that these statements are never terminated by ';' for example,


#include<stdio.h>  /* Only one file is permitted for one #include */
#include<math.h>


are the files that the compiler includes into the user program. These two files "stdio.h" and "math.h" are called header files and hence the extension '.h' . Using the preprocessor directive the user can define the constant. For example,

#define SIZE 100 /*Only one constant can be defined using one #define */
#define N 50
Here , Size and N are called symbolic constants. Their value is not changed during program execution.

Global declarations: The variables that are declared before all the functions are called global variables. All the functions can access these variables. By default the global variables are initialized with '0'.
main( ) : Each and every C program should have a function main( ) and there should be one and only one function by name 'main( )' . This function is always executable first. The function main( ) may call other functions. The statements enclosed within left and right curly braces are called body of the function main( ).

Declaration section : The local variables that are to be used in the function main( ) should be declared in the declaration section. The variables declared are identified as identifiers by the C compiler. The variables can also be initialized. For example, consider the declarations shown below:


int sum=0 ; /* The declared variables is initialized to 0 */
int a;           /* The declared variable contains garbage(unwanted) value */
float b,c,d;  /* More than one variables can be declared by single statements */

Executable section : This section contains the building blocks of the program. The blocks containing executable statements represent the directions given to the processor to perform the task. A statements may represent an expression to be evaluated, input/output operation , assignment operation, etc. The statements may be even control statements such as if statements , for statement , while statement,do-while statement,etc. Each executable statement of a block should end with a ';' .The symbol ';' is also called as statement terminated or separator.

User defined functions: This is the last optional section of the program structure. The functions written by the user to perform particular or special task are called defined functions. These are called as sub-programs. the basic structure of the user defined function resembles that of the function main( ).

Now, let us write a small program to display the message " dotprogramming":
#include <stdio.h>
main( )
{
printf("dotprogramming");
}

Principles of Programming in C language

Introduction to Programming
Remember your last visit to the super market. You might have purchased many items. What did you do after picking all the items and putting them into the carriage? Probably you met with the billing clerk to make the payment. Have you observed his way of preparing the bill? He picks the items one by one and enters something into the computer repeating the same task for all the items.
Within a count of few minutes or even may be within few seconds he gives the bill , you pay and come out carrying all the required things. So what made him to process the bill so fast? It is nothing but the “program” running in the computer’s memory. If you want to become a billing clerk, then what is needed is to just learn the method to use the program that helps in the billing the items.
Stop, don’t think in that way. You should dream something high! To design a program that helps the billing clerks to prepare the bill fast and in a most efficient way.

Program Concept
By and large computers are used either to run the designed programs or to design the program itself. As an upcoming programmer you are going to use the computer to design the programs. That should be your main dream and aim. Keeping that in mind, let us see “what is program?”
Definition:  A program, strictly, a computer program is a collection of coded instructions to direct a computer to perform a desired set of operations. So, coding the instructions to make a program is an art. This art of making or designing a program is called programming. The person who writes such programs is called programmer.

Another definition of program:

Algorithm + Data Structure = Program

Definition : A program can be defined as the combination or clubbing of algorithm and data structure together into single unit. Here the algorithm refers to the procedure containing primitive steps to solve a particular task. A primitive step implies an easily understandable one. Data structure refer to the modeling of the required data to solve the task.

A programmer can design the program as per the customer's needs. The customer's need is collected as problem . The problem is analyzed to arrived at a solution. This solution provided using certain tools is called program. Whenever a problem arises , the programmer can design a program. The designed program can also be used to solve some related problems with the little modification. Changes is the nature! The new demands, requirements , etc. rise as the time passes. The programmer should react properly to these and should come out with a new solution from the existing solutions. Such existing solutions along with the new solution frame a program library. Now, let us see " what is program library?"

Definition: A library , a store or collection of computer programs is called program library . Each and every program in the library is designed to solve a certain type of problem.

What is canned program?

Definition: A program in a library of computer programs is called canned program. Existence of libraries of programs that are easy to use and designed to solve very general problems is important. The Turbo C compiler, you are going to learn, is the best example that contains many canned programs.
Turbo C compiler container many small programs in the form of built-in-functions contained in header tiles. With the help of such canned programs you can become a very good programmer. So, you should practice using such canned programs. Such canned programs are designed and added to the library. The canned programs are established and maintained by various computer manufacturers and centers.
For example, you will use a function scanf ( ) to read the input for almost all the programs you write and learn throughout the learning of this course. Similarly, you will use printf ( ) to display the messages and values or data on the output screen. Using such library functions or canned programs you build your own programs to solve general purpose or system-oriented problems. These two functions are canned in a header file “stdio.h”.
The C language that you are learning, contains nearly 27 such header files to design a program. All the header files inclusively contain hundreds of built-in functions. The header file “stdio.h” contains 56 built-in functions to solve many problems related to input and output operations. The following list provides a rough idea of number of built-in functions available in C:
“stdio.h” - 56, “conio.h”- 29,
“stdlib.h” - 42, “string.h” - 37,
“math.h” - 30, “ctype.h” - 17, etc.

Wednesday, May 6, 2015

Demerits of bottom-up technique in c language

In my previous article i explained about:

  1. Features of top-down technique
  2. Merits of bottom-up technique
Now, today i will learn about Bottom up technique demerits:

  • The application developed using this technique cannot be tested as a whole before the development of main solution.
  • The sub-solutions are added or linked to the main solution without knowing the details of its coding.
  • The design of the sub-solutions are coded without the idea of their linking in the main solution.
  • The integration testing may cause complications because it is done in the later stage of application development.
You may also learn about : Top Down Technique

Wednesday, April 8, 2015

Unstructured technique of programming in C language

Definition : An approach of designing a program simply using series of statements is called unstructured programming.
In such programming technique every task is solved using series of simple statements only. Branching and repetitions are achieved through a 'goto; statement. Jumping to and out of a block of statements is achieved through 'goto' statement. So, a technique using 'goto' statement is called unstructured technique of programming.

Example:
Problem: Design a program to find factorial of a number
The program required is to find n!=1*2*3*4.....*(n-1)*n. If negative number is entered a proper message is to be displayed. So, the repetition and branching is achieved in unstructured programming technique using 'goto' statement. The statement 'goto' in C is used to take the control from one point to another irrespective of the location of transfer. It takes control to the specified label.

#include<stdio.h>
main( )
{
int n;
unsigned long int fact =1;
ReadAgain:     /* first label to transfer the control */
printf("Enter a number:");
scanf("%d", &n);
if(n<0)
{
printf("Negative number \n");
goto ReadAgain;    /* Control transferred */
}

Repeat:      /* second label to transfer the control  */
fact = fact*n;
n--;
if(n>=1) goto Repeat;  /* Control transferred to label Repeat */
printf(" The factorial is %lu", fact);
}

Features of unstructured technique

  • Simple statements are used to solve a task.
  • The statements used are basic and understandable.
  • This type of programming is called liner programming.
  • The approach is straight forward.
  • To branch from one point to other only a simple 'goto' statement is used.
  • Even to repeat a part of the program 'goto' statement is used.
  • Every logic of the program is developed using only 'goto' statement.
Merits of unstructured technique 
The unstructured technique of programming although having some merits is not at all entertained to design the programs. The merits to just list are:
  • The program designed is simple.
  • The logic is simple and straight forward.
  • Only one statement 'goto' is used for branching as well as looping.
Demerits of unstructured technique
The unstructured technique of programming is very rarely used. It is never entertained because of its demerits. The demerits of the unstructured technique are:
  • To follow the logic of the program is very difficult.
  • There may be abrupt transfer from one point to another.
  • This technique can be applied only to small-scale programs.
  • Difficult to keep track the logic of the program.
  • Excessive use of 'goto' statements causes confusion in program flow.
  • The quality of the program decreases as the number of 'goto' statements increases.
  • Program designed using this technique is not clear for the others and also at times to the designer of the program.
  • It is difficult for the programmer to understand the logic at the later stage.
  • Because of excessive use of 'goto' statements tracing the error is very difficult.
  • Testing of program consumes lots of time.
  • Redesigning a program is much more difficult.
  • Unstructured programs consist of statements that are not grouped for specific tasks.
  • The logic of such programs is messy with details and therefore difficult to follow.

Monday, March 9, 2015

Tips for program designing in C language

Now, let us see "What are the tips to design a good program?"
Although the program design involves several stages, the following are the few important tips to design a good program:

I Tip: Make a program readable. It is a combination of the following:

  1. Careful choice of data structure or data type.
  2. Careful choice of variable names.
  3. Generous use of remarks or comments.
  4. Program indentation.
  5. General program design


II Tip: Stepwise refinement of a solution

  1. Break a problem into a sequence of relatively self-contained or independent sub-problems.
  2. Follow orderly flow of stepwise refinement.


III Tip: Avoid 'goto' statement to jump around, forward and backward, into and out of loops or blocks of statements. The blocks are also called compound statements.

  1. The programs using 'goto' are difficult to read and debug.
  2. The programs using 'goto' are called unstructured programs. Always hate such unstructured programs.


Coupling in borland C language

Coupling measures the strength of all relationships between functional units. It is the measure of the interdependence of one module to that of another. The program should have low coupling. Low coupling minimize the cause of errors in other modules. The errors in the other modules are caused because of the change in one module.

Definition: Coupling can be defined as the degree of interdependence between two or more modules. The reduction in coupling reduces the complexity of the program. While designing a program, make each module as independent as possible eliminating unnecessary relationships.

The following figure shows the types of coupling that exists in programming:
Types of Coupling:
1. No direct coupling
2. Normal coupling
    2.1   Data coupling
    2.2   Stamp coupling
    2.3   Control coupling
3. Common coupling
4. Content coupling

No direct coupling: These are the independent modules of the program. They are not really components of a single program.

Normal coupling: Two modules, X and Y, are normally coupled if X calls Y, Y returns to X and all information passed between them is by parameters in the call.

Data coupling: Two modules are said to be data coupled if they communicate by passing parameters. This is the most common type of coupling. Try to keep the parameters as minimum as possible.

Stamp coupling: Two modules are said to be stamp coupled if they communicate through a passed data structure that contains more information than necessary for them to perform their operations. A complete piece of data is passed between the modules.

Control coupling: Two modules are said to be control coupled if they communicate with the help of at least one "control flag". The control flag controls internal logic of the module.

Common coupling: Two modules are said to be common coupled if both of them share the same global data area. This type of coupling is really undesirable. Problem or error in one module can affect the other modules. Even it is difficult to identify the affected modules.

Content coupled: Two modules are content coupled if one module changes a statement in another, one module references or alters data contained inside another module or one module branches into another module.

The cohesion exists is within the modules where as the coupling exists between modules within the program. The law of program development is:

"Minimize the COUPLING and Maximize the COHESION"

Pointer introduction in C language

Introduction

Pointer is one of the important feature available in C language. Almost all the program in the software industry are written only using pointer, But many people think that pointer concept is difficult. The correct understanding and use of pointer is very much required for successful C programming. Pointers are one of the strongest and also one of the most dangerous features(if not used properly) available in C. Due to its important and dangerous feature, the concept of pointer is dealt in detail. Now , let us understand the concept of pointers.

Pointer concept 

Definition  : The basic data types in C languages are int, float, char , double , and void. Pointer is a special data type which is derived from these basic data types. So, pointer is called derived data type. The pointer takes the values from 0 to 65535 (Memory address) if the size of the RAM is 64K. The pointers are always associated with the following three concepts:

Pointer concept

  • Pointer Constants
  • Pointer Values 
  • Pointer Variables 

Linear Search in C language

Searching

Before writing the algorithm or program for searching , let us see, “ What is searching? What are the searching techniques?”
Definition : More often we will be working with the large amount of data. It may be necessary to determine whether a particular item is present in the large amount of data or not. This process of finding a particular item in the large amount of data is called searching. The two important and simple searching techniques are linear search and binary search.

SQL Video Channel : Download all SQL Video


Linear search (Sequential search)
“What is the linear search?”
Definition: Linear search is a simple searching techniques in which we search for a given key item in the list in linear order(Sequential order) i.e. one after the other. The item to be searched is often called key item. The linear search is also called sequential search.

For example , if key=10 and the list is 20,10,40,25 after searching we say that key is present . If key =100 after searching we say that key is not present.
“How to search for an item in a list of elements?” The procedure is as follows.

Linear/Sequencial Search in C


Procedure: Assume 10 is the item to be searched in the list of items 50,40,30,60,10. Observe from above figure that, 10 has to be compared with a[0], a[1], a[2], a[3], a[4].
But, once the value of I is greater than or equal to 5, it is an indication that item is not present and display the message “Unsuccessful Search”.

Note: In general the terminal condition in the for loop i<5 can be replaced by i<n.
You are already familiar with the algorithm and flowchart of linear search . Now, the C program for linear search is shown below:

#include<stdio.h>
#include<conio.h>
main()
{
int i,n,key,a[20];
printf("Enter the value of n.");
scanf("%d",&n);
printf("Enter n values:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter item to search:");
scanf("%d",&key);
/* Search the key in array */
for(i=o;i<n;i++)
{
if(a[i]==key)
{
printf("item found");
exit(0);
}
}
printf("Not found");
}

Output
Linear Search in C

Linear Search in C

Advantage of linear search

  • Very simple approach
  • Works well for small array
  • Used to search when the elements are not sorted (not in any order)
Disadvantage of linear search

  • Less efficient if the array size is large
  • If the elements are already sorted, linear search is not efficient.

Note : When the elements are not sorted and size is very less, linear search is used . if the elements are sorted , a better method or technique binary search can be used.

Binary Search in C
Selection Search in C

Sunday, March 1, 2015

Difference between malloc( ) and calloc() in C language

In c language article we will see the difference between malloc() and calloc(). Both are the functions in c language. See the table which is mentioned below:


Malloc()
Calloc()
The Syntax of malloc() is :
Ptr = (data_type *) malloc(size);

The required number of bytes to be allocated is specified as argument i.e. size un bytes.
The Syntax of calloc() is :
Ptr = (data_type*)calloc(n,size);
Takes two arguments: n is number of blocks to be allocated, size is number of bytes to be allocated for each block.
Allocates a block of memory of size bytes.
Allocates multiple blocks of memory, each block with the same size.
Allocated space will not be initialized
Each byte of allocated space is initialized to zero.
Since no initialization takes place, time efficiency is higher than calloc()
Calloc() is slightly more computationally expensive because of zero filling but, occasionally, more convenient than malloc().
This function can allocate the required size of memory even if the memory is not available contiguously but available at different locations
This function can allocate the required number of blocks contiguously. If required memory cannot be allocated contiguously, it returns NULL.

Advantages and Disadvantages of pointers in C language

By this time, you might have understood the concepts of  C pointers and if any problem is given, you should be in a position to solve. After understanding the full concepts of pointers, we should be in a position to answer the question " What are the advantages and disadvantages of pointers?"

Advantages

  1. More than one value can be returned using pointer concept (pass by reference).
  2. Very compact code can be written using pointers.
  3. Data accessing is much faster when compared to arrays.
  4. Using pointers, we can access byte or word locations and the CPU register directly. The pointers in C are mainly useful in processing of non-primitive data structures such as arrays, linked list etc.


Disadvantages

  1. Un-initialized pointers or pointers containing invalid addresses can cause system crash.
  2. They are likely to be used incorrectly causing bugs that are very difficult to identify and rectify.
  3. They are confusing and difficult to understand in the beginning and if they are misused the result is unpredictable.

Sunday, February 22, 2015

C language: Searching in graph

Similar to traversal of graph two searching techniques of the graph are used which are Breadth First Search and Depth First Search. Both the techniques are same as respective traversal techniques. In case of breadth first search the traversal algorithm for breadth first is used. The algorithm is terminated with a message search successful whenever the deleted item from QUEUE is the node to be searched otherwise the algorithm is terminated with a message search unsuccessful when the QUEUE is empty. The formal algorithm for BFS is:

GRAPHBFS
[goal node is the node to be searched]
Mark the start node and insert it in the QUEUE
If the start node is the goal node Then:
Write: 'Search Successful' ; Exit
[End of If]
Repeat While QUEUE is not empty
Delete QUEUE
If deleted node is the goal node Then:
Write: ' Search successful;; Exit.
Else
Mark the unmarked adjacent nodes of the deleted node.
Insert the marked nodes(if any) of the deleted node in the QUEUE.
[End of If]
[End of While]
Write: 'Search unsuccessful'
Exit.

In case of depth first search the traversal algorithm for depth first is used. The algorithm is terminated with a message search successful whenever the popped item from STACK  is the node to be searched otherwise the algorithm is terminated with a message search unsuccessful when the STACK is empty. The formal algorithm for DFS is:

GRAPHDFT
[goal node is the node to be searched]
Mark the start node and push it on to the STACK
If the start node is the goal node Then:
Write: 'Search Successful'; Exit
[End of If]
Repeat While STACK is not empty
POP STACK.
If popped node is the goal node Then:
Write: ' Search successful'; Exit.
Else
Mark the unmarked adjacent nodes of the popped node. 
Push the marked nodes of the deleted node(if any) on to the STACK.
[End of While]
Write: 'Search unsuccessful'
Exit. 

C Language: Depth First Traversal in Graph

This traversal techniques is based on the fact of visiting all the nodes of graph in the depth of it. It means start from the start node of the graph and reach to last node of the graph in its depth (so that no further unexplored adjacent node exist). From the start node explore all the adjacent nodes but visit one of the adjacent nodes. From the visited adjacent node further explore all the adjacent nodes of it and again select one of the adjacent nodes and further explore it. In this way the exploration of the adjacent node carried till to reach the last node. Once the last node is reached then back track from the last node to previous node to visit the next adjacent node of it. The data structure stack is used in this type of traversal. 
Consider the following graph:
Depth First Traversal in Graph
The adjacent nodes of node
The adjacent nodes of node

The depth first traversal of the above graph, assuming node 'A' as start node is:

A     E      D      C     B

You can observe from the traversal result that the first node visited is the starting node. Then the next node visited is 'E' one of the adjacent nodes of the start node 'A'. Then the adjacent nodes of 'E' that are not marked are explored. The only marked node is 'D'. It is visited. There are no further adjacent nodes of 'D'  that are not marked . So, we have reached the other end of the graph. Now backtrack from 'D' to 'E' no adjacent nodes of 'E' are left to be visited. Again backtrack from 'E' to 'A'. There are two adjacent nodes left that are not visited. So, node 'C' is visited next. There are no adjacent nodes of node 'C' that are left for visiting. Again backtrack from 'C' to 'A' and visited the last node 'B' which is adjacent of 'A' all the nodes of the graph are visited and the traversal is complete. 

As we backtrack in the technique of depth first traversal it is necessary to use the last in first out data structure that is stack to store the nodes that are to be backtracked.
You can again remember that the traversal result may be different to the one given above. It differs because the adjacent nodes of a visited node may be pushed on the stack in any order. So, the other result of depth first traversal of the above graph are:

A        B       D       C        E
A        C       D       B        E     etc.

The formal algorithm of DFT(Depth First Search) is : 
GRAPHDFT
[TA is the one dimensional array of size n where n is is the number of nodes in the given graph] 
Mark the start node and push it on to the stack 
Repeat While STACK is not empty
POP STACK 
Add popped node to TA at the next position. 
Mark the unmarked adjacent adjacent nodes of the popped node.
Push the marked nodes of the deleted node (if any) on to the STACK.
[End of While] 
Print TA from the first position as traversal.
Exit.

The above algorithm works in the following manner. 
Consider the following graph
Consider the following graph

Let us assuming the start node as node 'A' let us mark and push the node 'A' on to the stack. So, the stack is:
Depth First Traversal in Graph

when pop stack is executed, the node obtained is 'A'. It is added to the traversed array. So, the traversed array is:
Depth First Traversal in Graph


The marked adjacent nodes to popped node 'A' are, 'B', 'C' and 'E'. Mark and push the nodes 'B', 'C' and 'E' on to the stack (in any order). So, the stack is:
Depth First Traversal in Graph

stack is not empty the processes is repeated.
When pop stack is executed, the node obtained is 'E'. It is added to the traversed array. So the traversed array is:
Depth First Traversal in Graph

The marked adjacent node of popped node 'E' is 'D' mark and push it on to the stack. So, the stack is:
Depth First Traversal in Graph

Stack is not empty the processes is repeated. When pop stack is executed, the node obtained is 'D'. It is added to the traversed array. So, the traversed array is :
Depth First Traversal in Graph

The adjacent nodes of poped node 'D' are 'B' and 'C' but they are already marked node. So, no nodes are pushed. So, the stack is:

Depth First Traversal in Graph

Stack is not empty the proceses is repeated. When pop stack is executed, the node obtained is 'C'. It is added to the traversed array. So, the traversed array is:
Depth First Traversal in Graph

The adjacent node of poped node 'C' are 'A' and 'D' but they are already marked nodes. So, no nodes are pushed. So, the stack is
stack is not empty the processes is repeated. When popped stack is executed, the node obtained is 'B'. It is added to the traversed array. So, the traversed array is 
Depth First Traversal in Graph

the adjacent node of popped node 'B' is 'D' but it is already marked. So, no node are pushed. So, the stack is:
Depth First Traversal in Graph

Stack is empty stop the process. When the TA array is printed we get the Depth first traversal as:

Successors of C language

C and even B have several direct descendants, through they do not rival pascal. One side branch developed early. When Steven Johnson visited the University of Waterloo on sabbatical in 1972, he brought B with him. It became popular on the Honeywell machines there, and later spawned Eh and Zed (the Canadian answers to 'what follows B?'). When Johnson returned to Bell Labs in 1973, he was disconcerted to find the language whose seeds he brought to Canada had evolved back home; even his own yacc program had been rewritten in C, by Alan Snyder.

More recent descendants of C proper include Concurrent C, Objective C, C* and especially C++. The language is also widely used as an intermediate representation (essentially, as a portable assembly language) for a wide variety of compilers, both for direct descendants like C++, and independent languages like Modula 3 and Eiffel. 

Saturday, February 21, 2015

C Language standards

C came into existence in between 1969-1973 in parallel with the development of UNIX operating system. The C Programming Language, in the middle of 1980s,was officially standardized by the ANSI X3J11 committee. Until the early 1980s, the language was almost exclusively associated with UNIX. Now, its use has spread much more widely, and today it is among the languages most commonly used in the industry.

BCPL the origination for C language was designed by Martin Richard in the mid-1960s and used during the early 1970s for several projects including OS6 operating system at OXford. The original BCPL compiler was transported both to Multics and to the GE-635 GECOS system by Rudd Canaday and others at Bell Labs. It was the language of choice among the group of people who involved with UNIX.

BCPL, B, and C all fit firmly in the traditional procedural family characterized by Fortan and Algo60. They are particularly oriented towards system programming. They are close to the machine. BCPL, B and C differ syntactically in many details, but broadly they are similar. Programs consist of a sequence of global declarations and function declarations. Several syntactic and lexical mechanism of BCPL are more elegent and regular than those of B and C. In spite of the differences , most of the statements and operators of BCPL map directly into corresponding B and C.

During the 1980s the use of the C language spread widely, and compiler became available on nearly every machine architecture and operating system; in particular it became popular as a programming tool for personal computers, both for manufactures of commercial software for these machine, and for end-users interested in programming.

Features of C Language

Definition: The capabilities and functionality provided by the C language are collectively called features of C Language. The features of C Language have made it popular.

Let us see, "What are the features of C language ?"

The C Language being a middle level language has many important features. It is said to be middle level language because of the following features:


  • The C language has the capabilities of assembly Language (Low Level Language)
  • It provide the functionality of a High Level Language.

As it is near to machine as well as to the user it is called middle level language. The capabilities of the low level language of C help in designing the program
especially required system management. It means the system software can be developed very easily using the C Language.

As It is also near to the user, most general purpose programs can be developed easily. So, the application programs can also be developed using C language.

In addition to the above mentioned features, C Language also has the following features:


  • Implementation of modular programming technique is very easy in case of C language because of the easy designing and handling of modules in the form of functions.
  • Each and every task using C Language is solved by means of function only so, C is called as functional language.
  • Users can define their own tasks in the form of user defined functions.
  • It has large collection of built-in or library functions that makes the programming task easier.
  • More and more user defined functions can be added to the library of the C to make the programs extendable.
  • C language is rich in operators containing very common arithmetic operators to system level bit-wise operators.
  • The programs written using C language are portable and can be executed at different platform.
  • C is a case sensitive language. All the keywords, data type means and built-in function names are written only using lower-case letters.
  • Most importantly the debugging, testing and maintenance activities of programs development can be performed easily on the program developed using C Language.
  • Implementation of structured programming technique is easy here because of the rich availability of the sequential, branching and looping structures.

Friday, February 20, 2015

Pointer values in C language

Suppose, we have the following declaration:

int i=100, j=200, k=300;

This declaration tells the compiler to perform the following activities:

  • Reserve space for three integer values in memory.
  • Associate the variables i,j and k with these memory location.
  • Store 100,200 and 300 at the locations i,j and k respectively as shown as below:

Variable
Address
Memory Locations

0



2



4
100
I
……..
200
J
65534
300
K



variables
address
Values



The address of the variable cannot be accessed directly. The address of the variable can be obtained using address operator (& in C) .

Note : The address operator can be used with any variable that can be placed on the left side of an assignment operator.Since constants, expressions and array names can not be used on the left hand side of the assignment, and hence accessing address is invalid for constants, expressions and array names. The following are invalid:


Usage
Valid/Invalid
Reasons for invalidity
&100
Invalid
Address of a constant cannot be obtained.
&(p+100)
Invalid
Address of an expression cannot be obtained.
&(p+q)

Invalid
Address of an expression cannot be obtained.
Int a[10];
&a;
Invalid
Address of entire array cannot be obtained.
Register a;
&a;
Invalid
Address of a register variable cannot be obtained.

Note : if a is an array , then address of the first location of the array is obtained either using : &a[0] or a.

Assume that the compiler has chosen the address 65530 for the variable i, the address 65532 for the variable j and 65534 for the variable k. These address assigned to variables i,j and k are called pointer values.

Definition : Memory is divided into number of storage cells called locations. All the locations in computer memory are numbered sequentially from 0 to 65535 ( with memory size of 64k) . Out of these address, the system assigns some addresses of the memory locations to the variables. These memory addresses assigned to variables by the system are called pointer values. 

Thursday, February 19, 2015

Array in C language

Introduction

In this chapter, we will discuss a very important data type arrays. Let us see, “Why arrays?’’
We know that in one variable we can store the information of only one  data item. Suppose that a
Student has scored 90 marks. These marks can be stored in a variable as shown below:

int marks=90;

After executing this statement, the value 90 will be stored in the variable marks. Suppose there is
A need to store marks of 10 students. In such case, we are forced to use 10 variables like marksl,
Marks2… marks10. But, if it is required to store the marks of 100 students, definitely it is not
Feasible to use 100 variables marksl ,marks2…marks100, Now, the question is “How to store 100
different marks?” In mathematics, we use sets to group the items of similar kind, For example,
consider the set shown below :

marks = {80,90,45,99,100,36,88,96,67,92}

This is a set of marks of `10 students. Note that every item can be accessed by prefixing marks along
With the position of marks in the set. The first item 80 corresponds to the marks of first student,90
Corresponds to the marks of second student and so on i.e., marks1=80, marks2=90…marks 10=92.
In C language, this is where, the concept of arrays is used. Since all the marks are of the same type,
We can group and refer all the marks  with a common name using arrays.
Note: In general, if more number of data items of same type (kind) are to be accessed or read
or stored, then we use arrays.


The meaning of an array

Definition : An array is defined as an ordered set of similar data items. All the data items of
an array are stored in consecutive memory locations in main memory . The elements of an array
are of same data type and each item can be accessed using the same name. e.g. consider an
array of marks of 5 students as shown below:

80
90
45
99
100
Marks(0)
Marks(1)
Marks(2)
Marks(3)
Marks(4)

To refer an item in the array, we specify the name of the array along with position of the item. The
Position of the item must be written within square brackets ‘ []’.  The position of the item, The item enclosed within square brackets is called ‘subscript’ or ‘index’.

For example, the above figure represents an integer array called marks where marks of 5 students
Are stored. The marks of each student can be accessed as shown below:

       marks[0] i.e. 80 – represent marks of first student
       marks[ 1] i.e. 90- represent marks of second student
       marks[2] i.e. 45- represent marks of third student
      marks [3] i.e. 99- represent marks of fourth student
       marks[4] i.e. 100 – represent marks of fifth student

Thus using marks [0] through marks[4]  we can access the  marks of 5 students.
Note: Using marks [0] through marks [n-1] We can access the marks of n students in general.
In an array it not possible to have a group of items with different data types. Types. For example,

83
94.8
“Delhi”
‘3’
910
a[0]
a[1]
a[2]
a[3]
a[4]

Is invalid way of storing the elements in an array. This is because, it is a collection of int,  float, char
and string datatypes. Once we know the definition of an array. The next question is “How arrays
are classified?” The arrays can be classified based on how the data items are arranged for human
understanding. This is pictorially represented as shown below:



© Copyright 2013 Computer Programming | All Right Reserved