Friday, August 22, 2008

Road Map for "C" Language


Variables

A variable is that whose value changes. They are used to allocate memory and also store a value. Every variable must have a name to it.

C provides a wide range of types. The most common are

int An Interger
float A floating point number
char A single byte of memory, enough to hold a character

There are also several variants on these types.

short an integer, possibly of reduced range
long an integer, possibly of increased range
unsigned an integer with no negative range, the spare capacity being used to increase the positive range
double a double precision floating point number

Arrays

An array is a group of related data items. All these data items are stored in contiguous memory locations and they have a common name. There are 3 types of arrays.

  1. One Dimensional Array
  2. Two Dimensional Array
  3. Multi-Dimensional Array

One Dimensional Array

A list of items can be given one variable name with only one subscript or index then such a variable is called as One Dimensional Array.

Syntax: datatype array-name[array-size];

 

Two Dimensional Array

In some situations we have to store a table of values. For this purpose we will use a two dimensional array. It will contain an array name with two subscripts or index. One is used to represent the memory locations in the index.

Syntax: datatype array-name[row-size][column-size];

Multi Dimensional Array

If an array contains more than one sub script or index then it is referred as Multi Dimensional Arrays. They can be two, three, four etc of dimensional arrays. The mostly used are either one, two or three dimensional arrays.

Syntax: datatype array-name[siz-dim1][siz-dim2]….[siz-dimn];

Operators

An operator is a symbol that tells the computer to perform some mathematical or logical manipulations. The operators in C are divided into number of categories.

Arithmetic Operators

+ Addition

- Subtraction

* Multiplication

/ Division

Modulus Operator

% Modulo Reduction(Remainder from integer division)

Relational Operators

< Less than

> Greater than

<= Less than or equal to

>= Greater than or equal to

== Equal to

!= Not equal to

Logical Operators

&& Logical AND

|| Logical OR

! Logical NOT

Arithmetic Assignment Operator

If the = sign is combined with other arithmetic operators, then the operator is referred as compound arithmetic assignment operator.

Syntax: ‘operator=’

Example: i+=2

Increment and Decrement Operator

The increment operator is used to increase the value of a variable by 1 and the decrement operator is used to decrease the value by1.

Post increment and decrement operator

Syntax: ++

--

Pre increment and decrement operator

Syntax: ++

--

Control Statements

These Statements are used to select among one or several alternatives. The different Control statements are

1. If Statement

2. If-else Statement

3. Nested if-else Statement

4. Ladder if or else if Statement

5. Switch Statement

Loops

These statements are used to perform any action repeated number of times. The different types of looping statements are

  1. For Loop
  2. Nested For Loop
  3. While Loop
  4. Do-While Loop

Function

A function is a set of program instructions. Functions can be also referred as a small program which can be accessed by other programs.

The different components of a function are

  1. Function Declaration or Prototype
  2. Function Call
  3. Function Definition
  4. Function Parameters
  5. Return Statement

Pointers

A pointer is that which points to an address of a variable. A pointer is like any other variable i.e. it has t be defined before it is used in the program. The C compiler needs to know the type of variable the pointer points to. Hence we declare the pointer with the same data type as that of the variables data type to which the pointer points to. Memory will not be allocated for the pointer when it is defined. Its memory will be allocated when the pointer actually points to any variable. It will get the same memory as that of the variable. Thus the data types of the pointer and the variable to which the pointer points should be the same.

Void Pointer

Pointers defined to be of a specific data type cannot hold the address of ay other data type of a variable. But if we define pointer as “void” then it can point to the address of any other type of variable.

Pointer Arithmetic

We can also perform arithmetic operations on pointers. The unary operators that can be used with pointers are the increment and decrement operators. The binary operators that can be used with pointers are the “+” and “-“ operators.

Pointers to Pointers

A pointer to pointer means that the pointer will point to another pointer and this pointer will point to the target value. This is mainly used when working with multi dimensional arrays.

Structures

Structures combine logically related data items into a single unit. These data items are enclosed with in a structure known as members and they can be of same or different data types. It helps in better organization and management of data in a program.

Defining a Structure

The structure definition creates structure variables and allocates storage space for them. These variables can be created at the point of structure declaration itself or by using the structure name as and when required.

Syntax: Struct Structure-name

{

data-type member1;

data-type member2;

….

};

Preprocessor

Preprocessor are commands which are executed by the C processor before the program is assigned to the compiler for compilation. Every C preprocessor directive start with the “#” (hash) symbol. Usually preprocessors appear at the beginning of the program but it is not a necessary condition. The following are the preprocessor directives:

  1. #define
  2. #include
  3. #if, #elif
  4. #undef

Saturday, August 16, 2008

Hello "C"

History


C was developed at Bell Laboratories in 1972 by Dennis Ritchie. Many of its principles and ideas were taken from the earlier language B, BCPL and CPL ( Combined Programming Language ) . The one major drawback of CPL was that it was too large for use in many applications. In 1967, BCPL ( Basic CPL ) was created as a scaled down version of CPL while still retaining its basic features. In 1970, Ken Thompson, while working at Bell Labs, took this process further by developing the B language. B was a scaled down version of BCPL written specifically for use in systems programming. Finally in 1972, a co-worker of Ken Thompson, Dennis Ritchie, returned some of the generality found in BCPL to the B language in the process of developing the language we now know as C.

C's power and flexibility soon became apparent. Because of this, the Unix operating system which was originally written in assembly language, was almost immediately re-written in C . Soon, many different organizations began using their own versions of C causing compatibility problems. In response to this in 1983, the American National Standards Institute ( ANSI ) formed a committee to establish a standard definition of C which became known as ANSI Standard C. Today C is in widespread use with a rich standard library of functions.

Significant Language Features


C is a powerful, flexible language that provides fast program execution and imposes few constraints on the programmer. It allows low level access to information and commands while still retaining the portability and syntax of a high level language. These qualities make it a useful language for both systems programming and general purpose programs.

C's power and fast program execution come from it's ability to access low level commands, similar to assembly language, but with high level syntax. It's flexibility comes from the many ways the programmer has to accomplish the same tasks. C includes bitwise operators along with powerful pointer manipulation capabilities. C imposes few constraints on the programmer. The main area this shows up is in C's lack of type checking. This can be a powerful advantage to an experienced programmer .

C has also been widely used to implement end-user applications, although as applications became larger much of that development shifted to other, higher-level languages.

One consequence of C's wide acceptance and efficiency is that the compilers, libraries, and interpreters of other higher-level languages are often implemented in C.

Another strong point of C is it's use of modularity. Sections of code can be stored in libraries for re-use in future programs. This concept of modularity also helps with C's portability and execution speed. The core C language leaves out many features included in the core of other languages. These functions are instead stored in the C Standard Library where they can be called on when needed.. An example of this concept would be C's lack of built in I/O capabilities. I/O functions tend to slow down program execution and also be machine independent when running optimally. For these reasons, they are stored in a library separately from the C language and only included when necessary.

Areas of Application


The C language is used in :
UNIX operating system
computer games.
Although C was designed for implementing system software ,it is also widely used for developing application software.