본문 바로가기

C

[C언어] 실습: 구조체 'book'으로 책이름, 저자, 가격 출력하기

반응형

구조체 'book'으로 책이름, 저자, 가격 출력하기


#include<stdio.h>
#include<string.h>   // strcpy()

struct book{
   char title[20];
   char writer[20];
   int price;
};

int main(){

   typedef struct book Book;   // struct book == Book
   Book b1 = {"c programming", "씨", 25000};
   Book b2;

   strcpy(b2.title, "c++ programming");
   strcpy(b2.writer, "씨플플");
   b2.price = 33000;

   Book books[3] = { {"c programming", "씨", 25000},
                     {"c++ programming", "씨플플", 33000},
                     {"java", "자바", 27000}
                   };

   /* books[2]의 멤버 변수 출력하기 */
   printf("책제목 : %s\n", books[2].title);
   printf("저자명 : %s\n", books[2].writer);
   printf("가  격 : %d\n", books[2].price);

   return 0;
}

 

반응형