Categories
b. Derived Data Type

Functions

A function is a piece of code that performs some specific task when invoked in the program. It can be called from anywhere and any number of times in the program. The return value i.e., what type of value it will return depends upon the return type of the function.

In C, a function can be called by types: call by value and call by reference. When the function is called by value, a copy of the variable is passed as the argument whereas when the function is called by the reference, the address or reference of variable itself is passed to the function.

Syntax

return_type function_name(parameters);

Description of the Syntax

  • return_type: This is the data type that specifies the type of value to be returned by the function. If the return type is void, then it is not mandatory for the function to return a value.
  • function_name: This is the name of the function. To specify the name of a function, you must follow the same rules which are applicable while declaring a usual variable in C.
  • parameters: The parameters are optional. They are passed according to the type of the function call.

Example

The following example illustrates functions in C.

#include <stdio.h>

// function returning the maximum

// numbers between two integers.

int max_num(int num1, int num2)

{

    // local variable declaration

int res; 

if (num1 > num2)

res = num1;

else

res = num2; 

return res;

}

int main()


{

    // local variable definition.

int num1 = 225;

int num2 = 250;

int res;

     // function call.

res = max_num(num1, num2); 

    // print the result.

printf("Maximum number is : %d\n", res);

return 0;

}
Data_Types_in_C_7

The above example is comparing two numbers and finding the greatest among them. Here, we have passed two numbers that need to be compared, to the function by value. The function has an int return type so, it is returning the greater number after making a comparison.

Categories
b. Derived Data Type

Union

A union is also a user-defined data type. It also holds members of different data types under a single name. A union sounds similar to a structure and they are similar in conceptual terms. But there are some major differences between the two. While a structure allocates sufficient memory for all its members, a union only allocates memory equal to its largest member. 

Syntax 

// define a structure.

union structure_name 

{

    data_type var1;

    data_type var2;   

};

Description of the Syntax

  • union: The union keyword is written at the beginning of the definition of a union in C. After it,  the name of the union is specified.
  • data_type: It is the data type of the member variable of the union. Members of different types can be defined inside a union.

To understand the major difference between a structure and a union, consider the following definitions of a structure and a union:

Structure definition

struct book      

{  

    int price;      // 4 bytes

    char name[10];  // 1*10 = 10 bytes    

};  

Union definition

union book      

{  

    int price;      // 4 bytes

    char name[10];  // 1*10 = 10 bytes    

};

An object of the structure book would be allocated 14 bytes for both the int and char members. However, an object of the union book would only be allocated 10 bytes (equal to the memory required by the char member) which is the maximum size.

Example

The following example illustrates union in C.

#include<stdio.h>

#include<string.h>

// define the union. 

union city

{

int pinCode;

char name[20];

};

int main( )

{

   // object of the type "city".

union city c1; 

c1.pinCode = 110090;

strcpy( c1.name, "Delhi");

   // print the information.

printf("The pin code of the city: %d\n", c1.pinCode);   

printf("The name of the city is: %s\n", c1.name); 

}

Data_Types_in_C_10

In the above example, the char array name is printed correctly whereas the pinCode gives a corrupted value. This happened because the name occupied the space allocated for object c1. 

Categories
b. Derived Data Type

Structure

A structure is a user-defined data type in C that allows you to combine members of different types under a single name (or the struct type). The reason why it is called a user-defined data type is that the variables of different types are clubbed together under a single structure, which can be defined according to the user’s choice. 

Consider a situation where you want to store a record of a book. The book will have properties like name, author, and genre. You can create three variables to store this information. But what if you need to store records of 10 books. Then creating 3 separate variables for each book would not be a practical solution. This is where a structure can be a great solution. A single structure book can be created having three members: name, author, and genre. This structure can be used for all 10 books. 

Syntax 

// define a structure.

struct structure_name 

{

    data_type var1;

    data_type var2;    

};

Description of the Syntax

  • struct: The definition of a structure includes the keyword struct followed by its name. All the items inside it are called its members and after being declared inside a structure. 
  • data_type: Each variable can have a different data type. Variables of any data type can be declared inside a structure.
  • The definition of a structure ends with a semicolon at the end.

Example

The following example illustrates structure in C.

#include<stdio.h>  

#include <string.h>  

// define a "user-defined" structure.

struct book      

{

    // declare members of the structure.

int id;      

char name[25];

char author[50];

char genre[20];      

};

int main( )    

{

// declare a variable of the book type.

struct book b1; 

   //store the information of the books.    

b1.id = 10; 

strcpy(b1.name, "Dummy");  

strcpy(b1.author, "Dummy Author"); 

strcpy(b1.genre, "Science Fiction");

   // print the information.    

printf( "The id is: %d\n", b1.id);    

printf( "The name of the book is: %s\n", b1.name);    

printf( "The author of the book is: %s\n", b1.author);    

printf( "The genre of the book is: %s\n", b1.genre);     

return 0;  

}

Data_Types_in_C_9

In the above example, a structured book is defined with 4 member variables: id, name, author, and genre. Now a separate copy of these 4 members will be allocated to any variable of the book type. So, the variable b1 also has its own copy of these 4 variables which are used to store the information about this book.

Categories
b. Derived Data Type

Pointers

A pointer can be defined as a variable that stores the address of other variables. This address signifies where that variable is located in the memory. If a is storing the address of b, then a is pointing to b. The data type of a pointer must be the same as the variable whose address it is storing. 

Syntax

type *pointer_name;

Description of the syntax

  • type: This is the data type that specifies the type of value to which the pointer is pointing.
  • pointer_name: This is the name of the pointer. To specify the name of a pointer, you must follow the same rules which are applicable while declaring a usual variable in C. Apart from these rules, a pointer must always be preceded by an asterisk(*).

Example

The following example illustrates pointers in C.

#include <stdio.h>

int main()

{

      // array declaration and initialization.

int arr[4] = {50, 100, 150, 200}; 


    // int type pointer variable declaration.


int *ptr; 

    // Assign the address of arr[0] to ptr.

ptr = arr;

for (int i = 0; i < 4; i++)

{

printf("Value of *ptr = %d\n", *ptr);

printf("Value of ptr = %p\n\n", ptr); 

        // increment pointer ptr by 1.

ptr++;

}

}



Data_Types_in_C_8.

In the above example, we have declared a pointer ptr that is holding the address of the array arr. We have looped through the array and printed the value at each index along with the address of the element.

Those were the derived data types in C. Moving forward, let us understand the user-defined data types in C.

Categories
b. Derived Data Type

Arrays

An array is a group of similar kinds of finite entities of the same type. These entities or elements can be referred to by their indices respectively. The indexing starts from 0 to (array_size-1) conventionally.  An array can be one-dimensional, two-dimensional, or multidimensional.

Syntax

data_type arr_name[size];

Description of the syntax

  • data_type: This is the data type that specifies the type of elements to be stored in the array. It can be int, float, double, and char.
  • array_name: This is the name of the array. To specify the name of an array, you must follow the same rules which are applicable while declaring a usual variable in C.
  • size: The size specifies the number of elements held by the array. If the size is n then the number of array elements will be n-1.

Example

The following example illustrates the array data types in C.

#include <stdio.h>

{

int idx, element;
    // initialize an array.

int my_array[10] = {10, 0, 29, 8, 52, 14, 16, 100, 2, 27};

printf("Enter element to be searched:\n");

    // input element to be searched.

scanf("%d", &element);

    // traverse the array to search the element.

for (idx = 0; idx <= 9; idx++)

{

if (my_array[idx] == element)

{

            // print the index at which

            // the element is found.

printf("Element found at idxex %d", idx);

break;

}

}

    // if the element is not found.

if (idx == 10)

{
printf("\nElement not found!");

}

return 0;

}
Data_Types_in_C_6

The above example is to check if the input element exists in the array or not. Here, we have declared an integer type array for size 10. We are iterating over the array and checking if the given element is found in the array. 

Categories
b. Derived Data Type

Derived Data type:

Data types that are derived from fundamental data types are called derived data types.

Derived data types do not create new data types. Instead, they add some functionality to the existing data types.

Derived data types are derived from the primitive data types by adding some extra relationships with the various elements of the primary data types. The derived data type can be used to represent a single value or multiple values.

Given below are the various derived data types used in C:

  1. Arrays: An array is an ordered sequence of finite data items of the same data type that share a common name.
  2. pointers: A pointer is a special type of variable used to hold the address of another variable.
  3. Functions: A function is a self-contained block of one or more statements with a name.
  4. Structures: A structure is a collection of different data type items stored in a contiguous memory allocation.
  5. Unions: A union is similar to a structure where the memory allocated to the largest data type is reused for other types in the group.

In some situations, structures and unions can also be called the user-defines data types.