![]() |
Data Types are the types of data that can be stored in memory using a programming language. Basically, data types are used to indicate the type of data that a variable can store. These data types require different amounts of memory and there are particular operations that can be performed on them. These data types can be broadly classified into three types:
In this article, will discuss the User-Defined Data Type. What is a user-defined Datatype in C?The data types defined by the user themself are referred to as user-defined data types. These data types are derived from the existing datatypes. Need of User-Defined Datatypes
Types of User-Defined DataTypesThere are 4 types of user-defined data types in C. They are
1. StructureAs we know, C doesn’t have built-in object-oriented features like C++ but structures can be used to achieve encapsulation to some level. Structures are used to group items of different types into a single type. The “struct” keyword is used to define a structure. The size of the structure is equal to or greater than the total size of all of its members. Syntaxstruct structure_name { data_type member_name1; data_type member_name1; .... .... }; ExampleC
Output
Name: GeeksforGeeks Age: 30 2. UnionUnions are similar to structures in many ways. What makes a union different is that all the members in the union are stored in the same memory location resulting in only one member containing data at the same time. The size of the union is the size of its largest member. Union is declared using the “union” keyword. Syntaxunion union_name { datatype member1; datatype member2; ... }; ExampleC
Output
Data.i: 10 Data.f: 3.140000 Data.str: GeeksforGeeks, C 2. Enumeration (enums)Enum is short for “Enumeration”. It allows the user to create custom data types with a set of named integer constants. The “enum” keyword is used to declare an enumeration. Enum simplifies and makes the program more readable. Syntaxenum enum_name {const1, const2, ..., constN}; Here, the const1 will be assigned 0, const2 = 1, and so on in the sequence. We can also assign a custom integer value such as: enum enum_name { const1 = 8; const2 = 4; } ExampleC
Output
Today is 4 Typedeftypedef is used to redefine the existing data type names. Basically, it is used to provide new names to the existing data types. The “typedef” keyword is used for this purpose; Syntaxtypedef existing_name alias_name; ExampleC
Output
Best Tutorials on: GeeksforGeeks |
Reffered: https://www.geeksforgeeks.org
C++ |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |