how to out put strcat of several char variable in one line

0 Nicholas Maswai · October 10, 2015
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main()
    char FirstName[20];
    char * pFname = FirstName;
    char MiddleName[20];
    char * pMname = MiddleName;
    char LastName[20];
    char * pLname = LastName;
    char FullName[100]="";
    char * pWname = FullName;

    puts("What is your name: ");
    fgets(pFname, 20, stdin);
    fgets(pMname, 20, stdin);
    fgets(pLname, 20, stdin);

    strcat(pWname, pFname);
    strcat(pWname, " ");
    strcat(pWname, pMname);
    strcat(pWname, " ");
    strcat(pWname, pLname);

    printf("My name is: ");

return 0;

the code above outputs each part of the name in a different. How can I print in a single line?  Thank you.

Post a Reply


Oldest  Newest  Rating
0 Linguist Llama · October 15, 2015
gets has no way of knowing how large the arrays are for. This is because of the implicit conversion from an expression that is an array to an expression that is a pointer (to the first element of the array); array size information is lost in that conversion.

The gets manual <> says this is how gets is defined:

char *gets(char *s);

See? The argument is a pointer. There is no size information.

The fgets manual <> also states that a pointer is used (in fact this can't be avoided; even if you write an argument as an array it's still a pointer), but cleverly uses a second argument to specify how large the array is:

char *fgets(char *restrict s, int n, FILE *restrict stream);

Hence, you would use it like so:

char first_name[20];
fgets(first_name, sizeof first_name, stdin);

To detect whether or not a full line is entered (and warn when the line is truncated) you could use strcspn:

size_t end = strcspn(first_name, "\n");
if (first_name[end] != '\n') {
   printf("WARNING: First name must be at most %zu characters! User input truncated...\n", sizeof first_name - 1);
first_name[end] = '\0';

It is usually a bad idea to truncate user input, so make sure you at least tell the user when you do so...
0 c student · October 13, 2015
use like this
scanf("%20s", foo);
0 Nicholas Maswai · October 12, 2015
scanf works fine in writing the variables in one line however, I cannot limit the number of characters on each part of the name to 20 using 20, stdin. Similarly i cannot use gets. My objective is to key a maximum of 20 characters on each part of the name or less but on the same line. Is there another way of doing it? Thank you.
0 c student · October 12, 2015
do NOT use gets.  fgets is more secure and if you want to remove the newline character, do it manually by navigating to and replacing it with a null terminator or use a different method such as scanf or getchar.
0 Jeff T. · October 12, 2015
Use 'gets', not 'fgets', it will print everything on one line like you want. 'fgets' adds a newline '\n' character at the end of the input that's why you are getting the output on 3 different lines. You can also get rid of 'fflush' as c student said. Not sure if that's the answer you looking for but 'gets' works.
0 c student · October 10, 2015
try snprintf.  also fflush is undefined for stdin.  it is only for output streams.
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator