0% found this document useful (0 votes)
22 views

Structure & Union

The document contains 7 questions and answers about C programming with structures, unions, and arrays of structures. It demonstrates how to define and use basic structures, arrays of structures, structures within structures, passing structures by value and reference to functions, simple unions, and unions within structures. For each question, it provides the code for a sample program and the expected output.

Uploaded by

Karan Srivastava
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Structure & Union

The document contains 7 questions and answers about C programming with structures, unions, and arrays of structures. It demonstrates how to define and use basic structures, arrays of structures, structures within structures, passing structures by value and reference to functions, simple unions, and unions within structures. For each question, it provides the code for a sample program and the expected output.

Uploaded by

Karan Srivastava
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Ques1)Writ a programme to show simple structure?

Ans) #include<stdio.h> #include<conio.h> struct student { int rno; char name[10]; }; void main() { struct student s={1,"\nmukesh"}; printf("%d",s.rno); printf("%s",s.name); getch(); }

Output:

Ques2)Write a programe to show array of structure?


Ans) #include<stdio.h> #include<conio.h> struct student { int rno; char name[10]; char course[5]; }; void main() { int i; struct student s[5]; for(i=0;i<5;i++) { printf("enter roll no"); scanf("%d",&s[i].rno); printf("enter name"); scanf("%s",s[i].name); printf("enter course"); scanf("%s",s[i].course); } for(i=0;i<5;i++)

{ printf("\n%d",s[i].rno); printf("\n%s",s[i].name); printf("\n%s",s[i].course); } getch(); clrscr(); }

Output:

Ques3)Write a programe to show stucture with in a structure?


Ans) #include<stdio.h> #include<conio.h> struct student { int rno; char name[10]; struct marksub { int s[3]; int total; }m; }s; void main() { int i; printf("enter rollno"); scanf("%d",&s.rno); printf("enter name"); scanf("%s",s.name); printf("enter three subject marks"); for(i=0;i<3;i++) { scanf("\n%d",&s.m.s[i]);

s.m.total+=s.m.s[i]; } printf("rollno=%d\n",s.rno); printf("name=%s\n",s.name); for(i=0;i<3;i++) { printf("\n%d",s.m.s[i]); printf(" } getch(); clrscr(); } %d",s.m.total);

Output:

Ques4)Write a programme to show structure passed as value to function? Ans) #include<stdio.h> #include<conio.h> struct sum { int a,b; }s; void addition(struct sum); void main() { printf("enter two numbers"); scanf("%d%d",&s.a,&s.b); addition(s); } void addition(struct sum s) { int total=0; total=s.a+s.b; printf("sum of two number is %d",total); getch(); clrscr(); }

Output:

Ques5)Write a programme to show structure passed as reference to function? Ans) #include<stdio.h> #include<conio.h> struct sum { int a,b,tot; }s,*p; void addition(struct sum *); void main() { printf("enter two numbers"); scanf("%d%d",&s.a,&s.b); p=&s; addition(p); } void addition(struct sum *p) { (*p).tot=0; (*p).tot=(*p).a+(*p).b; printf("sum of two number is %d",(*p).tot); getch(); clrscr();

Output:

Ques6)Write a programme to show simple union? Ans) #include<stdio.h> #include<conio.h> union student { int rno; char name[10]; }s; void main() { clrscr(); printf("enter roll no"); scanf("\n%d",&s.rno); printf("\n%d",s.rno); printf(" enter name");

scanf("\n%s",&s.name); printf("\n%s",s.name); getch();

Output:

Ques7)Write a prgramme to show union within a structure?


Ans) #include<stdio.h> #include<conio.h> struct student { int rno; char name[20]; union mark { int internal[3]; int external[3]; }m; int tot; }s; void main() { int i,tot=0; clrscr();

printf("enter rollno & name"); scanf("%d%s",&s.rno,s.name); printf("\nenter internal marks"); for(i=0;i<3;i++) { scanf("%d",&s.m.internal[i]); s.tot+=s.m.internal[i]; } printf("total marks =%d\nname=%s\nrolno=%d",s.tot,s.name,s.rno); for(i=0;i<3;i++) { printf("\n%d",s.m.internal[i]); printf("\n%d",s.tot); s.tot=0; printf("\nenter external marks"); for(i=0;i<3;i++) { scanf("%d",&s.m.external[i]); s.tot+=s.m.external[i]; } printf("rollno=%d\nname=%s\n",s.rno,s.name);

for(i=0;i<3;i++) { printf("\n%d",s.m.external[i]);

printf("\n%d",s.tot); } getch(); } getch(); clrscr(); }

Output:

You might also like