Sei sulla pagina 1di 4

C Programming : Union and Struct Assignment 1.

Write a program that rotates values of x, y and z such that x=y, y=z and z=y Soln: #include <stdio.h> int swap (int *a, int *b, int *c); main() { int x, y, z; printf("Enter the numbers \n "); scanf("%d %d %d", &x, &y, &z); swap(&x,&y,&z); printf("\n x = %d \n y = %d\n z = %d\n", x, y, z); return 0; } int swap (int *x, int *y, int *z){ int temp; temp=*x; *x=*y; *z=*y; *y=temp; } Write a program that accepts an input and displays a decremented value of the integer y one !"g" for an integer =#$%&', #$%&, #$%, #$, # Soln: #include <stdio.h> main() { int num; printf("Enter the number to be con erte!\n "); //Inquire for user input scanf("%d",&num); printf("\n%d\n",num); //print first output do{ num"=#0; //divide it by 1 and store in the same variable if(num$0) //chec! if its belo" #ero and print if its not printf("%d\n"); }"hile(num>0); // do this until you $et a number belo" #ero return 0; } %" Structures User defined data type which allows users to store data of different data type in a location.

2.

Syntax:( struct tag)name *data type#, data type$++"",.eclaration 2ethod #: struct boo%s{ int pa&es; char 'uthor()0*; +,oat ersion; char pub,isher()0*; }; struct boo%s -ron.an; 2ethod $: struct boo%s{ int pa&es; char 'uthor()0*; +,oat ersion; char pub,isher()0*; } /iaries, /racu,a; 2ethod %: struct { int pa&es; char 'uthor()0*; +,oat ersion; char pub,isher()0*; } boo%s, no e,s Example(programs) 1. #include <stdio.h> struct ,ibrary{ char tit,e(80*; int pa&es; float cost; }; int main() { struct ,ibrary boo%#; //%eclaration //'ssi&n a,ues strcpy(boo%#0tit,e,"9ampires /iaries"); boo%#0pa&es = 100; boo%#0cost = 100:1; //%isplay of the values printf(";it,e\t\t< %s\n",boo%#0tit,e); /nitialization 0.efine, declare, and Assign1 2ethod #: struct boo%s{ int pa&es; char 'uthor()0*; +,oat cost; }; struct boo%s /racu,a; /racu,a0pa&es=100; strcpy(/racu,a0'uthor ,"2ram 3to%er"); /racu,a0cost= 4000000; 2ethod $: struct boo%s{ int pa&es; char 'uthor()0*; +,oat cost; }/racu,a ={100,"2ram 3to%er", 2000.00}, /airies={500,"6isa 7ane 3mith", 8000000}; 2ethod %: struct boo%s{ int pa&es; char 'uthor()0*; +,oat cost; }; struct /racu,a ={100,"2ram 3to%er", 4000000}

printf("=umber o+ pa&es\t< %d\n",boo%#0pa&es); printf(";ota, >ost(?)\t< %.&f\n",boo%#0cost); return 0; 2. } #include <stdio.h> struct !ata{ int x,y; //%efinin$' declare and (ssi$n }+irst={:,4}; struct !ata swap(struct !ata one); // %eclarin$ )unction s"ap*+ void main() { struct !ata secon!; printf("2e+ore< %d %d\n",+irst0x,+irst0y); secon! = swap(+irst); //,allin$ the function s"ap*+ printf("'+ter< %d %d\n", secon!0x,secon!0y); } struct !ata swap(struct !ata one) //%efinin$ the function s"ap*+ { int temp; temp=one0x; one0x=one0y; one0y=temp; //-"ap (l$orithm return(one); } %" #include <stdio.h> struct mo ie{ char hero(10*; char i,,ian(10*; }' en&ers,;hor,-ronman; void main() { struct mo ie ' en&ers,;hor; //%efine' declare and assi$n

printf("\n-n ' en&ers,\n.ho was the i,,ain\t< "); scanf("%s",@' en&ers0 i,,ian); printf("\n-n ;hor,\nAho was the hero\t< "); scanf("\n%s",&;hor0hero);

///ser input ///ser input

printf("\n' en&ers ha! %s i,,ains",' en&ers0 i,,ian); printf("\n;hor is an epic mo ie with %s as hero\n",;hor0hero); }

%" Union

This is a user defined data type (similar to structures) that can only assign values to only one mem er of the union class. !llocates the limited space to the largest of the mem ers !"g" union digits * int xfloat ychar z, decimal//Similar in declaration, definition and assignment as struct Here the float member will be allocated priority space hence only one member can be use it at a time. Example #include <stdio.h> #include <strin$.h> union /ata { int i; float +; char str(40*; }; int main( ) { //0rint one at a time because of limited space union /ata !ata; !ata0i = #0; printf( "!ata0i < %d\n", !ata0i); !ata0+ = 44001; printf( "!ata0+ < %f\n", !ata0+); strcpy( !ata0str, "Betroz is 'wesome"); printf( "!ata0str < Cs\n", !ata0str); return 0; }

Potrebbero piacerti anche