KEMBAR78
Exercise of Struct and Union | PDF | Computing | Computer Science
0% found this document useful (0 votes)
92 views10 pages

Exercise of Struct and Union

The document contains examples of using structs and unions in C code. It demonstrates defining structs to store related data like student records with personal details, addresses, and dates of birth. It also shows initializing struct variables, accepting user input to populate struct fields, and printing stored struct data. The examples also demonstrate defining a union to represent an integer as individual bytes, and accessing those bytes to demonstrate the shared memory aspect of unions.

Uploaded by

Dio Damar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views10 pages

Exercise of Struct and Union

The document contains examples of using structs and unions in C code. It demonstrates defining structs to store related data like student records with personal details, addresses, and dates of birth. It also shows initializing struct variables, accepting user input to populate struct fields, and printing stored struct data. The examples also demonstrate defining a union to represent an integer as individual bytes, and accessing those bytes to demonstrate the shared memory aspect of unions.

Uploaded by

Dio Damar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Exercise of Struct and Union

1. Code:
#include <stdio.h>

const int length = 5;

struct dob{
int date;
char month[800];
int year;
};

struct address{
char streetname[500];
char number[500];
char city[500];
char province[500];
};

struct student{
char studentnumber[3000];
char studentname[3000];
struct address studentaddress[length];
struct dob studentdateofbirth[length];
};

int main(){
int i = 0;
struct student students[length];
struct address studentshome[length];
struct dob studentsbirthday[length];
for(i = 0; i < length; i++){
printf("Input student number for element #%d: ", i+1);
scanf("%[^\n]", &students[i].studentnumber);
fflush(stdin);
printf("Input student name for element #%d: ", i+1);
scanf("%[^\n]", &students[i].studentname);
fflush(stdin);
printf("Input student\'s home\'s street name for address in element #%d: ",
i+1);
scanf("%[^\n]", &studentshome[i].streetname);
fflush(stdin);
printf("Input student\'s home\'s number for address in element #%d: ",
i+1);
scanf("%[^\n]", &studentshome[i].number);
fflush(stdin);
printf("Input student\'s hometown for address in element #%d: ", i+1);
scanf("%[^\n]", &studentshome[i].city);
fflush(stdin);
printf("Input student\'s domain province for address in element #%d: ",
i+1);
scanf("%[^\n]", &studentshome[i].province);
fflush(stdin);
printf("Input student\'s date of birth for element #%d: ", i+1);
scanf("%d", &studentsbirthday[i].date);
fflush(stdin);
printf("Input student\'s month of birth for element #%d: ", i+1);
scanf("%[^\n]", &studentsbirthday[i].month);
fflush(stdin);
printf("Input student\'s year of birth for element #%d: ", i+1);
scanf("%[^\n]", &studentsbirthday[i].year);
fflush(stdin);
}
for(i = 0; i < length; i++){
printf("\n");
printf("Data %d: ", i+1);
printf("\n");
printf("Student number: %s ", students[i].studentnumber);
printf("\n");
printf("Student name: %s ", students[i].studentname);
printf("\n");
printf("Student\'s home\'s street name: %s ",
studentshome[i].streetname);
printf("\n");
printf("Student\'s home\'s number: %s ", studentshome[i].number);
printf("\n");
printf("Student\'s hometown: %s ", studentshome[i].city);
printf("\n");
printf("Student\'s domain province: %s ",
studentshome[i].province);
printf("\n");
printf("Student\'s date of birth: %s ", studentsbirthday[i].date);
printf("\n");
printf("Student\'s month of birth: %s ", studentsbirthday[i].month);
printf("\n");
printf("Student\'s year of birth: %s ", studentsbirthday[i].year);
printf("\n");
}
return 0;
}
2. (Same Code)
3. Code:
#include <stdio.h>

struct automobile {
int year;
char model[8];
int engine_power;
float weight;
};

int main(void){
int i, length = 5;
struct automobile cars[length];
for(i = 0; i < length; i++){
printf("Insert Car year: ");
scanf("%d", &cars[i].year);
fflush(stdin);
printf("Insert Car Model: ");
scanf("%[^\n]", &cars[i].model);
fflush(stdin);
printf("Insert Car Engine Power: ");
scanf("%d", &cars[i].engine_power);
fflush(stdin);
printf("Insert Car Weight: ");
scanf("%f", &cars[i].weight);
fflush(stdin);
printf("\n");
}

for(i = 0; i < length; i++){


printf("\nCar no. %d: \n", i + 1);
printf("Car year: %d \n", cars[i].year);
printf("Car Model: %s \n", cars[i].model);
printf("Car Engine Power: %d \n", cars[i].engine_power);
printf("Car Weight: %1.2f \n", cars[i].weight);
}

return 1;
}

4. Code:
#include <stdio.h>

struct ipkmhs{
char nim[11];
char name[30];
float gpa;
};

int main(void){
int length = 5, i, j;
struct ipkmhs ipk[length];
for(i = 0; i < length; i++){
printf("Input NIM of Student no. %d: ", i + 1);
scanf("%[^\n]", &ipk[i].nim);
fflush(stdin);
printf("Input Name of Student no. %d: ", i + 1);
scanf("%[^\n]", &ipk[i].name);
fflush(stdin);
printf("Input GPA of Student no. %d: ", i + 1);
scanf("%f", &ipk[i].gpa);
fflush(stdin);
}

for(j = 0; j < length; j++){


printf("\nStudent number %d: \n", j + 1);
printf("NIM: %s \n", ipk[j].nim);
printf("Name of Student: %s \n", ipk[j].name);
printf("GPA: %1.2f \n", ipk[j].gpa);
}
return 0;
}

5. Code:
#include <stdio.h>

struct studentscore{
char nim[11];
char name[30];
char subjectCode[5];
int sks;
char grade[20];
};

int main(){
studentscore score;
printf("Insert your NIM!\nNIM: ");
scanf("%[^\n]", &score.nim);
fflush(stdin);
printf("Insert your name!\nName: ");
scanf("%[^\n]", &score.name);
fflush(stdin);
printf("Insert subject code!\nSubject code: ");
scanf("%[^\n]", &score.subjectCode);
fflush(stdin);
printf("Insert SKS!\nSKS: ");
scanf("%d", &score.sks);
fflush(stdin);
printf("Insert your grade!\nGrade: ");
scanf("%s", &score.grade);
fflush(stdin);

/*Data*/
printf("\nStudent\'s NIM: %s\n", score.nim);
printf("Student\'s Name: %s\n", score.name);
printf("Subject code: %s\n", score.subjectCode);
printf("SKS: %d\n", score.sks);
printf("Student\'s Grade: %s\n", score.grade);
return 0;
}

6. Code:
#include <stdio.h>
#include <string.h>

int weightgrade;

struct studentscore{
char nim[11];
char name[30];
char subjectCode[5];
int sks;
char grade[20];
}score;

int getWeightGrade(char* grade){


if(strcmp(grade, "A") == 0){
weightgrade = 4;
}

else if(strcmp(grade, "B") == 0){


weightgrade = 3;
}

else if(strcmp(grade, "C") == 0){


weightgrade = 2;
}

else if(strcmp(grade, "D") == 0){


weightgrade = 1;
}

else if(strcmp(grade, "E") == 0){


weightgrade = 0;
}
return weightgrade;
}

int main(){
printf("Insert your NIM!\nNIM: ");
scanf("%[^\n]", &score.nim);
fflush(stdin);
printf("Insert your name!\nName: ");
scanf("%[^\n]", &score.name);
fflush(stdin);
printf("Insert subject code!\nSubject code: ");
scanf("%[^\n]", &score.subjectCode);
fflush(stdin);
printf("Insert SKS!\nSKS: ");
scanf("%d", &score.sks);
fflush(stdin);
printf("Insert your grade!\nGrade: ");
scanf("%[^\n]", &score.grade);
fflush(stdin);

getWeightGrade(score.grade);

/*Data*/
printf("\nStudent\'s NIM: %s\n", score.nim);
printf("Student\'s Name: %s\n", score.name);
printf("Subject code: %s\n", score.subjectCode);
printf("SKS: %d\n", score.sks);
printf("Student\'s Grade: %s\n", score.grade);

printf("Your weight grade: %d\n", weightgrade);


return 0;
}

7. Code:
#include <stdio.h>
#include <conio.h>

union convert{
unsigned int dw;
unsigned char b[4];
}converteddw;

int main(){
converteddw.dw = 0x12345678;
printf("%x\n", converteddw.b[0]);
printf("%x\n", converteddw.b[1]);
printf("%x\n", converteddw.b[2]);
printf("%x\n", converteddw.b[3]);
return 0;
}

You might also like