Home
Diploma Practice C Online People
Certificate Contact Us

Solutions for assignments on defining and using structure variables

as01.c

#include <stdio.h>

struct Time
{
  char hour;
  char min;
  char sec;
};

int main()
{
  struct Time tm;

  printf("Enter hours minutes and seconds with spaces in between\n");
  scanf("%hhd%hhd%hhd", &tm.hour, &tm.min, &tm.sec);
  printf("Entered Time is %02d:%02d:%02d\n", tm.hour, tm.min, tm.sec);
}

as02.c

#include <stdio.h>

struct Date 
{
  char  date;
  char  month;
  short year;
};

//Here we stored month names in two dimensional array with 12 rows and
// each row with 4 chars
char monthNames1[12][4] =
{
  "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

//Here we are defining 12 char pointer variables. Each pointer is initialized
//with the address of one string pointer
char *monthNames2[12] =
{
  "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
 
int main()
{
  struct Date dt;

  printf("Enter date month and year with spaces in between\n");
  scanf("%hhd%hhd%hd", &dt.date, &dt.month, &dt.year);

  //Printing date with all numbers
  printf("Entered Date is %02d/%02d/%04d\n", dt.date, dt.month, dt.year);

  //Printing date's month as string, method 1
  printf("Entered Date is %02d ", dt.date);
  switch(dt.month)
  {
    case 1:  
      printf("Jan "); 
      break;
    case 2:  
      printf("Feb "); 
      break;
    case 3:  
      printf("Mar "); break;
    case 4:  
      printf("Apr "); break;
    case 5:  printf("May "); break;
    case 6:  printf("Jun "); break;
    case 7:  printf("Jul "); break;
    case 8:  printf("Aug "); break;
    case 9:  printf("Sep "); break;
    case 10: printf("Oct "); break;
    case 11: printf("Nov "); break;
    case 12: printf("Dec "); break;
    default: printf("XXX "); break;
  }
  printf("%04d\n", dt.year);

  //Printing date's month as string; method 2
  printf("Entered Date is %02d %s %04d\n", dt.date, monthNames1[dt.month-1], 
                                           dt.year);
}

as03.c

#include <stdio.h>

struct Date 
{
  char  date;
  char  month;
  short year;
};

struct Student
{
  char        name[40];
  long long   mobile;
  struct Date dob;
};

int main()
{
  struct Student stu;

  printf("Enter name of student : ");
  gets(stu.name);
  printf("Enter mobile number   : ");
  scanf("%lld",&stu.mobile);
  printf("Enter date of birth with spaces in between : ");
  scanf("%hhd%hhd%hd", &stu.dob.date, &stu.dob.month, &stu.dob.year);

  printf("Name   : %s\n",  stu.name);
  printf("Mobile : %lld\n",stu.mobile);
  printf("DOB    : %02d/%02d/%04d\n",stu.dob.date, stu.dob.month,
                                     stu.dob.year);
}

as04.c

#include <stdio.h>

#define MAX_STUDENTS 2

struct Date 
{
  char  date;
  char  month;
  short year;
};

struct Student
{
  char        name[40];
  long long   mobile;
  struct Date dob;
};

int main()
{
  struct Student stus[MAX_STUDENTS];
  int ii;

  for(ii=0; ii<MAX_STUDENTS; ii++)
  {
    __fpurge(stdin);
    printf("Enter name of student %d : ", ii+1);
    gets(stus[ii].name);
    printf("Enter mobile number    : ");
    scanf("%lld",&stus[ii].mobile);
    printf("Enter date of birth with spaces in between : ");
    scanf("%hhd%hhd%hd", &stus[ii].dob.date, &stus[ii].dob.month, 
                         &stus[ii].dob.year);
  }
  printf("S.No  Name             Mobile     Date of Birth\n");
  printf("----  ---------------- ---------- -------------\n");
  for(ii=0; ii<MAX_STUDENTS; ii++)
  {
    printf("%4d  %-16s %lld %02d/%02d/%04d\n", ii,stus[ii].name,
            stus[ii].mobile, stus[ii].dob.date, stus[ii].dob.month, 
            stus[ii].dob.year);
  }
}



#401, Sai Sushma Homes, 23/A, S.R. Nagar Main Road, Hyderabad-500038, India.
Ph: +91-40-48508764, 7702028989, depik.help@gmail.com