TIP: Click on subject to list as thread! ANSI
echo: rberrypi
to: PASCAL J. BOURGUIGNON
from: AHEM A RIVET`S SHOT
date: 2019-02-25 12:20:00
subject: Re: C is not a low level

On Mon, 25 Feb 2019 08:46:55 +0100
"Pascal J. Bourguignon"  wrote:

> So why C uses a null terminating byte for string literals?
> The alternatives are:

 I can only give you the answer Dennis Ritchie gave me. There were
a number of factors. Using a length indicator would have meant deciding on
a size for the length which would be a machine dependent size[1]. Whatever
size of length indicator you choose has issues - use a char and on most
machines that limits you to 256 character strings, use an int on a 64 bit
machine and no string is shorter than nine bytes long (think of all those
two letter commands in unix) - remember memory was expensive and wasting it
was not sensible. Using a null terminator meant that the only limit was the
machines address range and avoided any risk of making dangerous assumptions
about the maximum possible string size.

 Finally there is the important point that C was designed for
efficiency *not* safety - the implementation of strcpy from the first
edition of K&R is gloriously efficient and did not depend on you handing
pointers to the start of the string.

strcpy(d, s)
char *d;
char *s;
{
 while (*d++ = *s++);
}

 For a more dramatic example of efficiency consider strtok(), the
routine at the heart of turning a string into argc and argv. It works by
injecting nulls into the string where the spaces are and returning an array
of pointers to the starts of the words in the original string - In other
words it turns a single string into several strings and returns them in an
array - and it does so in place without using more memory than is
absolutely essential. Doing string manipulation in place with minimal
overhead was very important at the time C was designed.

 Yes this violates several modern principles - all of which are
about preferring safety to efficiency, which in these days of pocket super
computers is laudable but in the days of souped up adding machines with
memory would have been insanely impractical.

[1] remember in C we have char is no larger than int is no larger than long
and char is big enough to hold the character set required for C source and
sizes are in terms of char. Also think how much that would impact the parts
of the compiler code that you would really like to be machine independent.

--
Steve O'Hara-Smith                          |   Directable Mirror Arrays
C:\>WIN                                     | A better way to focus the sun
The computer obeys and wins.                |    licences available see
You lose and Bill collects.                 |    http://www.sohara.org/

--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)

SOURCE: echomail via QWK@docsplace.org

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.