User Defined Data Types
1. Which of the following statements are true.
1. A variable of type struct is automatically initialized to 0 by the system.
Ans: False. User has to explicitly initialize a struct.
2. A struct helps us to keep related data elements together in a single entity.
Ans: True.
3. struct is a native data type.
Ans: False. struct is a composite data type.
2. If the system is 8 byte aligned and size of int is 4, what will be the size of struct example_type in above example? Also work out the memory map for same.
struct example_type { int one; char two; int three; };
Ans: size of struct example_type will be 24 bytes. Each of the three members will be 8 byte aligned.
3. Which of the following statements are true.
1. Size of a struct is always greater than sum of size of its members.
Ans: False. It is greater than or equal to sum of size of its members.
2. A struct cannot have just one member.
Ans: False.
3. Size of a union is equal to size of largest element of the union.
Ans: True.
4. Which of the following will have a larger size.
struct trains_s { int train_numbers[100]; }; union trains_u { int train_numbers[100]; };
Ans: Both will be of the same size.
5. Identify which of these are valid typedef declarations.
1. typedef unsigned short USHORT;
Ans: Valid.
2. typedef int[100] INTARRAY;
Ans: Invalid. int[100] is not a type.
3. typedef int char;
Ans: Invalid. char is a keyword, it cannot be typedef identifier.
6. Trace control flow of above program for first two iterations.
01: #include <stdio.h> 02: struct student_info { 03: char name[32]; 04: char class[16]; 05: int english_marks; 06: int maths_marks; 07: int science_marks; 08: float percentage; 09: }; 10: typedef struct student_info student_info_t; 11: #define NUM_STUDENTS 50 12: int main (int argc, char *argv[]) { 13: student_info_t students[NUM_STUDENTS] = {0}; 14: for (int i = 0; i < NUM_STUDENTS; i++ ) { 15: printf("Enter name of student %d: ", i + 1); 16: scanf ("%s", &students[i].name); 17: printf("Enter class of student %d: ", i + 1); 18: scanf ("%s", &students[i].class); 19: printf("Enter Marks in English for student %d: ", i + 1); 20: scanf ("%d", &students[i].english_marks); 21: printf("Enter Marks in Maths for student %d: ", i + 1); 22: scanf ("%d", &students[i].maths_marks); 23: printf("Enter Marks in science for student %d: ", i + 1); 24: scanf ("%d", &students[i].science_marks); 25: students[i].percentage = (students[i].english_marks + students[i].maths_marks + students[i].science_marks ) / 3.0; 26: printf ("Percentage of Student %d : %f \n", i + 1, students[i].percentage); 27: } 28: return 0; 29: }
- Line 13 defines an array, students, of 50 elements of type student_info_t which is an alias for struct student_info.
- Line 14, control expression 0<50 evaluates to true. Control enters for loop.
- Line 15, message Enter name of student 1 is printed to the console.
- Line 16, a string (student’s name) is read into member name of students[0], i.e. 0th element of array of struct student_info.
- Line 17, message Enter class of student 1 is printed to the console.
- Line 18, a string (student’s class) is read into member class of students[0], i.e. 0th element of array of struct student_info.
- Line 19, message Enter marks in English for student 1 is printed to the console.
- Line 20, an int (student’s score in English) is read into member english_marks of students[0], i.e. 0th element of array of struct student_info.
- Line 21, message Enter marks in Maths for student 1 is printed to the console.
- Line 22, an int (student’s score in Maths) is read into member maths_marks of students[0], i.e. 0th element of array of struct student_info.
- Line 23, message Enter marks in Science for student 1 is printed to the console.
- Line 24, an int (student’s score in Science) is read into member science_marks of students[0], i.e. 0th element of array of struct student_info.
- Line 25, percentage score for students[0] is computed and stored in students[0].percentage.
- Line 26, message Percentage of student 1 : followed by computer percentage is printed to the console.
- End of for loop is reached. Tail statement is executed, value of i is incremented to 2.
- Control jumps to line 14, control expression 1<50 evaluates to true. Control enters for loop.
- Line 15, message Enter name of student 2 is printed to the console.
- Line 16, a string (student’s name) is read into member name of students[1], i.e. 1st element of array of struct student_info.
- Line 17, message Enter class of student 2 is printed to the console.
- Line 18, a string (student’s class) is read into member class of students[1], i.e. 1st element of array of struct student_info.
- Line 19, message Enter marks in English for student 2 is printed to the console.
- Line 20, an int (student’s score in English) is read into member english_marks of students[1], i.e. 1st element of array of struct student_info.
- Line 21, message Enter marks in Maths for student 2 is printed to the console.
- Line 22, an int (student’s score in Maths) is read into member maths_marks of students[1], i.e. 1st element of array of struct student_info.
- Line 23, message Enter marks in Science for student 2 is printed to the console.
- Line 24, an int (student’s score in Science) is read into member science_marks of students[1], i.e. 1st element of array of struct student_info.
- Line 25, percentage score for students[1] is computed and stored in students[1].percentage.
- Line 26, message Percentage of student 2 : followed by computer percentage is printed to the console.
7. Which of the following statements are true.
1. An enum value can be used without defining a variable of enum of type.
Ans: True.
2. typedef cannot be used with an enum to define new type alias.
Ans: False. An enum can be aliased to another type name using typedef.
3. Be default, first enum member is assigned value 1.
Ans: False. By default first member is assigned value 0.
4. An enum cannot be used as a function parameter.
Ans: False. An enum can be used as a function parameter.
8. Which of the following statements are true.
1. A union cannot have an enum
Ans: False. A union can have enum member.
2. A struct can have a union
Ans: True.
9. Write a program to
a. Define a struct point, that represents a point in 2D space with its x and y coordinates.
b. Define a struct square that stores 4 vertices of the square.
c. Allow the user to print perimeter and area of the square.
#include <stdio.h> #include <math.h> struct point { int x,y; }; struct square { struct point vertices[4]; }; double square_edge (struct square sq) { double x_distance = 0, y_distance = 0, edge = 0; x_distance = (sq.vertices[0].x - sq.vertices[1].x) * (sq.vertices[0].x - sq.vertices[1].x); y_distance = (sq.vertices[0].y - sq.vertices[1].y) * (sq.vertices[0].y - sq.vertices[1].y); edge = sqrt(x_distance + y_distance); return edge; } double square_perimeter (struct square sq) { return (4 * square_edge(sq)); } double square_area (struct square sq) { double edge = square_edge(sq); return (edge * edge); } int main (int argc, char *argv[]) { struct square sqr = { 0 }; printf("Enter vertices of square, clockwise\n"); for(int i =0; i<4; i++) { printf("Enter x coordinate of vertex %d: ",i+1); scanf("%d", &sqr.vertices[i].x); printf("Enter y coordinate of vertex %d: ",i+1); scanf("%d", &sqr.vertices[i].y); } printf("Perimeter of square is %f\n", square_perimeter(sqr)); printf("Area of square is %f\n", square_area(sqr)); return 0; }
10. Evolve above program to
a. Define enum of shapes – circle, rectangle, square.
b. Define a union to store details of appropriate shape: vertices for square and rectangle, centre and radius for circle.
c. Print the area and perimeter of the shape entered.
#include <stdio.h> #include <math.h> #define PI (22.0/7) struct point { int x,y; }; enum shape { _circle = 1, _rectangle, _square }; struct quadrilateral { struct point vertices[4]; }; struct circle { struct point center; unsigned int radius; }; struct shapes { enum shape this_shape; union { struct quadrilateral quad; struct circle cir; }; }; double compute_distance (struct point p1, struct point p2) { double x_distance = 0, y_distance = 0, distance = 0; x_distance = (p1.x - p2.x) * (p1.x - p2.x); y_distance = (p1.y - p2.y) * (p1.y - p2.y); distance = sqrt(x_distance + y_distance); return distance; } double square_perimeter (struct quadrilateral sq) { return (4 * compute_distance(sq.vertices[0], sq.vertices[1])); } double square_area (struct quadrilateral sq) { double edge = compute_distance(sq.vertices[0], sq.vertices[1]); return (edge * edge); } double rectangle_perimeter (struct quadrilateral rec) { double length = 0, breadth = 0; length = compute_distance(rec.vertices[0], rec.vertices[1]); breadth = compute_distance(rec.vertices[1], rec.vertices[2]); return (2 * (length + breadth)); } double rectangle_area (struct quadrilateral rec) { double length = 0, breadth = 0; length = compute_distance(rec.vertices[0], rec.vertices[1]); breadth = compute_distance(rec.vertices[1], rec.vertices[2]); return (length * breadth); } double circle_perimeter (struct circle cir) { return (2 * PI * cir.radius); } double circle_area (struct circle cir) { return (PI * cir.radius * cir.radius); } void input_circle(struct circle * cptr) { printf("Enter x-coordinate of center of circle: "); scanf("%d", &cptr->center.x); printf("Enter y-coordinate of center of circle: "); scanf("%d", &cptr->center.y); printf("Enter radius of circle: "); scanf("%u", &cptr->radius); } void input_quadrilateral(struct quadrilateral * qptr) { printf("Enter vertices of quadrilateral, clockwise\n"); for(int i =0; i<4; i++) { printf("Enter x coordinate of vertex %d: ",i+1); scanf("%d", &qptr->vertices[i].x); printf("Enter y coordinate of vertex %d: ",i+1); scanf("%d", &qptr->vertices[i].y); } } int main (int argc, char *argv[]) { struct shapes my_shape = { 0 }; int choice = -1; double area = 0, perimeter = 0; printf("What shape would you like to enter?\n1. Circle\n2. Rectangle\n3. Square \n[1-3]: "); scanf("%d", &choice); switch (choice) { case _circle: my_shape.this_shape = _circle; input_circle(&my_shape.cir); perimeter = circle_perimeter(my_shape.cir); area = circle_area(my_shape.cir); break; case _rectangle: my_shape.this_shape = _rectangle; input_quadrilateral(&my_shape.quad); perimeter = rectangle_perimeter(my_shape.quad); area = rectangle_area(my_shape.quad); break; case _square: my_shape.this_shape = _square; input_quadrilateral(&my_shape.quad); perimeter = square_perimeter(my_shape.quad); area = square_area(my_shape.quad); break; default: printf("Invalid input\n"); } printf("Perimeter of the shape is %f\n", perimeter); printf("Area of the shape is %f\n", area); return 0; }