1

Is it possible to include variables through using %c and such when you have strings saved into the array?

char *MainMenuNames[] = { "%c - Move Left", "%c - Move Right","%c - Move Up","%c - Move Down","%c - Back","%c - Confirm","%c - Show Stats","%c - Show Inventory","%c - Show Legend","%c - Show Controls"};

When I have something like this, is it possible to put something into the %c?

3
  • Use it as a format string to sprintf, maybe? Commented May 9, 2018 at 18:16
  • That's exactly what the printf family of functions does. printf itself prints to stdout, but you can print to a file, or even just assemble a string with snprintf and do with it what you like. Commented May 9, 2018 at 18:17
  • printf and such are runtime functions. You can't achieve their effect at compile time. Commented May 9, 2018 at 18:19

1 Answer 1

2

You can use the array element as a parameter to formatting functions like printf()

char *commands = "lrudbcsiln";
for (int i; i < strlen(commands); i++) {
    printf(MainMenuNames[i], commands[i]);
    putchar('\n');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.