4

I am doing programs in The C Programming Language by Kernighan and Ritchie.

I am currently at exercise 1-24 that says:

Write a program to check a C Program for rudimentary syntax errors like unbalanced parentheses, brackets and braces. Don't forget about quotes, both single and double, escape sequences, and comments.

I have done everything well... But I am not getting how escape sequences would affect these parentheses, brackets and braces?

Why did they warned about escape sequences?

2
  • Don't count \" as part of the syntax?
    – Alex K.
    Commented Feb 13, 2013 at 17:00
  • 3
    char excerpt[] = "\"Aha!\" he said.";
    – pmg
    Commented Feb 13, 2013 at 17:00

3 Answers 3

4

In "\"", there are three double quote characters, but still it's a valid string literal. The middle " is escaped, meaning the outer two balance each other. Similarly, '\'' is a valid character literal.

Parentheses, brackets and braces are not affected, unless of course they appear in a string literal that you don't parse correctly because of an escaped quote.

2
  • 1
    Parentheses, brackets and braces are not affected. -That is what I wanted! Thanks!!!
    – Sam
    Commented Feb 13, 2013 at 17:05
  • 2
    @SAM: Parentheses, brackets and braces are not affected. Except when they are within a pair of quotes, in which case, they should be ignored for syntax checking purposes.
    – RobH
    Commented Feb 13, 2013 at 17:15
3

I'd guess they mean that you need to differentiate between " (which starts or ends a string) and \" (which is a " character, possibly inside a string)

This is important if you're to avoid reporting e.g. strlen("\")"); as having unbalanced parentheses.

0
2

The obvious possibility would be an escaped quote inside a string. If you don't take the escape into account, you might think the string ended there. For example: "\")\"". The ) is part of the string literal, so it doesn't count as a mis-matched parenthesis.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.