Explaining user defined data types in c programming | Allinonetuts

Explaining user-defined data types with Example

User-Defined Data Types:

Read This: Fundamental data types and sizes used in C Programming

C also provides users a feature to definite their own data types which can later be used to declare variables. This feature is known as “type definition,” that allows a user to define an identifier that would represent an existing data type. Generally, it takes the form:

typedef type identifier;

Where type refers to an existing data type, and ‘identifier’ refers to the new name given by the user to the data type. Some examples of type definition are:

typedef int marks;

typedefs float price;

They can later be used to definite variables as marks subject 1, subject 2; price item 1, item2;

Another user-defined data type is enumerated data type provided by ANSI standard. It is defined as follows:

enum identifier { value1  value 2, value n };

The identifier here is a user-defined enumerated data type which can be used to declare variables that can have one of the values enclosed within braces.

For Example:

enum day {Sunday, Monday, Tuesday, ..Saturday } enum day week_day 1 ,week_day 2;

Here the enumerated variables week day 1, week_day 2 can take one of the values Sunday, Monday,….Saturday. Each enumerated constant (sometimes called an enumerator) has an integer value. Unless specified, the first constant has a value of zero. The value increase by one for each additional constant in the enumeration. The values of each constant can also be specified.

A sample program:

#include<stdio.h>
enum days {Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
int main()
{
enum days today = Monday;
if ((today == Saturday) || (today == Sunday))
{
printf (“'Weekend\n”);
}
else
{
print(“Go to work or school\n”);
}
return 0;
}
<

From the sample program given above, Monday equals 1, Tuesday equals 2, and Wednesday equals 3 and so forth.

Hello Viewers,
welcome to the Allinonetuts a Technical tutorial and Programming Blog Maintained by Vikas Kardam, a Web Developer and Blogger From Delhi, India.
Explaining user-defined data types with Example was last modified: June 29th, 2017 by Vikas Kardam
Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

Average rating:  
 1 reviews

Easiest way to explain about user-defined data types with Example

Leave a Reply

Your email address will not be published. Required fields are marked *