Non-Primitive Data Types In C
If you need to store more values of the same data type, what are your options? Primitive or basic data types are used to preserve single values in various forms. In this situation, non-primitive data types let you combine the basic types of data and store multiple values in one variable.
Non-primitive types of data are those that the user defines for themselves, which means you can combine as many components of associated information types as you need. A derived data type can be one of four different types:
- Array
- Pointer
- Structure
- Union
Array
“An array is a collection of finite objects of identical type that share similar types.” By their respective indices, these entities or elements may be referred to. As is typical, indexing begins with 0 and goes up to array_size-1. One, two, or multiple dimensions are all possible for an array.
Syntax
data_type arr_name[size];
Describe the Syntax
- data_type : The type of data identifies the kind of elements that will be kept within the array. It is capable of being int, float, double, or char.
- array_name: The set of arrays is known by this name. You have to conform to the same rules that apply when declaring a regular variable in C when specifying the name that is part of an array.
- size: The size of an array specifies how many elements it can hold. The total number of elements in an array will be n-1 if the array's dimension is n..
Example
#include <stdio.h>
int main()
{
int i, x;
int array[5] = {1,2,3,4};
printf("Enter element to be searched:\n");
scanf("%d", &x);
for (i = 0; i <= 5; i++)
{
if (array[i] == x)
{
printf("Element is found at idex %d", i);
break;
}
}
if (i == 6)
{
printf("\nElement is not found!");
}
return 0;
}
Output
![Non-Primitive Data Types In C](https://static.tutorialandexample.com/c/non-primitive-data-types-in-c1.png)
Explanation
To determine whether an input element is present in an array or not, use the example above. In this case, a 5-dimensional array of the integer type has been declared. Verifying to see if the specified element is present in the array is done by iterating through it.
Functions
The function is a section of code that, when called by the program, carries out a specific task. It can be called from anywhere and as numerous times as necessary within the program. The return type of the function determines the return value, or the kind of value that will be returned.
The function might be called in C using the call by value or call by reference methods. If a function is called by value, a copy of the variable is included as an argument; however, when a function is identified by reference, the address or reference of the variable itself is passed to the function.
Syntax
return_type function_name(parameters);
Syntax description:
- return_type's : The data type for this function determines the kind of information that it will return. It is not necessary for the function to return a value if the return type is void.
- function_name: The name of a function is function_name. The exact same rules that apply when declaring a typical variable in C must be followed when specifying the name of a function.
- parameters: The conditions are not required. The format of the function call determines how they are passed.
Example
#include <stdio.h>
int max(int a, int b){
int c;
if (a > b)
c = a;
else
c = b;
return c;
}
int main()
{
int a=21;
int b= 24;
int c;
c = max(a, b);
printf("Maximum number between two numbers is : %d\n", c);
return 0;
}
Output
![Non-Primitive Data Types In C](https://static.tutorialandexample.com/c/non-primitive-data-types-in-c2.png)
Explanation
The two numbers in the aforementioned example are compared to determine which is the highest. In this case, we've passed the function two numbers separated by value that need to be compared. The function compares two numbers and returns the larger of the two numbers because it has an int return type.
Pointers
A variable that maintains the address of other parameters is known as a pointer. The address provided identifies the location of the variable in memory. A is pointing to b if it contains an address for b in its storage. A pointer's data type must correspond to the data type associated with the variable with which it is associated.
Syntax
type *pointer_name;
Syntax Description
Type: The type of value that the pointer points to is defined by this data type.
pointer_name: This indicates the name of the pointer. The exact same guidelines that apply when declaring a typical variable in C must be followed when specifying the title of a pointer. The pointer has to always be accompanied by an asterisk (*), excluding the cases described above.
Example:
#include <stdio.h>
int main(){
int arr1[5] = {23, 45, 56, 78,70};
int *pt;
pt = arr1;
for (int i = 0; i <5 ; i++)
{
printf("Value of *pointer = %d\n", *pt);
printf("Value of pointer = %p\n\n", pt);
pt++;
}
}
Output
![Non-Primitive Data Types In C](https://static.tutorialandexample.com/c/non-primitive-data-types-in-c3.png)
Explanation
The address that is part of the array arr is held by a pointer ptr that was specified in the example above. The value at every position as well as the element's address were printed after we iterated through the array in a loop.
Structure
In C, you may combine members of different kinds under a single name (or the struct type) by using a user-defined data type called a structure. The fact that the variables of various types are combined in a single structure and can be defined in accordance with the user's needs is the basis for the term "user-defined data type."
Syntax
struct structure_name
{
data_type var1;
data_type var2;
};
Syntax Explanation
- struct: The term struct will be followed by the name of a structure in the definition. After being declared to be a part of a structure, all of the things that are inside are referred to as its members.
- data_type: The data type of every variable is mutable. Inside a structure, variables for all kinds of information can be declared.
A semicolon marks the end of the definition of a structure.
Example
#include<stdio.h>
#include <string.h>
struct book {
int id;
char name[25];
char author[50];
char genre[20]; };
int main( )
{
struct book a1;
a1.id = 12;
strcpy(a1.name, "ABC");
strcpy(a1.author, "ABC Author");
strcpy(a1.genre, "ABC Fiction");
printf( "The id is: %d\n", a1.id);
printf( " name of the book is: %s\n", a1.name);
printf( " author name of the book is: %s\n", a1.author);
printf( " genre of the book is: %s\n", a1.genre);
return 0;
}
Output
![Non-Primitive Data Types In C](https://static.tutorialandexample.com/c/non-primitive-data-types-in-c4.png)
Explanation
In the example above, four member variables—id, name, author, and genre—are used to describe a structured book. These four members are going to be separated into separate copies and assigned to each parameter of the book type. Therefore, each of these four variables utilized for storing the data for this book is also present in the variable b1, which is a duplicate of them.
Union
Another type of user-defined data is a union. Additionally, it stores members of various data types under a single name. A union and a structure are conceptually comparable and share a similar sound. Additionally, there are some significant distinctions between the two. whereas a union only allots storage space equal to its largest member, a structure allots memory adequate for each of its members.
Syntax
union structure_name
{
data_type var1;
data_type var2;
};
Syntax Explanation
- union: The word "union" appears at the start of the description of a union in C. The name of the union is mentioned after that.
- data_type: This refers to the type of information that is stored in the union member variable. A union may have members of different types.
Example
#include<stdio.h>
#include<string.h>
union place
{
int pinCode;
char name[20];
};
int main( )
{
union place p1;
p1.pinCode = 503111;
strcpy( p1.name, "Hyderabad");
printf("Pin code of the place: %d\n", p1.pinCode);
printf("Name of the Place is: %s\n", p1.name);
}
Output
![Non-Primitive Data Types In C](https://static.tutorialandexample.com/c/non-primitive-data-types-in-c5.png)
Enumeration
Enumeration, also known as enum, is one of the user-defined data types in C that offers a unique kind of flexibility in the definition of variables. An enum is made up of a number of integer variables that can have user-defined names substituted for them.
Syntax
enum flag {const_name1, const_name2, ..., const_nameN};
Syntax Explanation
- enum: Enum is the keyword that appears at the start of the definition.
- flag: The enumeration set is identified by this name by default. It can either be used in place of a different title or left alone.
- const_name: An integral identifier within the enum set is represented by the constant name. These are the set's default values: 0, 1, 2,...
Example
#include <stdio.h>
enum colour { pink, black, blue };
enum fruit { mango = 10, apple, orange };
int main(){
enum colour s2;
enum fruit d2;
s2 = black;
d2 = apple;
printf("The second value in enum set is: %d\n",s2);
printf("The second value in enum set is: %d\n",d2);
return 0;
}
Output
![Non-Primitive Data Types In C](https://static.tutorialandexample.com/c/non-primitive-data-types-in-c6.png)
Explanation
The two enum flags are defined in the aforementioned instance. pink, black, and blue are the first three flag colors in the enumeration, and their default values are 0 and 1. As a result, 1 will be shown in the output when the second blue constant is printed. mango is the first constant and is initialized with 10 within the second enum flag fruit, which also includes apples and orange. As a result, the contents of the subsequent constants will be updated automatically. In the output, 11 is shown if the subsequent constant of the enum fruit is printed.