Wednesday, August 6, 2014

c assignment 5

↓↑ Program to show operations on structure like find offset,access using arrow operator. 
#include<stdio.h>
#include<stddef.h>
struct student
{
 int rollno;
 char name[10];
 int marks;
}s1;

int main()
{
        printf("Enter rollno name marks of student:");   // i/p  o/p operation
 scanf("%d%s%d",&s1.rollno,s1.name,&s1.marks);
 
 printf("rollno=%d\nname=%s\nmarks=%d\n\n",s1.rollno,s1.name,s1.marks);

 struct student s2={3,"nikhil",70};                                       //initialization of variable.............
 printf("rollno=%d\nname=%s\nmarks=%d\n\n",s2.rollno,s2.name,s2.marks);
 
 struct student *p=&s2;
        printf("rollno=%d\nname=%s\nmarks=%d\n\n",p->rollno,p->name,p->marks);

 printf("size of s1=%ld\nsize of *p=%ld\n",sizeof(s1),sizeof(p));
        printf("rollno offset=%ld name offset=%ld \nmarks offset=%ld\n",offsetof(struct student,rollno),offsetof(struct student,name),offsetof(struct student,marks));
 
 typedef struct student sinfo;                                            // alias for structure
 sinfo s3={3,"gaurav",60};                  
 printf("rollno=%d\nname=%s\nmarks=%d\n\n",s2.rollno,s2.name,s2.marks);
 
 typedef struct student *pstruct;                                        // alias for structure pointer
 pstruct p2=&s3;
 printf("rollno=%d\nname=%s\nmarks=%d\n\n",p2->rollno,p2->name,p2->marks);
return 0;
}

↓↑ Program to demonstrate efficient way of structure declaration(different behaviour on 64 bit machine there word size is 64bit).
#include<stdio.h>
#include<stddef.h>

struct A 
{
    int x;
    char c1;
    double d;
    float f;
    char c2;
};

struct B {
    char c1;
    char c2;
    int x;
    float f;
    double d;
};


int main()
{
    struct A a;
    struct B b;
    printf("befor optimization size of struct A=%d\nafter optimization size of struct B=%d\n",sizeof(a),sizeof(b));
    printf("offset x=%d,c1=%d,d=%d,f=%d,c2=%d\n",offsetof(struct A,x),offsetof(struct A,c1),offsetof(struct A,d),offsetof(struct A,f),offsetof(struct A,c2));
    printf("After\n\n");
    printf("offset x=%d,c1=%d,d=%d,f=%d,c2=%d\n",offsetof(struct B,x),offsetof(struct B,c1),offsetof(struct B,d),offsetof(struct B,f),offsetof(struct B,c2));

return 0;
}
//op on 32 bit 
//size of struct A = 20 bytes
//size of struct B = 16 bytes(place small data type first for better memory utilisation)
↓↑ Create a Box structure with the members length,breadth,height.Pass the structure variable to a function to calculate volume by value, by reference
#include<stdio.h>
struct box
{
    int length;
    int breadth;
    int width;
};

int vol(struct box);
int revol(struct box *);

int main()
{
    struct box a={10,20,30};
    int x,y;
    x=vol(a);
    printf("volume using call by function:%d\n",x);
    y=revol(&a);
    printf("volume using call by reference:%d\n",y);

return 0;
}

int vol(struct box b1)
{
    return (b1.length*b1.breadth*b1.width);
}

int revol(struct box *b2)
{
    return (b2->length*b2->breadth*b2->width);
}
↓↑ create student structure and modify marks member as an array(array of 5 subjects), create array of struct variables and do some input,output operations. (Marks of i th student in j th subject etc)
#include<stdio.h>
#include<stddef.h>
struct stud
{
    int rollno;
    char name[20];
    double marks[5];
};
void display(struct stud *,int );
void insert(struct stud *);

int records=0;
int main()
{
    int ch,rol,i;
    struct stud s[5];
    do
    {
        printf("\nEnter choice 1.Insert 2.Show 3.Exit");
        scanf(" %d",&ch);
 switch(ch)
 {
  case 1:
   insert(&s[records]);
   break;
  case 2:
   printf("\nEnter Roll No of student");
   scanf("%d",&rol);
   for(i=0;i<records;i++)
   {
    if(s[i].rollno==rol)
    display(&s[i],i);
    else printf("\nNo such student");
   }
  case 3:
   break;
  default:
   printf("\nWrong Choice...");     
                        break;
        }
    }while(ch!=3);
return 0;
}
void insert(struct stud *ptr)
{ 
    if(records<5)
    {
        int i;
        printf("\nEnter Roll No");
        scanf("%d",&(ptr[records].rollno));
        printf("\nEnter Name \n");
        scanf(" %s",ptr[records].name);
        for(i=0;i<5;i++)
        {
            printf("\nEnter Marks of Subject %d\n",i+1);
            scanf(" %lf",&ptr[records].marks[i]);
        }
    records++;
    }
}
void display(struct stud *ptr,int i)
{
    int j;
    printf("\nName=%s",ptr[i].name);
    printf("\nRoll No=%d",ptr[i].rollno);
    printf("\nEnter which subjects marks do you want\n");
    scanf("%d",&j);
    printf("\nMarks of Subject %d are \t%lf",j,ptr[i].marks[j-1]);
}
↓↑ Create program that return structure variable and 1 better approach
#include<stdio.h>
struct stud
{
    int a,b;
};
struct stud return1(struct stud);
void return2(struct stud *);

int main()
{
    struct stud s1={12,13};
    struct stud s2,s4;
    s2=return1(s1);
    printf("val of a=%d,b=%d\n",s2.a,s2.b);            //a=22,b=23

    return2(&s1);

    printf("better approch no need to return structure\na=%d\tb=%d\n",s1.a,s1.b);// a=33,b=34
return 0;
}

struct stud return1(struct stud a1)
{
    a1.a=22;
    a1.b=23;
return a1;
}

void return2(struct stud *s3)
{
    s3->a=33;
    s3->b=34;
}
↓↑ Anonymous structure variable with and without typedef.
#include <stdio.h>

struct
{
    int a;
    char name[10];
}b1;

struct
{
    int rollno;
}typedef s1;

int main()
{ 
    s1 a1;
    a1.rollno=34;
    b1.a=10;
    printf("%d %d\n",b1.a,a1.rollno);   
return 0;
}

↓↑ Nested Structure Example.
#include<stdio.h>
struct A
{
    int add;
    double d;
    struct B{
        int age;
 char name[10];
    }b1;
}a1;

int main()
{
    a1.add=10;
    a1.b1.age=20;
    printf("inner structure age=%d,outer structure add=%d\n",a1.b1.age,a1.add);
return 0;
}
↓↑ Structure Bitfield Example.
#include<stdio.h>
struct bit
{
    unsigned int a:4;
    //float b:6;
    unsigned int b:5;
}b1;

int main()
{
    b1.a=31;
    b1.b=31;
    //b1.b=2.23;
    printf("size of b1=%ld\ta=%d\tb=%d\n",sizeof(b1),b1.a,b1.b);
return 0;
}
↓↑ Program to convert ip address to unsigned int.
#include<stdio.h>
#include<math.h>
union u
{

char ch[4];

 unsigned int i;
};
int main()
{
 int a,b,c,d,num;
 union u u1;
 printf("\nEnter Ip Address(enter each part without . or : )...");
 scanf("%d%d%d%d",&a,&b,&c,&d);
 u1.ch[0]=d;
 u1.ch[1]=c;
 u1.ch[2]=b;
 u1.ch[3]=a;
 printf("\n32Bit Integer is %u",u1.i);
 return 0;
}
//(first octet * 256³) + (second octet * 256²) + (third octet * 256) + (fourth octet)
↓↑ Union example.
#include<stdio.h>
#include<stddef.h>
union A
{
 int x;
 int y;
 char ch;
};

union A a1;


int main()
{
 a1.x=0x10;
 a1.y=0x1121;
 a1.ch='A';
 printf("x=%d y=%d ch=%d\n",a1.x,a1.y,a1.ch);
 
 printf("size of union A=%ld\noffset of x=%ld\noffsetof y=%ld\noffset of ch=%ld\n",sizeof(a1),offsetof(union A,x),offsetof(union A,y),offsetof(union A,ch));

return 0;
}
↓↑ Union Inside Structure.
#include <stdio.h>
/*struct student
{
 int rollno;
 char name;
 union p{
  int age1;
  int age2;
  }p1;
}s1;*/
union student
{
 int rollno;
 char name;
 struct p{
  int age1;
  int age2;
  }p1;
}s1;
int main()
{
       /* s1.p1.age1=20;
 s1.p1.age2=22;
        s1.rollno=34;
        s1.name='A';*/

 s1.rollno=1023;
        s1.name='b';
 s1.p1.age1=20;
 s1.p1.age2=22; 
 //printf("rollno=%d name=%c\n",s1.rollno,s1.name);     
 printf("age1=%d age2=%d rollno=%d name=%c\n",s1.p1.age1,s1.p1.age2,s1.rollno,s1.name);
return 0;
}

No comments:

Post a Comment