Derived Data Types in C Programming Language

Posted on January 25, 2010

Derived Data Types in C Language

Derived data types in C Programming Language are those C data types which are derived from the fundamental data types using some declaration operators.

Now, in this tutorial we will be discussing each of these derived data types in brief as we will again come across these topics in the next coming up sections.

1. Arrays:
Arrays can be defined as a set of finite and homogeneous data elements. Each element of an array is referenced using an index.

For example:
If we the name of an array is AR which have 5 elements then the array will be represented as :
AR[0], AR[1], AR[2], AR[3], AR[4]
Here, these subscripts which are containing the digit is known as an index.

There are various types of arrays namely:

  • One dimensional
  • Two dimensional
  • Multi dimensional

The elements of an array are indexed from 0 to size-1. It is necessary to declare the size of an array before initialization. An array can be initialize by placing the elements of an array within the curly braces.

For example, an array initialization can be done in following manner:

int AR[5] = {5, 2, 6, 8, 3};

A sample program of an array:

#include
Void main()
{
int arr[10];
arr[10] = {1,5,4,6,3,9,14,81,7,11};
printf(“\n %d”,arr[i]);
}

2. Functions:
A function can be defined as a part of the program which is declared by the programmer in any part of the program and a function can be of any name depending upon the choice of the programmer. The function declared can be invoked from other part of the program.

Example of a function:

#include
Float square(float);
Void main()
{
Float num=3.14;
Float sq;
Sq=square(num);
Printf(“%f”,sq);
}

Float square(float x)
{
Return x * x;
}

The above code will print the square of the above given number. square is the name of the function used. And sq is the variable used to store the value of the square. The use of the function before the main function is known as declaration of the function, then comes the definition of a function which takes place outside the main function. Then finally comes the calling of the function which takes place inside the main function.

3. Pointers:
A pointer is a variable that holds the address of the memory space. We can say that if one variable can hold the address of the another variable then it is said that the first variable is pointing to the second.

A pointer is declared in a following manner:

int *temp;

i.e. first we have to declare the type and then the variable name precede by an * (asterisk) sign.

The pointer variable always occupies 2 bytes of memory.

4. Structures:
A Structure can be defined as a collection or a group of variables that are referenced under one name. it is used to keep related information together. Here we use a ‘struct’ keyword to construct a structure.

Example:

Struct bank
{
Char name[30];
Int account_no;
Int amount;
};

Struct b;

In the above example bank is the name of the structure, then we have declared the variables which are the elements of this structure. And b is the object of the structure which is used to refer the elements of the structure.

Elements will be referred as :

b.amount=5000;
b.account_no=2546321;

5. Class:
A class can be defined as a group of objects which are similar or of the same kind. Here a keyword ‘class’ is used to construct a class. A Class almost follows the same declaration procedure as we did in case of structures. But here comes the concept of access specifiers which applies a kind of restriction on the data. There are three types of access specifiers namely public, protected and private.

Example of a class:

Class student
{
Char name[20];
Int roll_no;
Public:
Details();
Display();
};

Student stud;

In the above example the student is the name of the class, and stud is the name of the object. Here two functions details and display are called member functions.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Looking for a Fresher Job? Post Your Resume Free !!!

More Articles From "C Programming" Category

home | top

TopOfBlogs