1. Home >
  2. Apps >
  3. Groups >

How does this C program make use of string library?

Question asked by TANMAY PAGARE in #Programming on Jan 13, 2020
TANMAY PAGARE
TANMAY PAGARE · Jan 13, 2020
Rank D2 - MASTER
//------------------ 

#include

#include

int main(void)

{

char joining1[10]="",joining2[10]="",joining3[10]="";

strcat(joining1,"TIME");

strcat( joining1,"_"); strcat( joining1,"_");

strcat( joining3,joining1);

strcat( joining2,"_"); strcat( joining2,"_");

strcpy( joining1,joining2);

strcat( joining1,joining3);

printf("\n time= %s", joining1);

return 0;

}

//------------------ 
Posted in: #Programming
Vinay Mishra
Vinay Mishra · Jan 14, 2020
Rank C1 - EXPERT

Something's wrong with this program. I think you forget to add in include section. You must have to use the string library(string.h) to work with strings like this in any program.

Except for this, the program is working on joining the strings in given arrays by using the functions strcpy, strcat.

strcpy- Used to copy the string in another

strcat- Used to concatenates two strings.

TANMAY PAGARE
TANMAY PAGARE · Jan 14, 2020
Rank D2 - MASTER

I forgot to mention the libraries. There should be stdio.h and string.h and i know the syntax.

  The output for above program is "time= __TIME__".So i can't understand the logic behind it. please explain it.   

Vinay Mishra
Vinay Mishra · Jan 14, 2020
Rank C1 - EXPERT

Check it again, somewhere by mistake you  wrote strcat in place of strcpy.

I applied the logics on paper but there's something wrong with the code.

TANMAY PAGARE
TANMAY PAGARE · Jan 15, 2020
Rank D2 - MASTER

No the program is correct. i had run it on Dev C++ with GCC compiler.


8U2l-qweee.jpg

You must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up