-2

I have some problems with collecting variables..

my current code:

strcpy (date, year);
strcat (date, "-");
strcat (date, month);
strcat (date, "-");

printf("%s" , date);

as result I would like to have 2014-04 for exsample, but currently it gives me 2014-042014-04-

So for some reason it prints it twice. Someone know what is wrong?

Tanhua
  • 39
  • 1
  • 5
  • 5
    There's not enough information here. Please show how date, year and month are allocated and initialized. – Henrik May 22 '14 at 16:57
  • Null terminating your string is your friend http://en.wikipedia.org/wiki/Null-terminated_string – EyeOfTheHawks May 22 '14 at 16:58
  • Ditto's on the lack of information. Possible issues include: date too small, month not terminated with null. – user3344003 May 22 '14 at 17:00
  • 1
    they are in {char *year} and {char *month} after user gives them and char date[64] collecting them – Tanhua May 22 '14 at 17:04
  • 1
    @hyde: that is *not* how `strcat` works. The OP is using the correct syntax so the problem lies in Code Not Shown. – Jongware May 22 '14 at 17:14
  • @Jongware Ah-ha, seems reading with mobile browser somehow messed the output mentioned in question to just `2014-04-`... I was already starting to suspect my own sanity when nobody else saw anything funny with that. – hyde May 22 '14 at 17:26
  • I suspect the bug is that year is incorrect and is "2014-04". Just print month and year and that should shed more light on the issue. – akn May 22 '14 at 18:32

1 Answers1

2

You can eliminate the problem by letting printf do the work instead of trying to assemble the string yourself:

int year = 2014;
int month = 4;
printf("%04i-%02i", year, month);

In general, it's not a good idea to use strcat. It's prone to buffer overruns, and is deprecated in newer versions of the C standard. Using printf means you don't need the intermediate date buffer, avoiding problems related to buffer overruns, missing NULL termination, etc.

If you want the result in a variable instead of printing it out, simply replace printf with snprintf:

char buffer[BUFFER_LEN] = {0};
snprintf(buffer, sizeof(buffer), "%04i-%02i", year, month);
bta
  • 43,959
  • 6
  • 69
  • 99
  • This would work otherside but I'm trying to collect new variable and with it search from database.. sorry didn't mention it :/ – Tanhua May 22 '14 at 17:10
  • @user3665673 Note that while this (updated version) may well "fix" your code to produce the correct output, it seems you are having either overlapping strings, or string which aren't properly nul-terminated. It'd be good idea to get your original code working too, even if you also change your code to use `snprintf` like shown here (which would be good too). – hyde May 22 '14 at 18:08
  • 2
    Using `strcat()` can sometimes cause issues, but it's not deprecated in any version of the C standard. – Crowman May 22 '14 at 18:09
  • I thought C11 deprecated `strcat` in favor of `strcat_s`. I must have remembered incorrectly. – bta May 28 '14 at 18:42