Thread: 'char' integer type or character type?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    48

    'char' integer type or character type?

    Hi,

    I read a page about C character type history.
    It says that there were just 'char' and 'unsigned char' in K&R C first edition. C language used these two data types as integer types already at that time. My question is:
    were 'char' and 'unsigned char' used as integer types from the very beginning? That is, were they designed for integer types by the C creators?
    If so, why were they named 'char's? I thought 'char' was for characters, not integers.

    Some people say that there is no dedicated character type in C. 'char' is actually an integer type, then who is responsible for transforming an integer to a character? For example, printf() with '%c' format, does printf() transform the integer to a character?

    Thanks,

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    The operating system is responsible for this; you tell it to (e.g. on Linux) write() and point it to a buffer that contains, say, the three 8 bit numbers [97, 98, 99] and it will send the characters 'abc' to the appropriate place.

    char is an integer type; you can use it even when not dealing with characters. On computers, all data (and your program) are really just a bunch of numbers, it's just how they're interpreted by different programs that makes them semantically different.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    @JohnGraham,
    Thanks. Just still curious why they name an integer type 'char' .. did they want to indicate people like this: when you want a character, use this integer type named 'char'.
    They may make separations for clear if the design is from the very beginning, for example, use a new keyword, say 'small': 'signed small' replaces 'signed char', 'unsigned small' replaces 'unsigned char', 'char' still for characters. This looks more reasonable.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    There are lots of things you can criticize about the language design (most of them rightly too).
    The names used for some things is one of them (char, float, creat, qsort, ..., ..., ...).

    Fortunately, since the acceptance and implementation of the 1999 Standard, you can use "Exact-width integer types" (int8_t, uint16_t, ...) if available on your implementation. Just remember to include <stdint.h> (or <inttypes.h>).

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by qny View Post
    The names used for some things is one of them (char, float, creat, qsort, ..., ..., ...).
    From the Wikiquote page for Ken Thompson (one of the creators of Unix):

    Ken Thompson was once asked what he would do differently if he were redesigning the UNIX system. His reply: "I'd spell creat with an e."

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by password636 View Post
    Some people say that there is no dedicated character type in C. 'char' is actually an integer type, then who is responsible for transforming an integer to a character? For example, printf() with '%c' format, does printf() transform the integer to a character?
    'C' does not do translation. It's the hardware. When a byte is sent to a printer or screen, it's up to that hardware to look up a pattern of ink dots to show the human readable letter. printf("%c", 65) will display 'A' because the hardware can display letters using a stored image of the appropriate letter. As far as the 'C" program knows, all it did is send a 1-byte (8-bit) decimal 65 someplace external.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  2. Replies: 2
    Last Post: 11-23-2011, 12:30 PM
  3. Replies: 17
    Last Post: 03-06-2008, 02:32 PM
  4. type char vs type int
    By stanlvw in forum C Programming
    Replies: 5
    Last Post: 12-10-2007, 11:04 AM
  5. Converting type string to type const char*
    By rusty0412 in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 05:59 PM