gets() 함수
정의
- 표준 입력으로부터 문자열을 입력받아 변수가 가리키는 메모리에 저장한다.
- 이 때 변수는 배열명이나 포인터 변수여야 한다.
- 포인터 (
*) 혹은 주소 연산자 (&)를 사용하지 않는다.
gcc 에서는 이 함수가 제거될 예정이다. 대신 fgets 함수를 사용하는 게 권장된다.
‘gets’ is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of gets(3), it is highly recommended that you use fgets(3) instead. [-Wdeprecated-declarations]
형식
1
2
| char a[100];
gets(a);
|
예시
기본 예시
1
2
3
4
5
6
| void get_s(){
char str[100];
gets(str);
printf("--------------------\n");
printf("%s", str);
}
|
1
2
3
| # 입력 : The Greatest Showman
--------------------
The Greatest Showman
|
null 문자를 입력하면
1
2
3
| # 입력 : The \0 Greatest Showman
--------------------
The \0 Greatest Showman
|
Reference
C 프로그래밍 (김형근, 곽덕훈, 정재화 공저)
C 프로그래밍 강의 (방송통신대 - 이병래)
Comments