开发者

how can I display the characters \t in a string?

开发者 https://www.devze.com 2023-01-29 23:21 出处:网络
Evening all, What would be the correct key sequence to display \"\\t\" as a literal value, and not a text format?

Evening all, What would be the correct key sequence to display "\t" as a literal value, and not a text format?

My code is below...

Thanks a bunch.

main开发者_开发技巧()
{

  int c;

  while ((c = getchar()) != EOF) {

      if (c == '  ')
          c = "\t";
    putchar(c);
    }

}

So to clarify, I do not want to have a tabbed string, but instead display the characters \t.


You can escape a backslash with another backslash, i.e. "\\t".

Incidentally, you're trying to assign a string (i.e. more than one character) to an int. This doesn't make sense!


Wouldn't you really want something like this instead?

if (c == '\t')
{
    printf("\\t");
}


Escape the backslash, thus "\\t".


To have a backslash in a character/string constant interpreted literally, you have to escape it with another backslash. Also, a single call to putchar() will not be enough since you have to print two characters. With this you get:

putchar('\\');
putchar('t');


You need to escape the escape, as follows:

printf("\\t");

This will print \t as you want.


Actually "\t" requires two characters to display.

main()
{

  int c;

  while ((c = getchar()) != EOF) {

      if (c == '  ') {
       putchar('\\');
       putchar('t');
      }
  }

would be one way of doing it. }

0

精彩评论

暂无评论...
验证码 换一张
取 消