Gursheesh Singh Gursheesh Singh - 1 month ago 5
C Question

Not Getting Any output While Printing after combining strings

To combine First , middle and last name of a person.

int main()

{

int i,j,k;

char first_name[11]="Gursheesh";

char middle_name[10]="Singh";

char last_name[10]="Chawla";

char name[30];

for(i=0;first_name!='\0';i++)

{

name[i]=first_name[i];

}

name[i]=' ';

i++;

for(j=0;middle_name!='\0';j++)

{

name[i+j]=middle_name[j];

}

name[i+j]=' ';

j++;

for(k=0;last_name!='\0';k++)

{

name[i+j+k]=last_name[k];

}

name[i+j+k]='\0';

printf("%s",name);

}


this is the code

the compiler at run time shows nothing

I cannot find the mistake,help me.

is it having some loop continue this infinity or some logical misktake.

Answer

All your loops are infinite:

for(i=0;first_name!='\0';i++)

condition first_name!='\0' is always true. You should check for first_name[i] != '\0':

for(i=0; first_name[i] != '\0'; i++)

By the way, to copy strings it's better to use strcpy function.

Comments