Structure and Union II-289
Program on Structure within Structure
9. Program to display information of an officer and his date of joining
#include <stdio.h>
#include <conio.h>
void main()
{
struct name
{
char first[7];
char second[7];
};
struct j_date
{
int day;
int month;
int year;
};
struct data
{
struct name nm;
struct j_date bt;
};
struct data r1={"Thomas","Rajesh",10,01,2005};
clrscr();
printf("Name : %s %s ",r1.nm.first,r1.nm.second);
printf("Joining Date : %d.%d.%d",r1.bt.day,r1.bt.month,r1.
bt.year);
getche();
}
OUTPUT:
Name : Thomas Rajesh
Joining Date : 10.1.2005
M13_ITL-ESL4791_02_SE_C13.indd 289 12/22/2012 5:06:16 PM
II-290 Programming Concepts
Programs on Pointer to Structure
10. Program to display numbers with pointers
#include <stdio.h>
#include <conio.h>
void main()
{
int *p;
struct num
{
int a;
int b;
int c;
};
struct num d;
d.a=2;
d.b=3;
d.c=4;
p=&d.a;
clrscr();
printf(" a=%d",*p);
printf(" b=%d",*(++p));
printf(" c=%d",*(++p));
}
OUTPUT:
a=2
b=3
c=4
11. Program to display time
#include <stdio.h>
#include <conio.h>
void main()
{
struct time
M13_ITL-ESL4791_02_SE_C13.indd 290 12/22/2012 5:06:16 PM
Structure and Union II-291
{
int second;
int minute;
int hour;
} ;
struct time *t;
t->second=50;
t->minute=20;
t->hour=12;
clrscr();
printf("%d:%d:%d",t->hour,t->minute,t->second);
}
OUTPUT:
12 : 20 : 50
Program on Union and Structure
12. Program to display numbers with structure and union
#include <stdio.h>
#include <conio.h>
void main()
{
struct x
{
float f;
char p[2];
};
union z
{
struct x set;
};
union z st;
st.set.f=5.9;
st.set.p[0]=67;
st.set.p[1]=69;
clrscr();
printf(" %g",st.set.f);
printf(" %c",st.set.p[0]);
M13_ITL-ESL4791_02_SE_C13.indd 291 12/22/2012 5:06:16 PM
II-292 Programming Concepts
printf(" %c",st.set.p[1]);
}
OUTPUT:
5.9
C
E
Programs on Typedef
12.
#include <stdio.h>
#include <conio.h>
# define H 60
void main()
{
typedef int hours;
hours hrs=2;
clrscr();
printf(" Minutes : %d",hrs*H);
printf(" Seconds : %d",hrs*H*H);
}
Minutes : 120
Seconds : 7200
13.
#include <stdio.h>
#include <conio.h>
void main()
{ typedef char string [20];
string a="Hello";
clrscr();
printf(" %s",a);}
}
OUTPUT:
Hello
M13_ITL-ESL4791_02_SE_C13.indd 292 12/22/2012 5:06:16 PM
Structure and Union II-293
14. Program to display information of an employee
#include <stdio.h>
#include <conio.h>
void main()
{
typedef struct
{
char name[20];
char sex[2];
int acno;
} info;
info empo={"Sanjay","M",1452};
clrscr();
printf(" %s %s %d", empo.name, empo.sex, empo.acno);
}
OUTPUT:
Sanjay M 1452
Programs on Enumerated Data Type
15. Program to display the values of some months with enum data
#include <stdio.h>
#include <conio.h>
void main()
{
enum {Jan,Feb,March,Apr,May};
clrscr();
printf("Jan=%d",Jan);
printf(" Feb=%d",Feb);
}
OUTPUT:
Jan=0 Feb=1
M13_ITL-ESL4791_02_SE_C13.indd 293 12/22/2012 5:06:16 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset