Thread: char vs. int

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    2

    char vs. int

    I was wondering whether it would be better to store and use data as char or int, since char uses only 8 bits and you could do stuff like say 'char a = 0x0F;' and then write it to a file, writing specific binary data to store info, or even use it as variables for arithmetic that only required smaller containers as opposed to using 32 bit int. However, I have also read that on 32-bit or 64-bit machines using smaller containers actually causes the program to run slower since it reads and writes in 32-bits, so maybe using an int type would be more efficient?

    Basically it comes down to two questions, first is it better to use char in a program as an 8-bit number instead of using a larger int variable when 2^32 values are not needed or is nothing saved like this?

    Second, is it better to store data into char variables and write to files in this form, or is it faster to use int 32-bit data type?

    Thanks for any input,
    Brian

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Reading and writing values from memory are fastest when done as words (32 bits for 32 bit machines). However, if you are talking about arrays of values (such as bytes), the machine will read a word's worth of values in one chunk and then monkey with them in the registers to isolate the value. Register activity is very fast and even if there are not enough registers to hold all the bits of the values, the remaining ones should be in a local data cache making the subsequent retrieval much faster than the initial retrieval. You should never worry about performance until you have a completely working, fully debugged program. If you like, you can read a bit on performance programming I wrote: http://sol-biotech.com/code/PerformanceProgramming.html.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    2
    Thanks, I'll take a look at that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM