Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

K and R exercise 1-24

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?

like image 826
Sam Avatar asked Jan 15 '23 05:01

Sam


2 Answers

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.

like image 159
Fred Foo Avatar answered Jan 16 '23 20:01

Fred Foo


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.

like image 33
simonc Avatar answered Jan 16 '23 19:01

simonc