What are data types?

The data type is a format to store data that contains a particular range or type of value. It specifies which type of value a variable should have. In other words, a data type describes the type of data that variables can store. When a program stores data in the variables, then every variable needs to be assigned value that falls within the range of its data type. The following are some of the data types:

  • Integer.
  • Float.
  • String.
  • Double.
  • Character.

User-defined data types

User-defined data types are the data types, which are defined by the user using the available data types. The user can develop data types by simply extending the predefined or built-in types. The other name for the user-defined data type is derived datatype. The following are the various user-defined data types used in the C programming language.

Structure

The keyword struct is used to define the structure data type. To group data types of dissimilar types to a single type, struct data type is used. The general syntax of the struct data type is shown below,

struct structName //defining structure

{

dataType member1; //declaration of struct members

dataType member2;

dataType member3;

};

Example

A structure Student is declared with three fields namely, roll number of type integer, name of type character array and marks of type integer array. Separate locations are reserved for each of these fields when the structure variable is declared.

struct Student

{

int rno;

char name[25];

int marks[5];

};

The following statement declares two structure variables s1 and s2 to store the details of two students.

struct Student s1,s2;

Union

The keyword union is used to define the union data type. The same memory location is shared by the members of the union. The general syntax of the union data type is shown below,

union name_of_union

{

dataType member1;

dataType member2;

---

};

Example

The following piece of code declares a user-defined data type union with three members namely id of type integer, name of type character array and price of type float.

union Product

{

int id;

char name[15];

float price;

};

The following code declares a variable p1 of type Product. Of the given members the character array occupies the maximum space and therefore, 15 bytes are reserved. Other members occupies the same space when referenced.

union Product p1;

Enumeration

The keyword enum is used to define the enumeration data type. The code readability is increased by using the enumeration data type. To assign names to the integral constant values, enumeration is used. The general syntax of the enum data type is shown below,

enum name_of_enum

{

const1,const2...

};

Example

The following code declares a user-defined type Directions which stores four named constants NORTH, SOUTH, EAST and WEST which are assigned values 1, 2, 3 and 4 respectively.

enum Directions

{

NORTH=1, SOUTH, EAST, WEST

};

Additional user-defined types in C++

Along with union, enumeration, and structure, C++ has the following user defined data type.

Class

It is used to tie together data members and member functions into a single unit. It enables data hiding by hiding the private members from the outside world. Only public methods available inside the class can be used to access those private members.

Typedef

It is used to assign alias to the existing datatype.

Java/Python

Java and Python support user-defined types such as classes and interfaces.

Classes

Classes act as template from which any number of objects can be created. It consists of data fields and methods.

Interface

Interface consists of abstract methods which are implemented in concrete class. It enables extension to existing code without affecting other modules dependent on it.

User-defined data type implementation

The code given below shows the implementation of structure, union and enumeration user-defined datatypes in a single program.

Code

#include<stdio.h>

struct student{ //declare a structure student

int studID;  //declare student id as an Integer data type

float marks; //declare marks as float data type

char grade; //declare grade as char data type

}s;

union welcome //declare union of type welcome

{

int x;  //declare a integer variable x

char y; //declare a char variable y

double t; //declare a double variable t

}wel;

enum day {   //Initializing enum data type

Mon, Tue, Wed, Thu, Fri, Sat, Sun  //declare named constant

};

int main()

{

    enum day date;  //declaring variable for an enumeration user-defined data type

    date= Fri; 

    printf("Position = %dn", date);  //prints position of friday

    printf("Size of union operator = %dn",sizeof (wel));//prints the size

    printf("Size of structure operator = %dn",sizeof (s));//prints the size of structure

}

Explanation

The given code creates three user-defined types student of type structure, welcome of type union and day of type enum. It declares a variable date of type enum, assigns a value and then displays the value stored in variable of type enum. In addition, it displays the size of variable of type union and struct. The elements of the program are pictorially represented as shown below.

The image represents user-defined types used in the program

Benefits

  • It provides flexibility.
  • Provides ability to model real-world concepts.
  • Facilitates extensibility.
  • Improves reusability.

Context and Applications

This topic is important for postgraduate and undergraduate courses, particularly for, Bachelors in Computer Science Engineering and Associate of Science in Computer Science.

Practice Problems

Question 1: Which of the user-defined data types stores values in the same memory address?

  1. Structure data type
  2. Union data type
  3. Enum data type
  4. None of the above

Answer: Option B is correct.

Explanation: The union operator stores the variables in the same memory address. But while using the structure data type, it does not store variables in the same memory address. Struct data type stores variables in separate locations.

Question 2: ___data type is used to store numeric values.

  1. Integer
  2. Float
  3. Enum data type
  4. None of the above

Answer: Option A is correct.

Explanation: Integer is a whole number. Integers can be negative, zero, or positive. Integers range from -2,147,483,647 to 2,147,483,647 for about 9 or 10 digits of precision

Question 3: Which one is not an advantage of user-defined data type?

  1. Reusability
  2. Reliability
  3. Flexibility
  4. Extensibility

Answer: Option B is correct.

Explanation: User defined datatypes provides flexibility, extensibility and reusability.

Question 4: Which of the user-defined data types store the fields in separate locations?

  1. Structure data type
  2. Union data type
  3. Enum data type
  4. None of the above

Answer: Option A is correct.

Explanation: The structure date type stores the fields in separate locations.

Question 5: Which keyword is used to declare a structure data type?

  1. s
  2. structure
  3. struct
  4. None of the above

Answer: Option C is correct.

Explanation: struct is a keyword that is used to declare a structure.

Want more help with your computer science homework?

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

Data Types

User Defined DataType

User Defined DataType Homework Questions from Fellow Students

Browse our recently answered User Defined DataType homework questions.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

Data Types

User Defined DataType