[comp.lang.c] Portability help

davek@lakesys.UUCP (Dave Kraft) (02/13/89)

Hi,
I am taking a C class in school this semester, and the instructor gave us the
following example:

#include <stdio.h>          /* Please note:  I used <dos/stdio.h> in the unix
			       environment */
main()
{
	char a, b, c, d, e;
	char whats_left[20];
	int x;

	printf("\nKey in a bunch of characters like 1234...9cr:  ");
	scanf("%c%c%c", &a, &b, &c);
	printf("\nYou typed in >>%c%c%c<<", a, b, c);
	scanf("%c%c", &d, &e);
	printf("\nYou had these left over >>%c%c<<", d, e);
	putc('\n', stdout);
	scanf("%s", whats_left);
	printf("\n>>%s<<", whats_left);
   	printf("\n>>%s<<", &a);
	x = getchar();
	if(x == EOF)
		printf("\nInput stream empty.");
	else;
		printf("\nGot back >>%d%x<<", x, x);
}

I have entered and compiled this on Turbo C 1.5, and it runs like it should.
I enter:  123456789
and the first thing it prints is the >>123<<, then >>45<<, then >>6789<<,
then >>123456789<<, and I get 'Got back >>10a<<'

On the Unix system I enter the same numbers, and I get >>123<< >>45<< >>6789<<
and here's one of the kludges:  >>126789<<, and for 'Got back', it shows
>>00<<..  Why?

Also, when I try to re-direct as follows:
	1) example > output
	   123456789  (<-- what I type in after (1))
	   then I get a core dump, and a message (do not remember exactly,
	   will redo and post)
	2) example < numlist > output  (numlist contains 123456789)    
    	   I get the same core dump as in (1).  Why??
Any help would be appriciated.
    
    
Dave
  );
-- 
davek@lakesys.lakesys.com
uunet!marque!lakesys!davek

marcoz@MARCOZ.BOLTZ.CS.CMU.EDU (Marco Zagha) (02/13/89)

In article <376@lakesys.UUCP> davek@lakesys.UUCP (Dave Kraft) writes:

[...]
> main()
> {
>        char a, b, c, d, e;
> 	
>        [...]
>
> 	printf("\n>>%s<<", &a);
>	[...]
> }

As far as your core dump problem goes, you are trying to treat the
address of a single character as a string.  A string (in the
printf sense) is a pointer to a NULL TERMINATED string of characters.
You should be using the %c specifier as in other parts of your code.

== Marco (marcoz@cs.cmu.edu)
-- 

maart@cs.vu.nl (Maarten Litmaath) (02/14/89)

davek@lakesys.UUCP (Dave Kraft) writes:
\...
\	char a, b, c, d, e;
\...
\   	printf("\n>>%s<<", &a);
                           ^^
You're telling printf() to expect a string, i.e. the address of the first of a
sequence of bytes, TERMINATED BY A '\0' BYTE; instead you supply printf() with
the address of 1 char. Use `%c' and delete the `&'.

\...
\	if(x == EOF)
\		printf("\nInput stream empty.");
\	else;
            ^
Probably you didn't intend that.
-- 
 "I love it                            |Maarten Litmaath @ VU Amsterdam:
          when a plan comes together." |maart@cs.vu.nl, mcvax!botter!maart

pathak@s.cs.uiuc.edu (02/15/89)

Your professor seems to be assuming that the complier will initialize each
var in consecutive memory location. TC1.5 did that so:

printf("%s",&a); gave you 123456789.  However  the Unix machine didn't 
use consecutive memory locations so you got the kludge.

The reason Turbo gave you a 10a for an answer is that it read in your 
return key.  Why it did this is a mystery.  The Unix version worked 
correctly because it gave you a null which is the proper terminated of 
an input stream.

Heeren Pathak
s.cs.uiuc.edu
zaphod.ncsa.uiuc.edu

pathak@s.cs.uiuc.edu (02/15/89)

Oh one more thing, I hope your instructor gave this as an example so 
NON PORTABLE code because it will give you different results every time you
use switch compliers.

Heeren Pathak
s.cs.uiuc.edu
zaphod.ncsa.uiuc.edu