#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void main()
{
    char name[30],mname[40];
    printf("enter the num:");
    gets(name);

    strcat(name,123);
    printf("%s",name);
}

is it possible to concatenate num and character its not working how to concatenate numbers and character

You should never ever use gets() because it will allow you to enter more characters then the buffer can hold, excess characters are just scribbled all over memory and your program will crash. There are a couple ways to avoid that problem

  1. fgets() -- e.g. fgets(name, sizeof(name), stdin); The problem with fgets() is that it may append '\n' to the end of the string, which your program will have to remove. If '\n' is not present at the end of the string that means there are more keys in the keyboard buffer which your program needs to flush out.
  2. scanf() -- e.g. scanf("%20s", name);

@ancient dragon will you please tell me that what is that problem of '\n' with fgets. i am not getting it from your answer. will you please explain it more . isn't scanf appends '\n' to the end ? please clearify me.
thanks in advance.

Edited 4 Years Ago by I_m_rude

ancient dragon will you please tell me that what is that problem of '\n' with fgets. i am not getting it from your answer. will you please explain it more . isn't scanf appends '\n' to the end ? please clearify me.

Try it and see. You can't always rely on someone else to explain things to you, often a little test program is sufficient to answer your own questions.

'\n' ( 0x0D hex) represents the value of the Return key that you press on the keyboard. You should become familiar with the data found in ascii charts such as this one. Find 0D in the hex column then look in the Description column where you see "Carriage Return"

This question has already been answered. Start a new discussion instead.