0

Simple example for my problem:

char answer;

printf("enter your favorite letter: \n");

answer = getchar();

Now here is the problem. I want to print that character in screen. But by using "answer" variable. Something like this:

printf(answer);

But of course it doesn't work. What do I do?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
blackwater7
  • 107
  • 1
  • 9

2 Answers2

0

Are you just after the command to print a char to the console?

int main() {
    char answer;

    printf("enter your favorite letter: \n");

    answer = getchar();

    printf("%c\n", answer);
}
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
0

You need to use printf. This lets you 'embed' variables in output using placeholders declared with % prefixes.

The placeholder for a character is %c, so you could use the following:

printf("Your favorite letter is %c", answer);

answer is passed as a parameter and replaces the %c.

This question should give you more information about printf.

Aaron Christiansen
  • 11,584
  • 5
  • 52
  • 78