Checking if a character is a '\'

This is a discussion on Checking if a character is a '\' within the C Programming forums, part of the General Programming Boards category; I'm trying to write a program in which I check whether a certain character is a '\', but when I ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    20

    Checking if a character is a '\'

    I'm trying to write a program in which I check whether a certain character is a '\', but when I code the line else if(c=='\') I get a "newline in constant" error. Any ideas?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    if(c == 92) //try the ASCII value for '\'

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    or even better, if (c=='\\')
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Whatever book or document taught you about escaping characters would have explained this to you. There are only a handful of escaped characters, so they wouldn't just leave that out. It's never advisable to actually use the hard coded integer value for a character when the character itself is easily available.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    20
    Thanks all, I got that part running. One last question: The program is supposed to replace all backspace characters with "\b". How would I go about inputting a backspace into the command prompt so I can test if my code works?

  6. #6
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    You cant.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's not exactly true. My web browser certainly knows when I hit the backspace key, which means you can actually input the backspace key. But... you can't easily do it. At least not with standard C. You could pipe the input of a file containing it to your program. Or, you could use some library that takes non-buffered input (ncurses for example), and read keyhits.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Quote Originally Posted by Adak View Post
    if(c == 92) //try the ASCII value for '\'
    That's not self-documenting code, you actually suggested an obfuscation! You naughty coder you!

    The correct answer is of course:
    Code:
    if (c=='\\')
    Ideally along with a description of how escape sequences work, or a link to such information.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I've never heard of escape sequences inside if statements. I thought the comment was entirely sufficient, but then I use ASCII numbers rather frequently.


    ░░░░▒▒▒▒▓▓▓▓ to dress up text output in console windows ▓▓▓▓▒▒▒▒░░░░

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Adak
    I've never heard of escape sequences inside if statements.
    The escape sequence is within a character constant which so happens to be part of an expression used in an if statement's condition.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, I understood that when I first saw the code. I just have not had occasion to use or see an escape sequence, in a char, as part of an if statement.

  12. #12
    Registered User ungalnanban's Avatar
    Join Date
    Feb 2010
    Location
    Chenai
    Posts
    12

    Thumbs up

    See the following Example for check the character is '\' or not

    Code:
    #include<stdio.h>
    
    main()
    {
        int c;
        while((c=getchar())!=EOF)    {
                if(c == '\\')            {
                        printf("Given character is \\");
                }
                else            {
                 printf("%c",c);
                }
        }
    }

  13. #13
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    There is nothing wrong with giving an ASCII number if you know the ASCII
    Table.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by UltraKing227
    There is nothing wrong with giving an ASCII number if you know the ASCII
    Table.
    There is: your reader might not be able to recall offhand what the value means, and for that matter, you might forget too. For example, state what characters in ASCII these values correspond to: 103, 35, 78. Did it take you longer than it took for you to read those numbers? That's way too long.

    Oh, and are you absolutely sure, without checking, that your answers are correct? What about other readers of your code? That's another potential problem right there.

    You might say that a comment will solve this, but...
    Code:
    if (op == 44) /* check if operator is '+' */
    {
        /* ... */
    }
    Last edited by laserlight; 03-04-2010 at 12:05 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    ASCII nums should never be excluded from programming, unless
    the reader is a beginner. everyone has his own methods, i
    like ASCII nums while others may not like using ASCII nums.
    Last edited by UltraKing227; 03-04-2010 at 03:42 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  5. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21