Friday 23 September 2011

Structure

Structure is a collection of dissimilar data type element.
we can creat a structure using a "struct" key-word.
systax:
      struct SN
      {
      dt v1; 
      dt v2;
      dt v3;
      } SV;
or 
     struct SN
     {
     dt v1;
     dt v2;
     dt v3;
     };
    struct SN SV;
or 
   struct 
    {
   dt v1;
   dt v2;
   dt v3;
   }SV;
    
To Access- 
    SV.SN;
ex: 
   struct record 
   {
   char n[20];
   int age;
   char c[30];
   }e;
  
--->e.n,e.age,e.c;
Question 1: write a program to enter the name ,age and city of a employ and print them?
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
struct record
{
char n[20];
 int age;
char c[20];
}e;
clrscr();
printf("enter the name");
gets(e.n); // scanf("%s",&e.n);
printf("\n enter the age");
scanf("%d",&e.age);
printf("\n enter the city");
scanf("%s",&e.c);
printf("\n name=%s",e.n);
printf("\n Age=%d",e.age);
printf("\n city=%s",e.c);
getch();
}
  

   
  


       
  
 

Union

Union is a collection of dessimller data types elements.
we can creat a union using union key word.
syntax:
             union record 
             { 
              char n[15];
               int age;
               char c[10];
              }e;


Question 1:Write a program to enter the name,age,and city of a employ and print them?
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
union record
{
char n[20];
int age;
char c[30];
}e;
clrscr();
printf("enter the name");
gets(e.n);    // scanf("%s",&e.n);
printf("enter the age");
scanf("%d",&e.age);
printf("\nenter the city");
scanf("%s",&e.c);
printf("\n name=%s",e.n);
printf("\n age=%d",e.age);
printf("\ncity=%s",e.c);
getch();
}
Question 2:write a progarm to enter the name,age and city up two student and print them?
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
union record 
{
char n[15];
int age:
char c[15];
}
e1,e2;
printf("enter the name");
scanf("%s",&e1.n);
printf("enter the age");
scanf("%d",&e1.age);
printf("enter the city");
scanf("%s",&e1.c);
printf("enter the name of 2nd student");
scanf("%s",&e2.n);
printf("enter the age of second student");
scanf("%d",&e2.age);
printf("enter the city of second student");
scanf("%s",&e2.c);
printf("\n anme=%s",e1.n);
printf("\nage=%d",e1.age);
printf("\ncity=%s",e1.c);
printf("\nname=%s",e2.n);
printf("\nage=%d",e2.age);
printf("\ncity=%s",e2.c);
getch();
}