Export (0) Print
Expand All

gets, _getws

Get a line from the stdin stream.

char *gets( 
   char *buffer 
);
wchar_t *_getws( 
   wchar_t *buffer 
);

Parameter

buffer
Storage location for input string.

Return Value

Returns its argument if successful. A NULL pointer indicates an error or end-of-file condition. Use ferror or feof to determine which one has occurred.

Remarks

The gets function reads a line from the standard input stream stdin and stores it in buffer. The line consists of all characters up to and including the first newline character ('\n'). gets then replaces the newline character with a null character ('\0') before returning the line. In contrast, the fgets function retains the newline character. _getws is a wide-character version of gets; its argument and return value are wide-character strings.

Security Note   Because there is no way to limit the number of characters read by gets, untrusted input can easily cause buffer overruns. Use fgets instead.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_getts gets gets _getws

Requirements

Routine Required header Compatibility
gets <stdio.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
_getws <stdio.h> or <wchar.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_gets.c

#include <stdio.h>

int main( void )
{
   char line[21]; // room for 20 chars + '\0'
   gets( line );  // Danger: No way to limit input to 20 chars.
                  // Much preferable: fgets( line, 21, stdin );
                  // but you'd have to remove the trailing '\n'
   printf( "The line entered was: %s\n", line );
}

Input

Hello there!

Output

The line entered was: Hello there!

Note that input longer than 20 characters will overrun the line buffer and almost certainly cause the program to crash.

See Also

Stream I/O Routines | fgets | fputs | puts | Run-Time Routines and .NET Framework Equivalents

Show:
© 2014 Microsoft