inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

홍정모의 따라하며 배우는 C언어

14.4 구조체의 배열 연습문제

연습문제

232

fe

작성한 질문수 25

1

안녕하세요.

int flag = scanf("%f", &library[count].price); 에서

int flag라고 변수를 따로만드는 이유는 무엇인가요?

scanf("%f", &library[count].price); 이렇게만 해두는거랑 무슨 차이가있나요?

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX_TITLE 40
#define MAX_AUTHOR 40
#define MAX_BOOKS 3

char* s_gets(char* st, int n)
{
	char* ret_val;
	char* find;

	ret_val = fgets(st, n, stdin); //공백을 포함한 문자열
	if (ret_val)
	{
		find = strchr(st, '\n'); //st에서 \n을 찾는다.
		if (find)
			*find = '\0';
		else
			while (getchar() != '\n')
				continue;
	}

	return ret_val;
}

struct book
{
	char title[MAX_TITLE];
	char author[MAX_AUTHOR];
	float price;
};

int main()
{
	struct book library[MAX_BOOKS] = { {"Empty","Empty",0.0f}, };
	//구조체배열 선언과 초기화
	int count = 0;

	while (1)
	{
		printf("Input a book title or press [Enter] to stop.\n>>");		
		if (s_gets(library[count].title, MAX_TITLE) == NULL) break;
		if (library[count].title[0] == '\0') break;
			
		printf("Input the author.\n>>");
		s_gets(library[count].author, MAX_AUTHOR);

		printf("Input the price.\n>>");
		int flag = scanf("%f", &library[count].price);
		while (getchar() != '\n')
			continue; //버퍼비우기

		count++;

		if (count == MAX_BOOKS)
		{
			printf("No more books.\n");
			break;
		}
	}

	if (count > 0)
	{
		printf("\nThe list of books:\n");
		for (int index = 0; index < count; index++)
			printf("\"%s\" written by %s: $%.1f\n",
				library[index].title, library[index].author, library[index].price);
	}
	else
		printf("No books to show.\n");

	return 0;
}

c

답변 1

4

Hello Yeo

안녕하세요?
없애면 Return value ignored: scanf warning이 뜨긴합니다.
아마 이 부분이 불편해서 일부러 지우신게 아닐까 생각이드는군요.
그런데 flag 를 만드신 local scope 내에서 사용하지는 않으므로 크게 의미는 없습니다.

Export template 안됨

1

11

2

완전히 똑같이 따라해도 exe파일이 안만들어져서 실행이 안됩니다.

1

54

3

main 함수에서 왜 int만 선언이 되는걸까요

1

56

2

8비트 2진수 변환시 왜 1을 더해야하나요?

1

54

2

혹시 강의를 빠르게 수강하려면 어디서부터 듣는게 좋을까요?

1

49

1

프로토타입과 함수간의 인자 불일치

1

73

2

12.12 헤더 관련 질문

1

60

2

Visual Studio Community 2026 사용 문의

1

137

2

Q. 15:30, 부호가 있는 8비트 정수 질문

1

60

2

getchar(), putchar()

1

93

3

강의자리ㅛ

1

79

2

비주얼스튜디오코드로 공부해도 상관없나요?

1

112

2

소스파일안에 여러 파일

1

75

2

F5와 F7의 차이

1

76

2

c = TWO * (a+b); 에서 a와 b는?

1

58

2

; 세미콜론을 붙이는 기준에 문의

1

68

1

Step over 기능 문의

1

53

2

2.6 강의 따옴표 출력 규칙 문의

1

74

2

int main 함수 관련 오류 문의

1

67

2

13.4 words[0]

0

60

2

11.7 함수를 구현해 봤습니다.

1

62

2

11.6 직접 strcmp와 strncmp를 구현해 보았습니다.

1

64

2

11.6 my_strcat과 my_strncat을 구현해봤습니다.

1

53

2

11.6 fit_str함수를 구현해 봤습니다.

1

53

2