tell me about a 'char'

This is a discussion on tell me about a 'char' within the C Programming forums, part of the General Programming Boards category; I've read lately that a 'char' is actually a small 'int' in C wich made a few questions pop in ...

  1. #1
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472

    tell me about a 'char'

    I've read lately that a 'char' is actually a small 'int' in C wich made a few questions pop in my head.

    Q1 :
    Does that mean i can assign 'char's to 'int's (and vice versa) without casting? i know it works , but i thought maybe its not recommended for some reason. And why shouldn't we use a 'char' to hold the return value of 'getchar()'? wouldn't it turn into a 'char' automatically anyway?


    Q2 :
    compilers tend to look up the ASCII table (in systems that use ASCII) , right? (just making sure)

    Q3 :
    Talking about tables , can this simple procedure determine wether a system is using ASCII or EBCDIC to represent characters? :
    Code:
    char ch = 110;
    
    if(ch == 'n')
        /* process characters as ASCII */
    else if(ch == '>')
        /* process characters as EBCDIC*/


    appreciate your help
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Brain Cell
    Q1 :
    Does that mean i can assign 'char's to 'int's (and vice versa) without casting? i know it works , but i thought maybe its not recommended for some reason. And why shouldn't we use a 'char' to hold the return value of 'getchar()'? wouldn't it turn into a 'char' automatically anyway?
    The char is a numeric integer data type. Kernighan and Ritchie state in their book The C Programming Language: Second Edition that, "By definition, chars are just small integers, so char variables and constants are identical to ints in arithmetic expressions." Also, a char is specifically meant for storing character data, but any integer type can be used as well as char, for the purpose of storing character data. This does NOT mean that chars and ints are interchangeable in all things.

    As far as assigning the return value of getchar() to a char - that is a big no no. Consider that when you obtain data from the user, there has to be a suitable method of distinguishing between valid data, and the end of the file (ie, no more input). The function getchar() does return a special value when the end of the input is reached, and this value is called EOF (or end of file). EOF is defined in <stdio.h> and must be a value that will not be mistaken as a useful character that was entered by the user. At this point it is not necessary to worry about the actual value of EOF - that has already been taken care of when your compiler was put together - but here is where the importance of assigning the return value of getchar() to an int becomes clear; The value of EOF is an integer that uses more bits than what is available in a char variable. Remember that EOF must be a value that will not be mistaken as normal input, and it is for that reason, that it is a value outside of the bounds of a normal char variable, 0-255. This then, is the reason why you assign the return value of getchar() to an int in order to make room for any character returned or EOF if necessary.

    But to go further, the function prototype for getchar() is like this:

    Code:
    int getchar(void);
    So we can see that getchar() returns an int.

    ~/
    Last edited by kermit; 02-19-2005 at 10:25 AM.

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Thanks alot kermit , that was a great explanation

    how about the first part of the question (assigning char to int and int to char without casting)?
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Quote Originally Posted by Brain Cell
    Thanks alot kermit , that was a great explanation

    how about the first part of the question (assigning char to int and int to char without casting)?
    Kermit answered most of that in his first paragraph. chars and ints are the same type of data, just different sizes. The compiler knows what to do when you move a char to int (clear the high portion of the new int) and moving an int to char (throw out the high portion).

    Write a program to see what happens when you assign an int to char and the int value is larger than a char value (127 is the maximum a char can hold). Also, output the values in HEX to see what the bit patterns look like
    Code:
    printf("%04X ", intval);
    will show you interesting stuff. Don't forget to try negative numbers, especially going from char to int.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Thanks Walt , i tried it with this :
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char charvar = 128;
    	int intvar = 128;
    
    	printf("%X\n", intvar);
    	printf("%X\n", charvar);
    
    	return 0;
    }
    it shows the int as FFFFFF80 while the cahr is just 80 , i got the idea now thanks


    I wonder if my other questions were too hard to get an answer , i thought they were simple
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

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