**gets_s()**
is a built-in function defined in the <stdio.h>
header. The function is available from the C11 version of C.
The following is the function prototype:
char* gets_s(char* str, rsize_t n)
gets_s()
reads data from the Standard Input, stdin
, and stores it into the character array pointed to by str
in the function prototype.
stdin
is an input stream where data is sent to and read by a program.
The function returns str
when one of the following cases occur:
A newline (\n
) character is read
Exactly n - 1
characters have been read
The function writes the terminating null character into str
before returning the character array.
gets_s()
returns a null pointer on failure. This may occur in any of the following cases:
str
is null
end-of-file
before storing n - 1
characters
#include<stdio.h>
int main()
{
char name[16]; //15 chars + 1 '\0' char
printf("Enter Name: ");
gets_s(name, 15);
printf("Hello, %s !", name);
return 0;
}
Free Resources