EmbeddedRelated.com
Forums

Vary size of char from 1 to 2

Started by RahulS January 10, 2006
Hi,
I am using ARM Developer Suite by Codewarrior on Hynix processor with
an ARM7 core.
I want to configure size of few datatypes.
e.g.: I want to configure the size of char as 2 or 4 accordingly.
Is it possible using the complier features? I was unable to find
anything in the complier menu.
Kindly guide me through this.
Regards
RahulS

RahulS wrote:
> Hi, > I am using ARM Developer Suite by Codewarrior on Hynix processor with > an ARM7 core. > I want to configure size of few datatypes. > e.g.: I want to configure the size of char as 2 or 4 accordingly. > Is it possible using the complier features? I was unable to find > anything in the complier menu. > Kindly guide me through this. > Regards > RahulS >
What is it you are actually trying to achieve here? In C, sizeof(char) is always 1 (by definition), and it is always the smallest size the target can directly access. Since the ARM can directly access 8-bit bytes, that is the size of a char. The most that you can do with most compilers is chose whether the default is signed or unsigned, although if your code depends on that setting, your code is badly written. Trying to write C on architectures with chars greater than 8-bit is a PITA. It's not something one would chose voluntarily.
RahulS <rahuls@kpitcummins.com> wrote:

> e.g.: I want to configure the size of char as 2 or 4 accordingly.
No way. If you're very lucky, you may manage to change the number of bytes occupied by a char, but you cannot ever possibly be allowed to change the size of it. If you managed to get sizeof(char)!=1, that beast you created would no longer be a C compiler. Anyway: what on earth do you hope to achieve by this? -- Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain.
On 2006-01-10, David Brown <david@westcontrol.removethisbit.com> wrote:

> In C, sizeof(char) is always 1 (by definition), and it is > always the smallest size the target can directly access.
Not always. It's the smallest size the target can address that is larger than 7 bits. Or something like that. On a machine that can directly address 1 bit, a char is not allowed to be 1 bit.
> Since the ARM can directly access 8-bit bytes, that is the > size of a char. The most that you can do with most compilers > is chose whether the default is signed or unsigned, although > if your code depends on that setting, your code is badly > written. > > Trying to write C on architectures with chars greater than > 8-bit is a PITA.
It's not that bad. You've got to pay attention to what you're doing, but no more so than is normally required in an usafe language like C.
> It's not something one would chose voluntarily.
Writing C is not something one would choose voluntarily. ;) -- Grant Edwards grante Yow! I'm sitting on my at SPEED QUEEN... To me, visi.com it's ENJOYABLE... I'm WARM... I'm VIBRATORY...
On 10 Jan 2006 06:25:45 -0800, "RahulS" <rahuls@kpitcummins.com>
wrote:

>Hi, >I am using ARM Developer Suite by Codewarrior on Hynix processor with >an ARM7 core. >I want to configure size of few datatypes. >e.g.: I want to configure the size of char as 2 or 4 accordingly.
Are you trying to use Unicode characters ? At least in C99 this might be possible using <wchar.h> if it contains sensible definitions. Paul
On 10 Jan 2006 16:38:03 +0100, David Brown
<david@westcontrol.removethisbit.com> wrote in comp.arch.embedded:

> RahulS wrote: > > Hi, > > I am using ARM Developer Suite by Codewarrior on Hynix processor with > > an ARM7 core. > > I want to configure size of few datatypes. > > e.g.: I want to configure the size of char as 2 or 4 accordingly. > > Is it possible using the complier features? I was unable to find > > anything in the complier menu. > > Kindly guide me through this. > > Regards > > RahulS > > > > What is it you are actually trying to achieve here? > > In C, sizeof(char) is always 1 (by definition), and it is always the > smallest size the target can directly access. Since the ARM can > directly access 8-bit bytes, that is the size of a char. The most that > you can do with most compilers is chose whether the default is signed or > unsigned, although if your code depends on that setting, your code is > badly written. > > Trying to write C on architectures with chars greater than 8-bit is a > PITA. It's not something one would chose voluntarily.
Not that bad, you just have to be careful. I've been doing it on a TI motor control DSP for quite some time now. Char, short, and int are all 16 bits, long is 32. Did some work in the past on an Analog Devices 32-bit DSP where char, short, int, and long were all 32 bits. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://c-faq.com/ comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
I am using M68AW512M RAM.It has 2 pins to read/write the upper or lower
byte. When i try to write a lower byte(char) onto this by enabling the
lower byte pin, it writes that to both lower as well as higher byte and
vice-versa.
When i try to write a single word, it writes it correctly on both the
lower and upper bytes.
Problem is only when i try writing a byte or in other words a ' char '.
That is why, i was trying to change the size of char from a byte to 2
bytes.

Can you suggest me some other way to overcome this problem.
Thanks
RahulS

I am using M68AW512M RAM.It has 2 pins to read/write the upper or lower
byte. When i try to write a lower byte(char) onto this by enabling the
lower byte pin, it writes that to both lower as well as higher byte and
vice-versa.
When i try to write a single word, it writes it correctly on both the
lower and upper bytes.
Problem is only when i try writing a byte or in other words a ' char '.
That is why, i was trying to change the size of char from a byte to 2
bytes.

Can you suggest me some other way to overcome this problem.
Thanks
RahulS

I am using M68AW512M RAM.It has 2 pins to read/write the upper or lower
byte. When i try to write a lower byte(char) onto this by enabling the
lower byte pin, it writes that to both lower as well as higher byte and
vice-versa.
When i try to write a single word, it writes it correctly on both the
lower and upper bytes.
Problem is only when i try writing a byte or in other words a ' char '.
That is why, i was trying to change the size of char from a byte to 2
bytes.

Can you suggest me some other way to overcome this problem.
Thanks
RahulS

Grant Edwards wrote:
> On 2006-01-10, David Brown <david@westcontrol.removethisbit.com> wrote: > >> In C, sizeof(char) is always 1 (by definition), and it is >> always the smallest size the target can directly access. > > Not always. It's the smallest size the target can address that > is larger than 7 bits. Or something like that. On a machine > that can directly address 1 bit, a char is not allowed to be 1 > bit.
Yes, that's true - a char must be at least 8 bits. But as sizeof(char) is always 1, and all other types are defined as multiples of char, C itself is unable to directly address anything smaller even if the target architecture supports it. Unless, of course, your compiler has non-standard extensions...
> >> Since the ARM can directly access 8-bit bytes, that is the >> size of a char. The most that you can do with most compilers >> is chose whether the default is signed or unsigned, although >> if your code depends on that setting, your code is badly >> written. >> >> Trying to write C on architectures with chars greater than >> 8-bit is a PITA. > > It's not that bad. You've got to pay attention to what you're > doing, but no more so than is normally required in an usafe > language like C. >
It's an extra thing to keep in mind. For many tasks, it makes little difference, but when trying to manipulate character-based telegrams using the least possible ram space it is very much in the way. It certainly leads to unportable code (you don't want code filled with byte extraction functions if you don't need it!).
>> It's not something one would chose voluntarily. > > Writing C is not something one would choose voluntarily. ;) >