Thread: Help with char array

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Dublin
    Posts
    55

    Help with char array

    How can i put '\t' into a char array and have it not produce a tab when I pass it to putchar?

    I've tried:

    Code:
    char tab[2] = {"\t"};
    then:

    Code:
    putchar(tab[0]);
    putchar(tab[1]);
    but it still produces a tab instead of literally printing '\t'

    :?

    Im basically looking to replace all tabs in the text input with '\t'

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you want the literal two characters \ and t? Then you'll need to make the first character \ (using \\). You also can't fit that into the char array you've defined (on the assumption you want to actually be able to use it), since you need another character for the \0 null terminator.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Dublin
    Posts
    55
    Gotcha! I did this and it worked fine:

    Code:
    char tab[3] = {"\\t"};
    putchar(tab[0]);
    putchar(tab[1]);
    putchar(tab[2]);
    So im guessing this is how you get around all the text formatting keywords when you are looking to use in this type of context? Is there any other way of making a string literal?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BIOS View Post
    How can i put '\t' into a char array and have it not produce a tab when I pass it to putchar?

    I've tried:

    Code:
    char tab[2] = {"\t"};
    then:

    Code:
    putchar(tab[0]);
    putchar(tab[1]);
    but it still produces a tab instead of literally printing '\t'

    :?

    Im basically looking to replace all tabs in the text input with '\t'
    Considering that \t is the ascii tab character (0x08) that's a distinction without a difference.
    If you want it to literally print \t then use \\t.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't think I know what context this is supposed to be in. If you need a literal backslash character, you use two backslashes. If you don't like doing a bunch of putchar, there's always printf.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Dublin
    Posts
    55
    No worries. Thanks guys! It kind of reminds me of rendering Latex documents with the roundabout ways of getting certain characters to render. Although in this case, it's thankfully quite simple and easy to remember!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. uincode char array to array<unsigned char,1>^
    By ripspinner in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2009, 05:41 PM
  2. Copy char array to char pointer
    By Suseela in forum C Programming
    Replies: 9
    Last Post: 08-06-2009, 12:49 PM
  3. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  4. signed char array to unsign char array.
    By beon in forum C Programming
    Replies: 5
    Last Post: 12-14-2006, 07:19 PM
  5. Read File To Char Array with Null char init
    By MicroFiend in forum Windows Programming
    Replies: 1
    Last Post: 10-28-2003, 06:18 PM