Re: Portable and ok?
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Thu, 03 Nov 2005 00:32:18 GMT
Jordan Abel <jmabel@xxxxxxxxxx> writes:
> On 2005-11-02, Keith Thompson <kst-u@xxxxxxx> wrote:
>> Jordan Abel <jmabel@xxxxxxxxxx> writes:
>>> On 2005-11-02, Simon Biber <news@xxxxxxxxx> wrote:
>>>> - the teacher said to *never* use literal values
>>>>
>>>> - thought it possible that null may not be zero, better use
>>>> the macro to be sure
>>>
>>> in either of those two cases, the appropriate way would be '\0'
>>
>> Um, '\0' is a literal value.
>
> So is the string he has with the hex digits - there's very little
> you can do without literal values of any kind - i think the most
> complicated program you can implement is echo.
Aha, a challenge!
Without using literals, you can easily construct constant expressions
for any integer you like:
static int dummy;
#define ZERO (&dummy!=&dummy)
#define ONE (&dummy==&dummy)
#define TWO (ONE+ONE)
#define THREE (TWO+ONE)
#define FOUR (THREE+ONE)
Character and string literals are a bit more difficult. You can
construct anything you like if you're willing to assume an ASCII
encoding, but that's cheating. I think you can determine the values
of '0' through '9' by looping over the range 0 .. SCHAR_MAX and
checking isdigit(). You can do the same thing for letters by checking
isupper() and islower(); this works for both ASCII and EBCDIC, but not
for any character sets in which the letters aren't in alphabetical
order. (The results won't be constant expressions unless you go to a
*lot* of extra effort.)
fopen() is difficult because the mode argument is usually a string
literal. It doesn't have to be a constant, but I can't think of a
good way to generate an 'r' or 'w' character.
printf(), of course, requires a format string. It can be built up
manually if you can construct the required characters.
Just to be clear (and because I've criticized others for not including
this disclaimer), this is absolutely nothing more than a silly little
intellectual exercise, of no actual use that I can think of. And I
haven't had a homework assignment in decades.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- Follow-Ups:
- Re: Portable and ok?
- From: tedu
- Re: Portable and ok?
- From: Jordan Abel
- Re: Portable and ok?
- References:
- Portable and ok?
- From: pemo
- Re: Portable and ok?
- From: Martin Ambuhl
- Re: Portable and ok?
- From: Simon Biber
- Re: Portable and ok?
- From: Jordan Abel
- Re: Portable and ok?
- From: Keith Thompson
- Portable and ok?
- Prev by Date: Re: How is a compiler written
- Next by Date: Re: Portable and ok?
- Previous by thread: Re: Portable and ok?
- Next by thread: Re: Portable and ok?
- Index(es):
Relevant Pages
|