Reputation: 75
I read that in C, char is actually an integer because characters are represented as patterns of bits.
So I wonder when you run your program how does the operating system know when to print the bits pattern as a number or a char. What code does determine which is to be printed?
And, where is the ASCII table located, and how does the conversion work?
Upvotes: 3
Views: 528
Reputation: 50190
... characters are represented as patterns of bits.
On a computer, everything is represented as a pattern of bits: characters, integers, real numbers, and executable code. The eight bits 01000001
could represent the integer 65
(though integers normally take up at least 16 bits), or the letter A
in the ASCII system. There are elaborate ways of keeping track of what is what, it's one of the main responsibilities of the operating system and of high-level programming languages. printf
is one of the rare cases where the programmer has to explicitly tell a function what kind of data is being passed.
I read that in C, char is actually an integer because characters are represented as patterns of bits.
That's not quite right: In C, chars and ints have been distinct types for a very long time. C allows you to use a char
as an int
if you want, by choice of the language designers: the bit pattern is basically used as the corresponding int
. C won't let you use a float
as an int
since the bit pattern would not give you anything useful (the bit pattern for 1.0
looks nothing like the bit pattern for integer 1
).
As for the ASCII table, it's embodied in the design of the hardware and software that displays text, and of the programming language functions that manipulate it. A computer font is a mapping from numbers to shapes. or "glyphs". In the simplest case, it maps numbers in the ASCII range (32-126) to the appropriate glyph. (In reality it's often a more indirect route to the same result). Old computer terminals had the glyphs hardwired, while Windows or X11 applications use software fonts.
As for programming languages, a function like isdigit()
or isalpha()
just looks up the character code on a table of its properties: isdigit()
returns true
for the numbers 48-57 (which encode the ASCII digits), and false
for all others. No glyphs are involved.
Upvotes: 0
Reputation: 214
If you are running a linux machine just type man ascii in a terminal and you'll see all of the char values (in decimal, octal and hexa). If not, just google ascii table and you'll probably be done.
Upvotes: 3
Reputation: 121387
It depends on how you tell your program to interpret the bits in your code. For example,
printf("%d %c", 'a', 97);
This will print: 97 a
Upvotes: 3
Reputation: 206526
How does the operating system know when to print the bits pattern as a number or a char.
It doesn't. You explicitly tell the compiler whether to treat it as a character or integer by specifying the proper format descriptor to printf
. And that is the very reason that if there is a mismatch between format descriptor and type of the actual argument then it results in Undefined Behavior.
Upvotes: 4