How to print '\n' instead of a newline?

前端 未结 11 534
南方客
南方客 2020-12-11 04:34

I am writing a program that uses prints a hex dump of its input. However, I\'m running into problems when newlines, tabs, etc are passed in and destroying my output formatti

相关标签:
11条回答
  • 2020-12-11 05:00

    Print "\\n" – "\\" produces "\" and then "n" is recognized as an ordinary symbol. For more information see here.

    0 讨论(0)
  • 2020-12-11 05:03

    In C/C++, the '\' character is reserved as the escape character. So whenever you want to actually print a '\', you must enter '\'. So to print the actual '\n' value you would print the following:

    printf("\\n");
    
    0 讨论(0)
  • 2020-12-11 05:05

    In addition to the examples provided by other people, you should look at the character classification functions like isprint() and iscntrl(). Those can be used to detect which characters are or aren't printable without having to hardcode hex values from an ascii table.

    0 讨论(0)
  • 2020-12-11 05:09

    Just use "\\n" (two slashes)

    0 讨论(0)
  • 2020-12-11 05:10

    There are three solutions for this question:

    Solution 1: Every Symbol, Number, Alphabet has it's own ASCII value. The ASCII value of '\' as 92 and 'n' as 110. The immediate values(Numbers (ASCII)) are stored onto two integer variables. While printing, the format specifier %c (Character), is used.

    void main() { 
        int i=92, j=110; 
        clrscr(); 
        printf("%c%c", i, j);
        getch();
    }
    

    Try it out in your C programming software...

    Solution 2:

    The programs works. But I think this one isn't fair... At the output screen, type the input as \n... you will get another \n..

    void main() {
      char a[10];
      gets(a);
      printf("\n\n\n\n");
      puts(a);
      getch(); 
    }
    

    Try out the programs

    Solution 3: Already said above use \n

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