Write a program to check if the character forms an escape character in C

I tried to check if the alphabet perpendicular to the \ character would form an escape character in C. What would be the easiest way to check this?

I tried adding "\" with an ASCII character set, but it failed

Edit: I do not want to manually add characters. If I could somehow iterate over the ASCII values ​​and add and then print for validation, that would be great!

+4
source share
3 answers

I think the OP might be confused and think that it is possible to programmatically generate these escape sequences of strings in a C program and interpret them specially (possibly using printf or the language environment itself), for example

 char str[3] = "\"; str[1] = 'n'; printf(str); 

This is not possible . All he does is type the literal backslash characters and the letter "n". If you want to check if your compiler interprets the escape sequence, the only way to do this is to write the .c file and run the compiler on it. However, the set of escape sequences is completely standardized, so there is no reason for testing. Just read the language specification or your compiler guide.

+1
source

Script output:

  ascii letters allowed in escape sequences: a, b, e, f, n, r, t, u, v, x, E, U
 Non-escape letters: A, B, C, D, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, V, W,
                        X, Y, Z, c, d, g, h, i, j, k, l, m, o, p, q, s, w, y, z

NOTE: '\U' , '\x' , '\U' alone do not form escape sequences. \ , ' , " , ? and numbers are not taken into account because they are not alphabetic. '\e' is only GCC.

Sequences are created by compiling C code containing the string "\a\b...(for all ascii letters)...\z" and parsing the compilers:

 #!/usr/bin/env python import re, string, subprocess, sys def _find_non_escape_chars(compiler="cc -xc -".split(), verbose=False): # prepare C code to compile test_code = 'char *s = "%s";' % ''.join('\\'+c for c in string.ascii_letters) # compile it p = subprocess.Popen(compiler, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, _ = p.communicate(test_code) if verbose: print stdout # find all non-escape characters return set(re.findall(r"'\\(.)'", stdout)) def is_escape_char(c, non_escape=_find_non_escape_chars()): """Whether `c` letter may be present in an escape sequence in C. >>> f = is_escape_char >>> f("a") True >>> f("g") False """ return c not in non_escape def main(): escape_chars = filter(is_escape_char, string.ascii_letters) print "ascii letters allowed in escape sequences:", ', '.join(escape_chars) print "Non-escape letters:", ', '.join( sorted(set(string.ascii_letters)-set(escape_chars))) if __name__=="__main__": import doctest; doctest.testmod() main() 
+1
source

How about checking all possible cases? For letters, this is \ a, \ b, \ f, \ n, \ r, \ t, \ v - not too much ...

0
source

All Articles