Thread: Characters that don't appear litterally in printf(" ")

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    65

    Characters that don't appear litterally in printf(" ")

    When using printf or getchar, there are characters started with a \ that don't apear like how they are written (i.e.
    Code:
    printf("\n");
    ) I need a list of them, but don't know what they are called properly so I can't do online searches. If anyone knows, the answer or a link to a list would be great!
    Thanks,
    -Matt

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want a backslash in your output, you need to give printf() [or any other C function] a string with two backslashes, e.g.
    Code:
    printf("Using '\\n' gives you a newline\n");
    will output
    Using '\n' gives you a newline
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    31
    They are called "escape sequences".

    http://www.acm.uiuc.edu/webmonkeys/b...guide/1.1.html

    mc61

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    They are often called "escape/control sequences" or "control characters". This page has a table of some(all?) escape sequences available about a quarter of the way down the page.

    [edit]Ooh, beaten! Darn.[/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    isprint() will tell you if a particular character is printable.

    gg

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Thanks, those were great, but two more questions:
    1What is the syntax for isprint()?
    2Are there escape sequences for numbers/letters (characters that would register from getchar())?


    Matt

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    And one more: is there an easy way to compare a char to a letter?
    (Like this:
    Code:
    #include <stdio.h>
    
    int main()
    {
      int test;
      scanf("%d", &test);
      if(test == 1)
              printf("Text");
      getchar();
      getchar();
    }
    , but with char instead of int)

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mcotter222 View Post
    And one more: is there an easy way to compare a char to a letter?
    (Like this:
    Code:
    #include <stdio.h>
    
    int main()
    {
      int test;
      scanf("%d", &test);
      if(test == 1)
              printf("Text");
      getchar();
      getchar();
    }
    , but with char instead of int)
    Did you try something like
    Code:
    int main()
    {
        char test;
        scanf("%c", &test);
        if(test == '1')
            printf("Text");
        getchar();
        getchar();
    }
    ?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    1 is an int. '1' is a char. The former is what you do arithmetic with, and which can consist of multiple digits. The latter can only be one digit, much like 'a' or '#'. You can convert between the two with:
    Code:
    char c = '1';
    int i = c - '0';
    /* and back again */
    char c2 = i + '0';
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Ahh! Great, I tryed it with " instead of ', that answers all! Thank you so much.
    Matt

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Double quotes denote strings, which are arrays of chars. They're a bit more complicated.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM