Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Char variables

  1. #1
    Regular Coder
    Join Date
    Nov 2003
    Location
    Code Heaven
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Char variables

    Variables of type char can be assigned values in these ways:
    Code:
    char x='a';
    or
    char x=65;
    Why should I use the second form,and why are the benefits from using it?
    Can I do things like:
    Code:
     char x=543454;
    ?

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    chars are usually one byte or two so something like:
    Code:
    char x=543454;
    will probably give you an out of bounds error or some unexpected results (depending on the language/platform you use) because the number is out of range.

    But basically, as far as the computer knows, characters are the same as numbers. There are tables that do the conversion. 'a' and 65 are the same thing for the machine.

    Why should you use the second form? If the variable is actually representing characters then you probably shouldn't since it would make the code less clear. But many people use chars when they know a number will fit in their range and they want to save some memory. For example, to represent a bitmap with 8-bit color depth, you'll probably want to use an array of chars.

    shmoove


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •