Thread: Change size of a char

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154

    Change size of a char

    Is it possible to change the size of a char ?

    For example, a char has size of one byte. Is it possible using preprocessor or a gcc flag to make it, let's say two bytes or anyway the size of char will always be 1 byte ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Big Brother says that a char has the size of two bytes. Therefore, a char has the size of two bytes. Even if sizeof(char) always returns 1, the size of a char is two bytes. 2 + 2 == 5 evaluates to 1.

    Okay, why do you want to do this? The C standard defines sizeof(char) == 1. If this can be changed... then everything should be scaled according to what sizeof(char) has been changed to. I do not see any benefit.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Quote Originally Posted by laserlight View Post
    Big Brother says that a char has the size of two bytes. Therefore, a char has the size of two bytes. Even if sizeof(char) always returns 1, the size of a char is two bytes. 2 + 2 == 5 evaluates to 1.
    The more you live, the more you learn. I didn't know that. Thanks
    How sizeof returns 1 ? Is there any excuse ?

    Quote Originally Posted by laserlight View Post
    Okay, why do you want to do this? The C standard defines sizeof(char) == 1. If this can be changed... then everything should be scaled according to what sizeof(char) has been changed to. I do not see any benefit.
    I can't see any benefit too, but i had a conversation about if that change can be done.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ch4
    How sizeof returns 1 ? Is there any excuse ?
    Well, the returning part is part of the joke: sizeof is an operator, not a function, so to say "sizeof(char) always returns 1" is inaccurate. That said, one possibility why sizeof(char) == 1 is that all the standard integer types have sizes that are larger than char, except for unsigned char and signed char, both of which have sizes equal to that of char. Therefore, if sizeof(char) is not 1, then there is no standard integer type whose size is exactly 1 byte.

    But of course, "Big Brother" could represent a compiler author who wishes to do whatever he/she wants, ignoring the standard in the process. If you obey Big Brother, then whatever happens is up to Big Brother, and we really cannot advise you as to what might happen (e.g., what if Big Brother insisted that INTERCAL is C?)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This question doesn't make much sense. Perhaps you want to use a larger integer type? Or do you want to store strings (char arrays)?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Quote Originally Posted by laserlight View Post
    Well, the returning part is part of the joke: sizeof is an operator, not a function, so to say "sizeof(char) always returns 1" is inaccurate. That said, one possibility why sizeof(char) == 1 is that all the standard integer types have sizes that are larger than char, except for unsigned char and signed char, both of which have sizes equal to that of char. Therefore, if sizeof(char) is not 1, then there is no standard integer type whose size is exactly 1 byte.

    But of course, "Big Brother" could represent a compiler author who wishes to do whatever he/she wants, ignoring the standard in the process. If you obey Big Brother, then whatever happens is up to Big Brother, and we really cannot advise you as to what might happen (e.g., what if Big Brother insisted that INTERCAL is C?)
    I see.

    //Then, how would be the "Hello World" according to "Big Brother" ?

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ch4 View Post
    I can't see any benefit too, but i had a conversation about if that change can be done.
    People can have conversations about anything they want, BUT:

    If by this you mean, "Are there any standard commands or sequences of standard commands I can issue in a C program to change the size of char or any other standard datatype?" the answer is NO, not if you are using a standards compliant C compiler.

    If you are using a compiler which does allow such a thing, it would have to include some new non-standard command(s) to do it, which anything is possible that way, but it is unlikely that it is referred to as a "C compiler" and it would be an untruth to call this new language "C", altho it may also compile normal C programs. In reality this compiler does not exist, and it is very unlikely that someone will create one, either, whether the author goes by the name of "Big Brother" or "Twisted Sister"
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Generic heapsort
    By Sephiroth1109 in forum C Programming
    Replies: 15
    Last Post: 12-07-2007, 06:14 PM
  5. Replies: 11
    Last Post: 03-25-2003, 05:13 PM

Tags for this Thread