gets Function

Get a string from stdin.




char * gets(char * s);


pointer to the storage string

Return Value

Returns a pointer to the string s if successful; otherwise, returns a null pointer.


The gets() function reads characters from the stream stdin and stores them into the string pointed to by s until it reads a newline character (which is not stored) or sets the end-of-file or error indicators. If any characters were read, a nul character is stored immediately after the last read character in the next element of the array. If gets() sets the error indicator, the array contents are indeterminate.

When building with the MPLAB XC8 compiler for PIC MCUs, the gets() function relies on the getch() function being properly defined to obtain input from the required peripheral or location. Reading will not work as expected until the getch() stub (found in the pic/sources/c99/common/getch.c of your compiler distribution) is completed. See Example code for 8-bit PIC MCUs for information on defining getch() so that text can be read from a file in the MPLAB X IDE simulator.


#include <stdio.h>

int main(void)
  char y[50];

  gets(y) ;
  printf("Text: %s\n", y);

Example Input

Contents of UartIn.txt (used as stdin input for simulator):

Longer string"

Example Output

Text: Short