Programming Reference/Librarys
Question & Answer
Q&A is closed
Reads a string from the console.
cgets reads a string of characters from the console and stores the string (and the string length) in the location pointed to by str.
cgets reads as long as a sign to mark the combination carriage return / line feed (CR / LF) occurs or the maximum number of characters has been read. When reading cgets the CR / LF combination is replaced by the combination of characters \ 0 (null terminator) before the string is stored.
Assign str[0] before calling cgets to the maximum length of the string to be read.
After completion of the read operation st is assigned[1] the actual number of characters read.
The reading begins with the character at position str[2] and ends with the null terminator.
Consequently, at least str str[0] in length plus 2 bytes.
return value: a pointer to str[2]
#include <stdio.h> #include <conio.h> int main(void) { char buffer[83]; char *p; buffer[0] = 81; printf("Input some chars:"); p = cgets(buffer); printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); return 0; }
Input some chars: 123 cgets read 3 characters