1.char * gets ( char * str )

Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached.

The newline character, if found, is not copied into str.

A terminating null character is automatically appended after the characters copied to str.

字符串拷贝完成后,会自动在str的末尾添加一个’\0’;

Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str (which can lead to buffer overflows).

On success, the function returns str.
If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged).
If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned (but the contents pointed by str may have changed).

例:

/* gets example */
#include <stdio.h>

int main()
{
  char string [256];
  printf ("Insert your full address: ");
  gets (string);     // warning: unsafe (see fgets instead)
  printf ("Your address is: %s\n",string);
  return 0;
}

注意:

在最新的C语言标准当中,已经移除了这个函数。在c++的2011标准当中,这个函数已经被弃用,原因是这个函数不检查输入的字符个数,可能会造成溢出的问题,具体例子参见http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1049157810&id=1043284351)。一般改用fgets()函数。

2.char * fgets ( char * str, int num, FILE * stream )

Get string from stream
Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.

A terminating null character is automatically appended after the characters copied to str.

Notice that fgets is quite different from gets: not only fgets accepts a stream argument, but also allows to specify the maximum size of str and includes in the string any ending newline character.

例:

#include <stdio.h>
int main()
{
  char string [256];
  printf ("Insert your full address: ");
  fgets (string,255,stdin);    
  printf ("Your address is: %s\n",string);
  return 0;
}

stdin: 标准输入流,是输入数据的默认来源,一般默认指向键盘。

3.char * strcpy ( char * destination, const char * source )

Copy string
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.

例:

/* strcpy example */
#include <stdio.h>
#include <string.h> // strcpy()函数头文件

int main ()
{
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  strcpy (str2,str1);
  strcpy (str3,"copy successful");
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;
}

输出:


str1: Sample string
str2: Sample string
str3: copy successful