Вот один из способов сделать это:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*----------------------------------------------------------------------------
** Allow user to enter a string with optional prompt.
**
** Parameters:
** _O_input String input by the user.
** I__inputSize Size of the _O_input string.
** I__prompt User prompt string, or NULL.
*/
int InputString(
char *_O_input,
size_t I__inputSize,
const char *I__prompt
)
{
int rCode = EXIT_SUCCESS;
char *cp;
/* Display input prompt, if specified. */
if(I__prompt)
printf("%s", I__prompt);
/* Call fgets(), and check for failure. */
if(!fgets(_O_input, I__inputSize, stdin))
{
rCode=EXIT_FAILURE;
fprintf(stderr, "ERR: fgets() failed.\n");
goto CLEANUP;
}
/* Remove the trailing newline from the _O_input string. */
cp=strchr(_O_input, '\n');
if(cp)
*cp='\0';
CLEANUP:
return(rCode);
}
/*----------------------------------------------------------------------------
** Program start.
*/
int main(void)
{
int rCode = EXIT_SUCCESS;
char fname[9+1]; /*Nine charactears plus the string termination character.*/
char lname[9+1];
char fullname[19+1];
/* Have user input first name. */
rCode=InputString(fname, sizeof(fname), "Enter first name: ");
if(rCode)
{
fprintf(stderr, "ERR: InputString() reports: %d\n", rCode);
goto CLEANUP;
}
/* Have user input last name. */
rCode=InputString(lname, sizeof(lname), "Enter last name: ");
if(rCode)
{
fprintf(stderr, "ERR: InputString() reports: %d\n", rCode);
goto CLEANUP;
}
/* Safely combine first and last names into fullname */
snprintf(fullname, sizeof(fullname), "%s %s", fname, lname);
/* Display welcome message. */
printf("Welcome, %s\n", fullname);
CLEANUP:
return(rCode);
}