Detailed explanation of gets() function in C language

1.Description

 char* gets( char* str) function: reads a string from standard input (stdin), does not end when it encounters a space until it encounters a carriage return , stores the string to the string pointed to by str.

2.The difference between gets( ) and scanf( )

gets(str) and scanf("%s",str) have similar functions, but are different. The main differences are:

gets(str): Reading is completed only after carriage return and line feed are reached. Reading is not terminated when encountering a space< a i=3>.

scanf("%s",str): End reading when encountering a space.

#include <stdio.h>
int main()
{
	char str1[20] = { 0 };
	char str2[20] = { 0 };
	gets(str1);
	scanf("%s",str2);
	printf("str1=%s\n",str1);
	printf("str2=%s\n", str2);
	return 0;
}

Print the result:

Let me tell you here by the wayThe difference between printf( ) and puts( ) 

puts() willautomatically wrap lines when outputting a string.

#include <stdio.h>
int main()
{
	char str[20] = { 0 };
	gets(str);
	puts(str);//等价于printf("%s\n", str);
	printf("%s\n", str);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_48460693/article/details/132496982