1번문제 flag의 값이 1이 나오는 이유?
358
satanmoo
작성한 질문수 5
0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define NLEN 30
struct name_count {
char first[NLEN];
char last[NLEN];
int num;
};
void receive_input(struct name_count*);
void count_characters(struct name_count*);
void show_result(const struct name_count*);
char* s_gets(char* st, int n);
int main()
{
struct name_count user_name;
receive_input(&user_name);
count_characters(&user_name);
show_result(&user_name);
return 0;
}
char* s_gets(char* st, int n)
{
char* ret_val;
char* find;
ret_val = fgets(st, n, stdin); // vs. scanf()
if (ret_val)
{
find = strchr(st, '\n'); // look for newline
if (find) // if the address is not NULL
*find = '\0'; // place a null character there
else
while (getchar() != '\n')
continue; // dispose of rest of line
}
return ret_val;
}
void receive_input(struct name_count* ptr_nc)
{
int flag;
printf("Input your first name:\n>> ");
//s_gets(ptr_nc->first, NLEN);
flag = scanf("%[^\n]%*c", ptr_nc->first); // ^ : cap operator:not을 의미합니다.
printf("%d \n", flag);
if (flag != 1)
printf("Wrong input");
printf("Input your last name:\n>> ");
//s_gets(ptr_nc->last, NLEN);
flag = scanf("%[^\n]%*c", ptr_nc->last);
if (flag != 1)
printf("Wrong input");
}
void count_characters(struct name_count* ptr_nc)
{
ptr_nc->num = strlen(ptr_nc->first) + strlen(ptr_nc->last);
//ptr_nc->num = (int)strlen(ptr_nc->first) + (int)strlen(ptr_nc->last); // int castings remove warnings
}
void show_result(const struct name_count* ptr_nc)
{
printf("Hi, %s %s. Your name has %d characters.\n",
ptr_nc->first, ptr_nc->last, ptr_nc->num);
}
위 코드에서 scanf 의 return이 flag인데 정상적으로 입력했을 때 1이 나오는 이유가 궁금합니다.
답변 1
Export template 안됨
1
45
2
완전히 똑같이 따라해도 exe파일이 안만들어져서 실행이 안됩니다.
1
71
3
main 함수에서 왜 int만 선언이 되는걸까요
1
63
2
8비트 2진수 변환시 왜 1을 더해야하나요?
1
62
2
혹시 강의를 빠르게 수강하려면 어디서부터 듣는게 좋을까요?
1
59
1
프로토타입과 함수간의 인자 불일치
1
81
2
12.12 헤더 관련 질문
1
64
2
Visual Studio Community 2026 사용 문의
1
150
2
Q. 15:30, 부호가 있는 8비트 정수 질문
1
65
2
getchar(), putchar()
1
97
3
강의자리ㅛ
1
86
2
비주얼스튜디오코드로 공부해도 상관없나요?
1
119
2
소스파일안에 여러 파일
1
81
2
F5와 F7의 차이
1
82
2
c = TWO * (a+b); 에서 a와 b는?
1
63
2
; 세미콜론을 붙이는 기준에 문의
1
73
1
Step over 기능 문의
1
57
2
2.6 강의 따옴표 출력 규칙 문의
1
81
2
int main 함수 관련 오류 문의
1
72
2
13.4 words[0]
0
64
2
11.7 함수를 구현해 봤습니다.
1
66
2
11.6 직접 strcmp와 strncmp를 구현해 보았습니다.
1
67
2
11.6 my_strcat과 my_strncat을 구현해봤습니다.
1
57
2
11.6 fit_str함수를 구현해 봤습니다.
1
56
2





