Re: Pascal vs C style string ? (Ciaran McHale)
Wed, 29 Jun 1994 11:26:03 GMT

          From comp.compilers

Related articles
[8 earlier articles]
Re: Pascal vs C style string ? (Stefan Monnier) (1994-06-28)
Re: Pascal vs C style string ? (Erkki Ruohtula) (1994-06-28)
Re: Pascal vs C style string ? (1994-06-28)
Re: Pascal vs C style string ? (1994-06-28)
Re: Pascal vs C style string ? (1994-06-28)
Re: Pascal vs C style string ? (1994-06-28)
Re: Pascal vs C style string ? (1994-06-29)
Re: Pascal vs C style string ? (1994-06-29)
Re: Pascal vs C style string ? (1994-06-30)
Re: Pascal vs C style string ? guerin@IRO.UMontreal.CA (1994-06-30)
Re: Pascal vs C style string ? synaptx!thymus! (Dave Gillespie) (1994-06-30)
Re: Pascal vs C style string ? (1994-07-01)
Re: Pascal vs C style string ? (1994-07-05)
| List of all articles for this month |

Newsgroups: comp.compilers
From: (Ciaran McHale)
Keywords: C, Pascal, design
Organization: DSG, Dept. of Computer Science, Trinity College Dublin
References: 94-06-214 94-06-224
Date: Wed, 29 Jun 1994 11:26:03 GMT

Erkki Ruohtula <> writes:
>I have too often seen careless C code like
>for (i = 0; i < strlen(very_long_string); ++i) {
> do_something_with(very_long_string[i]);
>For those who do not know C, this loop unnecessarily scans through
>the whole very_long_string at every iteration. It would be quite OK
>if the length-prefixed representation were used.

This is an example of "you can write poor code in any language". I think
that the careless C code you have seen is the fault of the programmers
rather than the language since any self-respecting C programmer should
know how to rewrite that loop to remove the ineffeciency you mention.


I used the p2c Pascal-to-C translator to help me port the code of a
compiler from Turbo Pascal (on a PC) to C so that I could use it on a UNIX
machine. _Overall_ I was happy with the generated C code but there were
some things that I was unimpressed with. One was the way in which it
translated Pascal string-manipulation into C string-manipulation. A
Pascal statement such as:

StringVar := StringVar + 'c'; { add a char to the end of the string }

was translated into C code like the following:

sprintf(StringVar, "%s%c", StringVar, 'c');

The problem here is a mismatch between how the two languages treat
strings. Since Paslcal strings are length-prefixed, Pascal programmers are
encouraged to manipulate strings in a particular manner that is efficient
for that type of string representation. However, this form of string
manipulation is quite inefficient when used on null-terminated strings as
in C. Thus a statement-to-statement source translator generates
inefficient code where string manipulation is concerned.

In case anyone is wondering in which part of the compiler this string
manipulation code appeared... It was in the lexical analyser. Appending
characters to the end of a string was used to construct the spelling of
identifiers and numbers. The UNIX/C port of that compiler probably had one
of the slowest lexical analysers ever seen in a compiler. I could have
rewritten that part of the generated code so that it manipulated strings
in a more efficient manner. However, since it was a research tool, speed
wasn't important and so I did not bother.

Ciaran McHale (
Dist. Systems Group, Department of Computer Science, Trinity College,
Dublin 2, Ireland. Telephone: +353-1-7021539 FAX: +353-1-6772204

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.