简体   繁体   中英

Relationship between char and ASCII Code?

My computer science teacher taught us that which data type to declare depends on the size of the value for a variable you need. And then he demonstrated having a char add and subtract a number to output a different char. I remember he said this is something to do with ASCII Code. Can anyone explain this more specifically and clearly ? So, is char considerd as a number(since we can do math with it ) or a character or both? Can we print out the number behind a char?how?

So, is char considerd as a number or a character or both?

Both. It is an integer, but that integer value represents a character, as described by the character encoding of your system. The character encoding of the system that your computer science teacher uses happens to be ASCII.

Can we print out the number behind a char?how?

C++ (as the question used to be tagged):

The behaviour of the character output stream (such as std::cout ) is to print the represented character when you insert an integer of type char . But the behaviour for all other integer types is to print the integer value. So, you can print the integer value of a char by converting it to another integer type:

std::cout << (unsigned)'c';

C:

There are no templated output streams, so you don't need to do explicit conversion to another integer (except for the signedness). What you need is the correct format specifier for printf :

printf("%hhu", (unsigned char)'c');

hh is for integer of size char , u is to for unsigned as you probably are interested in the unsigned representation.

A char can hold a number, it's the smallest integer type available on your machine and must have at least 8 bits. It is synonymous to a byte .

It's typical use is to store the codes of characters. Computers can only deal with numbers, so, to represent characters, numbers are used. Of course you must agree on which number means which character.

C doesn't require a specific character encoding , but most systems nowadays use a superset of ASCII (this is a very old encoding using only 7 bits) like eg UTF-8.

So, if you have a char that holds a character and you add or subtract some value, the result will be another number that happens to be the code for a different character.

In ASCII, the characters 0 - 9 , a - z and A - Z have adjacent code points, therefore by adding eg 2 to A , the result will be C .

Can we print out the number behind a char?

Of course. It just depends whether you interpret the value in the char as just a number or as the code of a character. Eg with printf :

printf("%c\n", 'A');                   // prints the character
printf("%hhu\n", (unsigned char)'A');  // prints the number of the code

The cast to (unsigned char) is only needed because char is allowed to be either signed or unsigned, we want to treat it as unsigned here.

A char takes up a single byte. On systems with an 8 bit byte this gives it a range (assuming char is signed) of -128 to 127. You can print this value as follows:

char a = 65;
printf("a=%d\n", a);

Output:

65

The %d format specifier prints its argument as a decimal integer. If on the other hand you used the %c format specifier, this prints the character associated with the value. On systems that use ASCII , that means it prints the ASCII character associated with that number:

char a = 65;
printf("a=%c\n", a);

Output:

A

Here, the character A is printed because 65 is the ASCII code for A .

You can perform arithmetic on these numbers and print the character for the resulting code:

char a = 65;
printf("a=%c\n", a);
a = a + 1;
printf("a=%c\n", a);

Output:

A
B

In this example we first print A which is the ASCII character with code 65. We then add 1 giving us 66. Then we print the ASCII character for 66 which is B .

Every variable is stored in binary (ie as a number,) char s, are just numbers of a specific size.

They represent a character when encoded using some character encoding, the ASCII standard ( www.asciitable.com ) is here.

As in the @Igor comment , if you run the following code; you see the ASCII character, Decimal and Hexadecimal representation of your char .

char c = 'A'; 
printf("%c %d 0x%x", c, c, c);

Output:

A 65 0x41

As an exercise to understand it better, you could make a program to generate the ASCII Table yourself.

My computer science teacher taught us that which data type to declare depends on the size of the value for a variable you need.

This is correct. Different types can represent different ranges of values. For reference, here are the various integral types and the minimum ranges they must be able to represent:

Type                             Minimum Range
----                             -------------
signed char                       -127...127
unsigned char                        0...255
char                same as signed or unsigned char, depending on implementation
short                           -32767...32767
unsigned short                       0...65535
int                             -32767...32767
unsigned int                         0...65535
long                       -2147483647...2147483647
unsigned long                        0...4294967295
long long         -9223372036854775807...9223372036854775807
unsigned long long                   0...18446744073709551615

An implementation may represent a larger range in a given type; for example, on most modern implementations, the range of an int is the same as the range of a long .

C doesn't mandate a fixed size (bit width) for the basic integral types (although unsigned types are the same size as their signed equivalent); at the time C was first developed, byte and word sizes could vary between architectures, so it was easier to specify a minimum range of values that the type had to represent and leave it to the implementor to figure out how to map that onto the hardware.

C99 introduced the stdint.h header, which defines fixed-width types like int8_t (8-bit), int32_t (32-bit), etc., so you can define objects with specific sizes if necessary.

So, is char considerd as a number(since we can do math with it ) or a character or both?

char is an integral data type that can represent values in at least the range [0...127] 1 , which is the range of encodings for the basic execution character set (upper- and lowercase Latin alphabet, decimal digits 0 through 9 , and common punctuation characters). It can be used for storing and doing regular arithmetic on small integer values, but that's not the typical use case.

You can print char objects out as a characters or numeric values:

#include <limits.h> // for CHAR_MAX
...
printf( "%5s%5s\n", "dec", "char" );
printf( "%5s%5s\n", "---", "----" );

for ( char i = 0; i < CHAR_MAX; i++ )
{
  printf("%5hhd%5c\n", i, isprint(i) ? i : '.' );
}

That code will print out the integral value and the associated character, like so (this is ASCII, which is what my system uses):

...
65    A
66    B
67    C
68    D
69    E
70    F
71    G
72    H
73    I 
...

Control characters like SOH and EOT don't have an associated printing character, so for those value the code above just prints out a '.' .

By definition, a char object takes up a single storage unit (byte); the number of bits in a single storage unit must be at least 8, but could be more.


  1. Plain char may be either signed or unsigned depending on the implementation so it can represent additional values outside that range, but it must be able to represent *at least* those values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM