char

This is a discussion on char within the C++ Programming forums, part of the General Programming Boards category; Hello, I would like to know what char does and is. I know it is a variable, used to hold ...

  1. #1
    K&J
    K&J is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    Question char

    Hello,
    I would like to know what char does and is. I know it is a variable, used to hold a value that are assigned to it. Although in the book I am useing it says, "if "a" is an int, "b" is a double, and "c" is a char, what is the type of value returned by the expression a + c?

  2. #2
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    Well, since a is an integer, and c is a character, where character is represented by its integer value the expression of a+c would give the integer value of a plus the integer value of the character stored in c.

    Hope this helps.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  3. #3
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    Example:

    a = 10;
    c = 'A'; // integer value of A is 65


    b = a + c; // here 'b' = 75 = 65+10

    Cheers,
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  4. #4
    Unregistered
    Guest
    Lte's say that I am a supernatural mathematician. I can do things with numbers faster than most people can believe. However I can't speak, recognize letters, etc. How could you, or anyone else, communicate with me?

    One way would be to agree on a mathematical code that substitutes a numeric value for each letter. Let's say that capital A was represented by the integer 0 and each successive capital letter was one integer higher. Lower case letter a would be 27 and each successive lower case letter would be one integer higher. Then we could do the same thing for other symbols like the numbers 0-9, the various punctuation marks, and even other symbols like dollar signs, asterisks, mathematical signs for addition, subtraction; really just about anything we could think of. Now we can convert back and forth without any trouble using this conversion process.

    Now let's take it one step more. Suppose I can deal only with binary, not decimal numbers. No big deal really, just convert all decimal numbers into binary and back again as needed. Kinda cool, actually.

    Turns out that's not too far off from what happens with the computer. The memory area's within the computer can be viewed as a long series of mailboxes, each of which can be found at a very specific location, or address, and each of which can have either one of two states, say maybe full or empty, or you might prefer on or off, or maybe right or left, or maybe even zero or one. Whatever, doesn't really matter. The point is that these two states can be thought of as the equivalent of the zero or one used in binary math. Hence you could use a given sequence of memory to represent binary numbers. All you have to do is agree that a given amount of memory will be used to store a given piece of information. The word bit is often used to mean a given "mailbox" in memory. It represents the smallest piece of useable information. Eight bits represent a byte. This means that if you let each char represent eight bits (or one byte) of information you could store up to 256 (that is 2^8) different chars, which is plenty for most of the symbols used in routine english (and many other languages). It turns out large groups of people have agreed to accept a given conversion table, called ASCII character set, that converts english characters/symbols into decimal and binary equivalents so they can manipulate the pattern of storage in the computer to do useful things. Thus each time you type a given character on the keyboard the computer changes it into a decimal and a binary number and does something with it. This means you can actually do crazy things like adding 3 + Z to get 91 (or some such number) or something like C++ (which gives D), even. Each char has a given decimal and binary equivalent there for you to manipulate if you wish. Often we don't bother, but some people can do some pretty funky things by twiddling the bits underlying the chars/symbols, etc you and I routinely use.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    "if "a" is an int, "b" is a double, and "c" is a char, what is the type of value returned by the expression a + c?
    By the rules of integral promotion the expression evaluates to an int.
    - lmov

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I would like to know what char does and is. I know it is a variable, used to hold a value that are assigned to it.
    I actually screwed you up on this a couple days ago. I tried to clear it but was not specific. A char is not a variable, but instead it is a 'data type'. Furthermore it is a 'primitive' or 'built in' data type. It is used to define an instance of an object or in simple terms a variable. The variable can than store data values according to its data type.

    The statement you are asking for is incomplete. A statement requires an lvalue.
    Code:
    lvalue assignment operator  operand  math operator  operand
    ?               =               a           +             c;
    The result will be influence by the lvalue. You didn't state any lvalue, but the right hand side will evaluate into an integer. People explained that part.
    Last edited by Troll_King; 10-24-2001 at 03:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 10:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21