Example Program: a[i]=b[i++] non-portability

Chris Koenigsberg ckk at g.cs.cmu.edu
Tue Apr 8 08:32:31 AEST 1986


I mentioned (article title "*p++ = *p and more")
that the PCC's on the Sun2 and the IBM RT PC treated "a[i]=b[i++]"
differently, and some people said I must have been mistaken. I dug up an
example program which displays the non-portability by printing different
 results on the two machines. Try this on yours and see what you get!
Here is a short program displaying the problem:
*************
/* testing certain implementation-dependent features of compilers.
*/
#include <stdio.h>
main(argc, argv)
	{char a[10];static char b[]="abcdefghij";int i,j;
	printf("a =%s, b=%s\n", a, b);
	printf("Looping over i...");
	while(b[i] != 0) a[i] = b[i++];
	printf("New a =");
	for(i=0;i<=10;printf("%c",a[i++]));printf(", b=%s\n",b);
	fflush(stdout);
	}
**************
On a sun, the output looks like:
a =, b=abcdefghij
Looping over i...New a =abcdefghij^@, b=abcdefghij
**************
And on the IBM RT PC, the output looks like:
a =, b=abcdefghij
Looping over i...New a =^@abcdefghij, b=abcdefghij
**************
Note: The "^@" was actually a null character, in the output.  
On the sun, a[0] gets b[0], but on the RT PC, a[1] gets b[0] and a[0] is
a null.

Chris Koenigsberg
ckk at g.cs.cmu.edu , or ckk%andrew at pt.cs.cmu.edu
{harvard,seismo,topaz,ucbvax}!g.cs.cmu.edu!ckk
(412)268-8526
Center for Design of Educational Computing
Carnegie-Mellon U.
Pgh, Pa. 15213



More information about the Comp.lang.c mailing list