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

No comments: