• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    해결됨

gets() warnings 이유

21.08.24 00:55 작성 조회수 151

0

#include <stdio.h>

#include <string.h>

void my_strcmp(void);

int main()

{

my_strcmp();

return 0;

}

void my_strcmp(void)

{

char str1[100] = "";

char str2[100] = "";

gets(str1);

gets(str2);

int i = 0;

int num1 = 0, num2 = 0, count = 0;

while (str1[i] != '\0' || str2[i] != '\0')

{

if (str1[i] != str2[i])

{

num1 += (int)str1[i];

num2 += (int)str2[i];

count++;

}

i++;

}

if (count == 0)

printf("%d", count);

else

printf("%d", num1 > num2 ? 1 : -1);

}

안녕하세요,

strcmp함수를 나름대로 구현해 봤는데 my_strcmp 함수 안 gets에 warinings가 왜 뜨는걸까요 undefined되었다고 하는데 도저히 이유를 모르겠습니다,,ㅠ

답변 2

·

답변을 작성해보세요.

1

안소님의 프로필

안소

2021.08.25

안녕하세요 :)

scanf 사용시에도 warning 이 발생하기 때문에 CRT_NO_SECURE 이런 전처리를 해줬던 것 기억 나실 것입니다.  질문자님께서도 앞 강의 따라 이런 처리를 해주셨을거라고 생각해요!

gets 도 이와 비슷하게 사용시 warning 이 발생하는 함수입니다. gets_s 나 혹은 fgets 로 대체하여 사용하도록, 추천하지 않는 함수랄까요.

2014년에 만든 C언어 표준에서부터는 gets 가 완전히 제거됐다고 합니다. 즉, 없는 함수인거에요 사실상..! (https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used)

https://www.cplusplus.com/forum/beginner/275391/

이곳에선 dead 함수라고 표현하네요. 링크 참고해보시면 좋을 것 같아요!

0

saneum3님의 프로필

saneum3

질문자

2021.08.25

아하 그렇군요 구체적이고 친절한 답변 감사드립니다,,