Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add const char and char in c

I have a player char player = x; and want to overwrite a string char string[30]; to contain "Player" + player + "won". I tried

strcpy_s(string, "Player ");
strcat_s(string, player);
strcat_s(string, " won\n");

but obviously this doesn’t work, because char is not compatible with const char

How can I do it instead?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re looking for snprintf, your general purpose string-formatting stdlib function.

snprintf(string, sizeof(string), "Player %c won\n", player);

You can read all about the different % formatting directives available in the above link, but %c is the one you want for characters.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading