_cgets, _cgetws
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

_cgets, _cgetws

Get a character string from the console.

char *_cgets( 
   char *buffer 
wchar_t *_cgetws(
   wchar_t *buffer


Storage location for data.

Return Value

_cgets and _cgetws return a pointer to the start of the string, at buffer[2]. There is no error return.


These functions read a string of characters from the console and store the string and its length in the location pointed to by buffer. The buffer parameter must be a pointer to a character array. The first element of the array, buffer[0], must contain the maximum length (in characters) of the string to be read. The array must contain enough elements to hold the string, a terminating null character ('\0'), and two additional bytes. The function reads characters until a carriage return–line feed (CR-LF) combination or the specified number of characters is read. The string is stored starting at buffer[2]. If the function reads a CR-LF, it stores the null character ('\0'). The function then stores the actual length of the string in the second array element, buffer[1]. Because all editing keys are active when _cgets or _cgetws is called, pressing F3 repeats the last entry.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_cgetts _cgets _cgets _cgetws


Routine Required header Compatibility
_cgets <conio.h> Win 98, Win Me, Win NT, Win 2000, Win XP
_cgetws <conio.h> or <wchar.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_cgets.c
// compile with: /c
/* This program creates a buffer and initializes
 * the first byte to the size of the buffer. Next, the
 * program accepts an input string using _cgets and displays
 * the size and text of that string.

#include <conio.h>
#include <stdio.h>

int main( void )
   char buffer[83] = { 80 };  /* Maximum characters in 1st byte */
   char *result;

   printf( "Input line of text, followed by carriage return:\n");
   result = _cgets( buffer );  /* Input a line of text */
   printf( "\nLine length = %d\nText = %s\n", buffer[1], result );

Sample Output

Input line of text, followed by carriage return:
This is a line of text

Line length = 22
Text = This is a line of text

See Also

Console and Port I/O Routines | _getch | Run-Time Routines and .NET Framework Equivalents

© 2016 Microsoft