이 while 문에서 scanf한게 3이 된다는게 무슨뜻인가요?
조회수 954회
1 답변
-
http://www.cplusplus.com/reference/cstdio/scanf/
일단
scanf
함수의 문서를 봅시다.사용방법이 잘 나와 있습니다.
여기서 궁금한
scanf
의 리턴값을 봅시다.- Return Value
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.
If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ.
함수호출의 성공시 대입된 변수의 갯수를 리턴한답니다.
그럼 질문상의 코드를 보죠
while(scanf("%d %s %s", &s[n].id,s[n].name,s[n].major)==3) n++;
보다시피
scanf
함수에 의해&s[n].id
,s[n].name
,s[n].major
변수 3개를 받게 되므로 결과는 3을 리턴합니다.즉 위의 코드는 기대한대로 3개의 값을 입력한다면
n
을 하나 더합니다.
댓글 입력