1

I have a question about strcpy and strcat.

In the program I'm trying to make I need to get the year someone was born from a fiscal code. The fiscal code is given as a char from a serial port,

strcpy(temp, code[6]);
strcat(temp, code[7]);
yyyy = 1900 + (atoi(temp));

This is what I came up with: basically the last two digits of the year will be added to 1900 (I know it doesn't quite work with people born in the 2000). The first digit is copied from the full code to a temp variable using strcpy, then I would like to add the second digit to then use atoi and convert eveything to integer; for that I use strcat in a way I've never seen before. Am I doing it right?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
parmigiano
  • 95
  • 1
  • 14
  • 3
    How is `code` declared ? – ameyCU Oct 22 '15 at 13:28
  • 5
    What is `code`? What is `temp`? That's not a minor matter, that's the difference between "works fine" and "undefined behaviour". – DevSolar Oct 22 '15 at 13:28
  • 1
    You need to show a bigger amount of code to get some help associated with some tests results. – Kotshi Oct 22 '15 at 13:29
  • 1
    Just in case `code` is a C string (`char[]`), note that both `strcpy` and `strcat` demand `char *` operands, and you're passing `char`... i.e. undefined behaviour. – DevSolar Oct 22 '15 at 13:30

1 Answers1

5

No need for strcpy/strcat (and they are not appropriate in this context anyway). No need for a temporary string either. You can just do this:

yyyy = 1900 + (code[6] - '0') * 10 + (code[7] - '0');

This just extracts the two digit characters, converts each one to an integer in the range 0..9, and then calculates the year from these two values.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Fair warning: although it's common, there is no guarantee in C that `"0123456789"` is a string of consecutive `char` values. -edit- I see now. Did not know about that :) –  Oct 22 '15 at 13:31
  • 1
    ^^ lol. Anyway, actually writing code is not allowed - it's too complicated. You can only use library functions to handle strings in C :) – Martin James Oct 22 '15 at 13:32
  • 4
    @Rhymoid: in both C and C++ the relevant standards mandate that the characters for 0 to 9 are contiguous and ordered, so this technique works for ASCII and EBCDIC, and anything else for that matter. – Paul R Oct 22 '15 at 13:33
  • Sorry, did I type 'strings'? I meant 'unbelievably lame null-terminated char arrays' :) – Martin James Oct 22 '15 at 13:33
  • 5
    @Rhymoid: Actually the standard (both C and C++) **does** guarantee consecutive digits. (For C it's section 5.2.1 paragraph 3, "the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous." It's the *letters* that are not necessarily consecutive. But I admit I flinch as well whenever I see "character arithmetics" like that, and I am not 100% sure what happens in the case of cross-compilation if source character set != execution character set. ;-) – DevSolar Oct 22 '15 at 13:35
  • 1
    @DevSolar Thanks for citing the clause! –  Oct 22 '15 at 13:37
  • 2
    @MartinJames This is bad, I should flag for "low quality humour" ;) – Kotshi Oct 22 '15 at 13:47