이 while 문에서 scanf한게 3이 된다는게 무슨뜻인가요?

조회수 952회

while(scanf("%d %s %s", &s[n].id,s[n].name,s[n].major)==3) n++;

뭐가 3이라는 거죠..?

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    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을 하나 더합니다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)