6.18.31 gets Function
Get a string from stdin
.
Include
<stdio.h>
Prototype
char * gets(char * s);
Argument
s
- pointer to the storage string
Return Value
Returns a pointer to the string s if successful; otherwise, returns a null pointer.
Remarks
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.
Example
#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):
"Short
Longer string"
Example Output
Text: Short