strcpy info

Hi again ppl.

Am using this line of code

strcpy_P(str, CMD);

that copies CMD into str and CMD is defined as #define CMD  "ATSP2\r".

My question is does strcpy add NULL (\0) automatically at the end like sprintf does? Or if not is it good practice to have CMD as this instead #define CMD  "ATSP2\r\0" with the NULL added.

Many thanks.

Am using

which refers to cmd.

that copies cmd into str and CMD is defined as

How is cmd related to CMD? Case matters. A great deal.

My question is does strcpy add NULL (\0) automatically at the end like sprintf does?

Yes. The 2nd argument must be a string (a NULL terminated array of chars) though.

Sorry, edited to correct the CMD case. So i dont need to add \0 to my defines because strcpy does that for me and am i correct in thinking my str size is going to be 7?

Nearly: The string literal “A Text” does that for you already. (create 7 bytes including a final 0) Not sure if that 0 is copied or created from scratch in the target : )
strlen(CMD) returns 6.
sizeof (str) depends on the definition of str. E.g. char str[10] has a sizeof 10.

And:

  • strcpy_P takes the src from PROGMEM. Have a look at the F() macro or the PROGMEM keyword, to define CMD lying there.

edit: strlen/sizeof comment