简体   繁体   中英

C - How do you store a string with variables in it within an array?

Because I know that

char array[STRING_ELEMENTS + 1][MAX_STRING_LENGTH + 1];

/*just for the first element*/
array[0] == ("this number: %d, and that number: %d\n", a, b);

probably makes me look disgusting. I would have no clue how else to do this though.

You can't assign a string like this in C, but you can use snprintf :

snprintf(array[0], MAX_STRING_LENGTH, "this number: %d, and that number: %d\n", a, b);

(don't forget to #include <stdio.h> somewhere near the top of your program.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM