How can I print a quotation mark in C?

后端 未结 9 1422
臣服心动
臣服心动 2020-11-29 04:38

In an interview I was asked

Print a quotation mark using the printf() function

I was overwhelmed. Even in their off

相关标签:
9条回答
  • 2020-11-29 05:06

    you should use escape character like this:

    printf("\"");
    
    0 讨论(0)
  • 2020-11-29 05:18
    #include<stdio.h>
    int main(){
    char ch='"';
    printf("%c",ch);
    return 0;
    }
    

    Output: "

    0 讨论(0)
  • 2020-11-29 05:21

    Try this:

    #include <stdio.h>
    
    int main()
    {
      printf("Printing quotation mark \" ");
    }
    
    0 讨论(0)
  • 2020-11-29 05:21

    Besides escaping the character, you can also use the format %c, and use the character literal for a quotation mark.

    printf("And I quote, %cThis is a quote.%c\n", '"', '"');
    
    0 讨论(0)
  • 2020-11-29 05:21

    You have to use escaping of characters. It's a solution of this chicken-and-egg problem: how do I write a ", if I need it to terminate a string literal? So, the C creators decided to use a special character that changes treatment of the next char:

    printf("this is a \"quoted string\"");
    

    Also you can use '\' to input special symbols like "\n", "\t", "\a", to input '\' itself: "\\" and so on.

    0 讨论(0)
  • 2020-11-29 05:22

    Without a backslash, special characters have a natural special meaning. With a backslash they print as they appear.

    \   -   escape the next character
    "   -   start or end of string
    ’   -   start or end a character constant
    %   -   start a format specification
    \\  -   print a backslash
    \"  -   print a double quote
    \’  -   print a single quote
    %%  -   print a percent sign
    

    The statement

    printf("  \"  "); 
    

    will print you the quotes. You can also print these special characters \a, \b, \f, \n, \r, \t and \v with a (slash) preceeding it.

    0 讨论(0)
提交回复
热议问题