Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing escape sequence in C using hex, dec, and oct values?

Can someone explain this question to me? I don't understand how the book arrived at its values or how one would arrive at the answer.

Here is the question:

Suppose that ch is a type char variable. Show how to assign the carriage-return character to ch by using an escape sequence, a decimal value, an octal character constant, and a hex character constant. (Assume ASCII code values.)

Here is the answer:

Assigning the carriage-return character to ch by using:

a) escape sequence: ch='\r';
b) decimal value: ch=13;
c) an octal character constant: ch='\015';
d) a hex character constant: ch='\xd';

I understand the answer to part a, but am completely lost for parts b, c, and d. Can you explain?

like image 960
Jon Plotner Avatar asked Oct 23 '25 14:10

Jon Plotner


1 Answers

Computers represent characters using character encondings, such as ascii, utf-8, utf-16, iso-8859 (http://en.wikipedia.org/wiki/ISO/IEC_8859-1), as well as others. The carriage return character was used by early computers as a printer instruction to return the printhead to the leftmost position. And the linefeed character was used to index the paper to a new line (thus why DOS uses CRLF for lines, it worked better with dot matrix printers). Anyway the CR character is stored internally as a numeric value in either a single 8-bit byte/octet or a 16-bit pair of two bytes/octets, depending upon your language.

The common ascii characterset is found here: http://www.asciitable.com/ and you can find that CR, '\r', 13, 0xD, et al are different representations for the same value.

Strings are just sequences of characters stored either as an array of characters with a marker at the end (terminator), or stored with a count of the current string length.

like image 139
ChuckCottrill Avatar answered Oct 25 '25 03:10

ChuckCottrill