Program to illustrate typedef in C

#include <stdio.h> #include <string.h>   typedef struct { char title[20]; char publisher[25]; char author[20]; int no_of_pages; float cost; } books;   int main() { books book1;   strcpy(book1.title,"C tutorials"); strcpy(book1.publisher,"sdfsdf"); strcpy(book1.author,"xxxxhjh"); book1.no_of_pages=281; book1.cost=72.00; printf("Book Title: %sn",book1.title); printf("Book Publisher: %sn",book1.publisher); printf("Book Author: %sn",book1.author); printf("No. of Pages: %dn",book1.no_of_pages); printf("Cost: %fn",book1.cost); return 0; }
Continue reading…

Enjoyed this post? Share it!

 
 

Program to demonstrate usage of unions in C

#include <stdio.h>   union item { char cvalue; int ivalue; float fvalue; double dvalue; }; int main() { union item value;   value.cvalue=’C’; printf("cvalue=%c, address=%un",value.cvalue,&value.cvalue);   value.ivalue=58; printf("ivalue=%d, address=%un",value.ivalue,&value.ivalue);   value.fvalue=79.0; printf("fvalue=%f, address=%un",value.fvalue,&value.fvalue);   value.dvalue=3849.273; printf("dvalue=%g, address=%un",value.dvalue,&value.dvalue);   printf("nNew Values…n");   printf("cvalue=%c, address=%un",value.cvalue,&value.cvalue); printf("ivalue=%d, address=%un",value.ivalue,&value.ivalue); printf("fvalue=%f, address=%un",value.fvalue,&value.fvalue); printf("dvalue=%g, address=%un",value.dvalue,&value.dvalue);   return 0; }
Continue reading…

Enjoyed this post? Share it!

 
 

Program to demonstrate arrays of structures in C

#include <stdio.h> # define limit 20 struct pair{ int first; int second; float average; };   void findavg(struct pair *,int);   int main() { struct pair triplet[limit]; int i;   printf("Enter the integer pairs (a,b):n"); for(i=0;i<limit;i++) scanf("%d,%d",&triplet[i].first,&triplet[i].second);   findavg(triplet,limit);   for(i=0;i<limit;i++) printf("%d,%d – %fn",(triplet+i)->first, (triplet+i)->second,(triplet+i)->average); return 0; }   void findavg(struct pair *ptr,int lim) { […]
Continue reading…

Enjoyed this post? Share it!

 
 

Create a structure and access it using pointers to structures in C

#include <stdio.h>   struct point{ float abscissa; float ordinate; }; struct point mid(struct point,struct point);   int main() { struct point p1,p2,pm;   printf("Enter coordinates of two points:n"); printf("tFor the first point (x1,y1) : "); scanf("%f,%f",&p1.abscissa,&p1.ordinate); fflush(stdin); printf("tFor the second point (x2,y2) : "); scanf("%f,%f",&p2.abscissa,&p2.ordinate); pm=mid(p1,p2); printf("The middle point is : (%f,%f).n",pm.abscissa,pm.ordinate); return 0; } […]
Continue reading…

Enjoyed this post? Share it!

 
 

Illustrate structure member passing to and from functions in C

#include <string.h>   struct books{ char title[20]; char publisher[25]; char author[20]; int no_of_pages; float cost; };   float modify_cost(float);   int main() { struct books book1,*pb=&book1;   strcpy(pb->title,"C tuts"); strcpy((*pb).publisher,"xxxxxx"); strcpy(pb->author,"xxxxxxx"); pb->no_of_pages=281; pb->cost=72.00; printf("Book Title: %sn",pb->title); printf("Book Publisher: %sn",pb->publisher); printf("Book Author: %sn",pb->author); printf("No. of Pages: %dn",pb->no_of_pages); printf("Cost: %fn",pb->cost); pb->cost=modify_cost(pb->cost); printf("New Cost: %fn",pb->cost); return 0; } […]
Continue reading…

Enjoyed this post? Share it!

 
 

Create a structure and access it using pointers to structures in C

#include <stdio.h> #include <string.h>   struct books{ char title[20]; char publisher[25]; char author[20]; int no_of_pages; float cost; }; int main() { struct books book1,*pb=&book1;   strcpy(pb->title,"C tutorials"); strcpy((*pb).publisher,"xxxx"); strcpy(pb->author,"xxxx"); pb->no_of_pages=281; pb->cost=72.00;   printf("Book Title: %sn",pb->title); printf("Book Publisher: %sn",pb->publisher); printf("Book Author: %sn",pb->author); printf("No. of Pages: %dn",pb->no_of_pages); printf("Cost: %fn",pb->cost);   return 0; }
Continue reading…

Enjoyed this post? Share it!

 
 

Program to create a structure and access it in C

  #include <stdio.h> #include <string.h>   struct books{ char title[20]; char publisher[25]; char author[20]; int no_of_pages; float cost; };   int main() { struct books book1;   strcpy(book1.title, "C tutorials"); strcpy(book1.publisher, "some publisher"); strcpy(book1.author, "some author"); book1.no_of_pages=211; book1.cost=71.00;   printf("Book Title: %sn",book1.title); printf("Book Publisher: %sn",book1.publisher); printf("Book Author: %sn",book1.author); printf("No. of Pages: %dn",book1.no_of_pages); printf("Cost: %fn",book1.cost);   […]
Continue reading…

Enjoyed this post? Share it!