User Defined data Types
5/1/2006 Computer Programming TA 103 BE I year 2
typedef datatype user_defined_data_type
typedef int salary;
salary emp1,emp2;
salary x;
typedef char sentence[50];
sentence header,footer;
char header[50],footer[50];
5/1/2006 Computer Programming TA 103 BE I year 3
enumerated data types
An enumerated data type stores specific valuesenum product{pen,pencil,eraser,paper};members of an enumerated data types are constants.
e.g. pen,pencil are constants.members are unique and cannot be duplicate.enum product p1;p1=pen;members of an input variable cannot be used with an
input function like scanf;Members are assigned integer values by the compiler.
By default the first member gets the value 0, second get 1 and so on.
5/1/2006 Computer Programming TA 103 BE I year 4
overriding default values
enum product{pen=1,pencil,eraser,paper}
5/1/2006 Computer Programming TA 103 BE I year 5
There is no data type in C that allows you to store different types in one unit.However, you can store all this information as one unit in a structure and use the structure to write records in a file.
5/1/2006 Computer Programming TA 103 BE I year 6
The variables contained in a structure are called its members. The data types of these members can be different. Structures can contain arrays and pointers.
5/1/2006 Computer Programming TA 103 BE I year 7
Member names with in a structure are unique. However, different structures can have the members with the same name. There is no limit on number of members a structure can contain.
5/1/2006 Computer Programming TA 103 BE I year 8
#include<stdio.h>struct date{int day;char month[15];int year;};main (){}
5/1/2006 Computer Programming TA 103 BE I year 9
The declaration of a structure does not reserve any bytes in memory for the structure. The declaration only indicates the variables that can be stored in a structure.
5/1/2006 Computer Programming TA 103 BE I year 10
#include<stdio.h>struct date{int day;char month[15];int year;};main (){struct date dat1,dat2;}
5/1/2006 Computer Programming TA 103 BE I year 11
#include<stdio.h>struct date{int day;char month[15];int year;} dat1,dat2;main (){
}
5/1/2006 Computer Programming TA 103 BE I year 12
#include<stdio.h>
struct
{
int day;
char month[15];
int year;
} dat1,dat2;
main ()
{
}
5/1/2006 Computer Programming TA 103 BE I year 13
#include<stdio.h>typedef struct {int day;char month[15];int year;} date;main (){date day1,day2;}
5/1/2006 Computer Programming TA 103 BE I year 14
structure variables can be accessed by using dot operator.
e.g. dat1.day=6;
or printf(“%d”, dat2.year);
5/1/2006 Computer Programming TA 103 BE I year 15
When a structure is passed as an argument to a function , structure is defined before a function. Entire structure or a member of a structure can b passed to a function as an argument.
5/1/2006 Computer Programming TA 103 BE I year 16
#include<stdio.h>struct date{int month;int day;int year;};void PrintDate(struct date) ;main (){struct date today = {4,18,2006};PrintDate(today);}void PrintDate(struct date dt){printf(“Month = %d“, dt.month);printf(“Day = %d“, dt.day);printf(“Year = %d“, dt.year);
}
5/1/2006 Computer Programming TA 103 BE I year 17
#include<stdio.h>struct BankAccount{int ActNo;char *name;int balance;};struct BankAccount adjust(struct BankAccount);main(){struct BankAccount customer = { 333, “sham”, 3000};printf(“%d %s %d \n”, customer.ActNo, customer.name, customer.balance);
customer=adjust(customer);printf(“%d %s %d \n”, customer.ActNo, customer.name, customer.balance);
}struct BankAccount adjust(struct BankAccount cust){cust.ActNo=645;cust.name=“ram”;
cust.balance=4000;return cust;}
5/1/2006 Computer Programming TA 103 BE I year 18