Re: Portable and ok?



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.
.



Relevant Pages

  • Re: [Full-Disclosure] TinyURL
    ... Tinyurl will store that long link, ... > It appears that the last four letters are incremented one letter at a ... > one character in the url and came across a link to a kiddie porn site... ... > that types in the string. ...
    (Full-Disclosure)
  • RE: [Full-Disclosure] TinyURL
    ... Tinyurl will store that long link, ... > It appears that the last four letters are incremented one letter at a ... > one character in the url and came across a link to a kiddie porn site... ... > that types in the string. ...
    (Full-Disclosure)
  • Re: coding an anagram function
    ... Any reason you cannot use the ASCII values of each character? ... contents are anagrams of each other. ... The cells will only contain letters, and no letter will appear more ... For this to produce a unique result for any given string, ...
    (microsoft.public.excel.programming)
  • Re: String/isUpperCase?
    ... >and counts the Upper Case letters. ... I tried isUpperCase but it was only doing ... >a character not the whole string. ...
    (comp.lang.java.help)
  • [TOMOYO #15 3/8] Common functions for TOMOYO Linux.
    ... This file contains common functions (e.g. policy I/O, pattern matching). ... Since TOMOYO Linux is a name based access control, ... TOMOYO Linux's string manipulation functions make reviewers feel crazy, ... the Linux kernel accepts all characters but NUL character ...
    (Linux-Kernel)