카테고리

asm (27) bootloader_x86_grub (1) C (92) compile (11) config (76) CPP (13) CSS (1) debugging (7) gimp (1) Go (1) html (1) Java (1) JavaScript (1) kernel (19) LibreOffice (3) Linux system progamming (21) MFC (1) opencv (4) OpenGL (1) PHP (1) Python (4) qemu (29) shell (3) socket (7) troubleshooting (2) ubuntu18.04 (2) windows (1)

2018/12/27

유니온 공용 구조

#include <stdio.h>

void main(void){
typedef union{
short individual; // 개인
float won; // 원화
float ounce; // 온스
} amount;

amount orangeAmt = {.ounce = 16};

orangeAmt.individual = 4;

printf("Orange Juice Amount: %.2f : Size: %ld\n\n", orangeAmt.ounce, sizeof(orangeAmt.ounce));
printf("Number of Oranges: %f : Size: %ld\n\n", orangeAmt.individual, sizeof(orangeAmt.individual));

orangeAmt.won = 1.5;

printf("Orange Juice Amount: %.2f : Size: %ld\n\n", orangeAmt.won, sizeof(orangeAmt.won));

typedef struct{
char *brand;
amount theAmount;
} orangeProduct;

orangeProduct productOrdered = {"Chiquita", .theAmount.ounce = 16};

printf("You bought %2.f ounces of %s oranges\n\n", productOrdered.theAmount.ounce, productOrdered.brand);

typedef enum{ INDIV, OUNCE, OWN } quantity;

quantity quantityType = INDIV; // OUNCE Change

orangeAmt.individual = 4;

if(quantityType == INDIV){
printf("You bought %d oranges\n\n", orangeAmt.individual);
} else if(quantityType == OUNCE){
printf("You bought %.2f ounces of oranges\n\n", orangeAmt.ounce);
}

}

댓글 없음:

댓글 쓰기