전공/C

과제10 - StructUnion (1 ~ 7)

yha97 2021. 6. 22. 14:44

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// creating structure
typedef struct Student{
	char name[20];
	int id;
	double salary;
};

int main(){
	struct Student s; // declaring structure
	
	// initializing structure's contents
	strcpy(s.name, "Henry");
	s.id = 1112;
	s.salary = 95743;
	
	// printing output
	printf("Name: %s\n", s.name);
	printf("Id: %d\n", s.id);
	printf("Salary: %f", s.salary);
	
	return 0;
}

 

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// creating structures
struct birth{
	int day;
	int month;
	int year;
};

struct Student{
	char name[20];
	int rollnum;
	struct birth date; // it has object of birth(nested structure)
};

int main(){
	struct Student s; // declaring structure
	
	// initializing structure's contents with entering from user
	printf("Enter name: ");
	scanf("%s", s.name); getchar(); // inputting name from user
	printf("Enter roll number: ");
	scanf("%d", &s.rollnum); // inputting roll number from user
	printf("Enter Date of Birth [DD MM YY] format:");
	scanf("%d %d %d", &s.date.day, &s.date.month, &s.date.year); // inputting date from user
	printf("\n");
	
	// printing output
	printf("Name: %s\n", s.name);
	printf("RollNo : %d\n", s.rollnum);
	printf("Date of birth : %02d/%02d/%d", s.date.day, s.date.month, s.date.year);
	
	return 0;
}

 

 

 

 

#include <stdio.h>
#include <stdlib.h>

// making structure
typedef struct {
	char name[20];
	double quantity;
	double price;
	double amount;
} Fancy;

int main(){
	// declaring and initializing structure
	Fancy my;
	Fancy* ptr; // pointer of structure Fancy
	ptr = &my;
	
	// entering contents from user
	printf("Enter product name: ");
	scanf("%s", ptr->name); getchar(); // to erase new line
	printf("Enter price: ");
	scanf("%lf", &(ptr->price));
	printf("Enter quantity: ");
	scanf("%lf", &(ptr->quantity));
	ptr->amount = ptr->price * ptr->quantity; // calculating amount of item
	
	// print output
	printf("\nName: %s\n", ptr->name);
	printf("Price: %f\n", ptr->price);
	printf("Quantity: %d\n", (int)ptr->quantity); // casting from double to integer to print like sample
	printf("Total Amount: %f\n", ptr->amount);
	
	return 0;
}

 

 

 

#include <stdio.h>

// declaring union
union prac{
	char *name;
	char names[20];
};

int main()
{
 	// initializing unions
    union prac p1,*ptr;
    p1.name="Henry Wong";//assigning string to null character
    ptr=&p1; // pointing p1
    
    // printing output
 	printf("\n\n Pointer : Show a pointer to union :\n"); 
	printf("----------------------------------------\n");

    printf(" %s %s\n\n",ptr->name,(*ptr).name);

    return 0;
}

 

 

 

#include <stdio.h>
#include <stdlib.h>

// declaring union with 3 variable, and array size 32
union u1{
	int n1;
	short n2, n3;
	char arr[32];
};

// declaring structure with 3 variable, and array size 32
struct s1{
	int n1;
	short n2, n3;
	char arr[32];
};

int main(){
	// creatin union and structure
	union u1 u;
	struct s1 s;
	
	// printing output with size of union and structure
	printf("size of union = %d bytes\n", sizeof(u));
	printf("size of structure = %d bytes", sizeof(s));
	
	return 0;
}

 

 

 

#include <stdio.h>
#include <stdlib.h>

// creating structure
struct score{
	int id;
	double gpa;
};

int main(){
	// declaring structures and pointer
	struct score s1;
	struct score s2;
	struct score* ptr;
	
	// assigning valus into variables of structures
	s1.id = 1;
	s1.gpa = 2.9;
	s2.id = 2;
	s2.gpa = 3.7;
	
	ptr = &s1; // pointing s1
	// printing output with pointer
	printf("Student1: ID =%d\n", ptr->id);
	printf("Student1: GPA =%lf\n", ptr->gpa);
	ptr = &s2; // pointing s2
	// printing output with pointer
	printf("Student2: ID =%d\n", ptr->id);
	printf("Student2: GPA =%lf", ptr->gpa);
	
	return 0;
}

 

 

 

#include <stdio.h>
#include <stdlib.h>

// creating structure
union score{
	int id;
	double gpa;
};

int main(){
	// declaring structures and pointer
	union score s1;
	union score* ptr;
	
	// assigning valus into variables of structures
	
	ptr = &s1; // pointing s1
	// printing output with pointer
	// pointing id and gpa whenever printing output due to union
	s1.id = 1;
	printf("Student: ID =%d", ptr->id);
	s1.gpa = 2.9;
	printf(" and Student: GPA =%lf\n", ptr->gpa);
	s1.id = 2;
	printf("Student: ID =%d", ptr->id);
	s1.gpa = 3.7;
	printf(" and Student: GPA =%lf", ptr->gpa);
	
	return 0;
}