Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C encoding of character constants

My programmer's instinct would say that a character constant in c (eg: 'x') is encoded using the machine character set from the machine on which it is compiled. However, the following exerpt is from "The C Programming Language: ANSI C Edition"

"A character constant is a sequence of one or more characters enclosed in single quotes, as in 'x'. The value of a character constant with only one character is the numeric value of the charachter in the machine's character set at execution time."

Emphasis on the last 3 words.

Can anyone explain why they would say "at execution time". Surely the character value is encoded in the compiled binary (or ELF, A.OUT...) ?

I was wondering, but couldn't come up with any logical explanaition for this, surely K & R knew what they were doing!

like image 229
Max Snijders Avatar asked Jan 17 '13 17:01

Max Snijders


1 Answers

You will have to tell the compiler what system you are going to run the program on. It will then choose the proper encoding for the characters.

Of course, default is to run on a system similar to the one running the compiler. In that case the compile time and runtime character sets will be identical.

like image 87
Bo Persson Avatar answered Sep 28 '22 07:09

Bo Persson