#include <stdio.h>
#include <string.h>
//gets() - get string
//puts() - print string + \n
int main()
{
char longString[100];
printf("Enter String: \n");
gets(longString);
printf("You Entered: \n");
puts(longString);
return 0;
}
Result:
Enter String:
Some String
You Entered:
Some String
Process returned 0 (0x0) execution time : 6.392 s
Press any key to continue.
Note that the function gets()
is deprecated in modern C standards (C11 and later) due to security vulnerabilities. It is recommended to use fgets()
instead.
That being said, let's discuss the gets()
and puts()
functions:
-
gets()
reads a line of characters fromstdin
(standard input) and stores it as a C-style string in the buffer pointed to by its argument. It continues reading until it encounters a newline character ('\n'
) or the end-of-file character (EOF
). The newline character, if present, is replaced with a null terminator ('\0'
) in the buffer. The function returns the same buffer if successful, or a null pointer if an error occurred. -
puts()
writes the string pointed to by its argument tostdout
(standard output), followed by a newline character ('\n'
). It returns a non-negative integer if successful, orEOF
if an error occurred.
In the example code you provided, the user is prompted to enter a string using gets()
. The input string is then printed to the console using puts()
. Note that puts()
automatically adds a newline character at the end of the string, which creates a line break before the next console output.
No comments:
Post a Comment