Escape character in C

前端 未结 6 1684
面向向阳花
面向向阳花 2021-01-29 12:07

So I want to check if someone enters: \\n (\'\\\' and then \'n\') from the keyboard to a string, so I\'m doing something like this:

相关标签:
6条回答
  • you use another \

        if ( str[i] == '\\' && str[i+1] == 'n');
    
    0 讨论(0)
  • 2021-01-29 12:48

    The escape sequence for an actual backslash is \\, e.g.

    char c = getchar();
    if(c == '\\')
        ... stuff ...
    
    0 讨论(0)
  • 2021-01-29 12:48

    Instead of escape characters you can use the ASCII values like this

      if ((str[i] == 92) && (str[i+1] == 'n'))
            ;
    
    0 讨论(0)
  • 2021-01-29 12:50

    You have to escape \ by doubling it:

    if ( ((str[i] == '\\') && (str[i+1] == 'n')) ){
    }
    
    0 讨论(0)
  • 2021-01-29 12:54

    compare the \ like this. if ((str[i] == '\\') && (str[i+1] == 'n'))

    because we have to escape the escape sequence \.

    0 讨论(0)
  • 2021-01-29 13:08

    if user enter like \ and n using keyboard .

    then try like this

     if(str[i] == '\\' && str[i+1] == 'n')
    

    As \ is represented by \\, so use \\ instead of \ to check.

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