fast code and no morals

Chris Torek chris at umcp-cs.UUCP
Tue Feb 4 14:54:38 AEST 1986


In article <231 at imagen.UUCP> SofPasuk at imagen.UUCP writes:
> "C" is as portable as Stonehedge!! 

 ... and more about C code making hidden assumptions.

C is portable:

	/* echo */
	#include <stdio.h>

	main(argc, argv)
		register int argc;
		register char **argv;
	{
		register int i;

		argc--;
		for (i = 1; i <= argc; i++)
			printf("%s%c", argv[i], i < argc ? ' ' : '\n');
		exit(0);
	}

C is not portable:

	/* probebus */
	main(argc, argv)
	{
		register int i;
		register int size = 1;

		if (strcmp(argv[1], "i") == 0)	/* by ints */
			size = 4;
		for (i = 1 << 18; i < (1 << 19); i += size) {
			switch (size) {
			case 1:
				if (*(char *) i != 0) {
					printf("%x\n", i);
					exit(1);
				}
				break;
			case 4:
				if (*(int *) i != 0) {
					printf("%x\n", i);
					exit(1);
				}
				break;
			}
		}
		exit(0);
	}
	/* unportable assumptions: sizeof (int) == sizeof (char *);
	   *(char *)0 == ""; ints hold > 16 bits; type casts can
	   be used pretty much arbitrarily. */

It is not the language that makes code portable, though it can
help or hurt:  Programmers make code portable, or do not.

Incidentally, it is `Stonehenge'.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list