[Courses] Reminder, and questions (and semi spoiler on ex 6-1)

Michelle Murrain tech at murrain.net
Mon Feb 25 15:36:55 EST 2002


At 01:25 PM 2/25/2002, Laurel Fan wrote:
>On Mon, Feb 25, 2002 at 08:54:21AM -0500, Michelle Murrain wrote:
> >   I have two questions for the gurus:
> >
> > 1) Why is forgetting the "\n" at the end of a printf statement suppress 
> the
> > line altogether? ( in perl, it will simply print the line w/o a newline).
>
>It has to do with what your terminal and shell does with a line with
>no \n.  With the same terminal and shell, this C code:

<snip>

Thanks! That was the issue.

>Not entirely C-related exercise for the class: How can you tell what's
>in a file if you can't cat it (because the last \n-less line is
>overwritten by the prompt)?

I'd just open it up in a text editor. (lazy answer)

>You need to link it with the math library.  The short answer is that
>you need to add "-lm" to your gcc command line.

Thanks to everyone who gave me this answer.

Now the code compiles fine, but after I input the first pair of variables, 
and before it asks for the second, it segfaults!

Code again (a bit different):

#include <stdio.h>
#include <math.h>

/* program to find the distance between too points */

int x1,x2,y1,y2;
char line[20];
int distance_squared;
double distance;

int main()
{
   printf("Point 1(x,y):");
   fgets(line, sizeof(line), stdin);
   sscanf(line, "%d,%d",&x1,&y1);

   printf("Point 2(x,y):");
   fgets(line, sizeof(line), stdin);
   sscanf(line, "%d,%d",&x2,&y2);

   printf("Here are the points:%d,%d and %d,%d\n",x1,y1,x2,y2);

   distance_squared = ((x2-x1)^2) + ((y2-y1)^2);

   distance = sqrt(distance_squared);

   printf("Square of distance between two points: %d Actual distance: 
%f\n",distance_squared,distance);

   return(0);
}

Very, very strange behavior:

After doing some experimentation, it seems that it segfaults when it is 
trying to put a value in the variable y1. Is y1 reserved? When I try to 
initialize y1 by saying:
int y1 = 0;

I get this error:/usr/bin/ld: Warning: type of symbol `y1' changed from 2 
to 1 in /home/mpm/tmp/cc4lBMzm.o

When, instead of asking for y1 I just assign it's value:

y1 = 23;

It segfaults.

And, the clincher - replace the variable name y1 with yy1, and it all works 
just fine, thank you. So is y1 a reserved word??? What am I missing?

.Michelle

---------------------------------------
Michelle Murrain
tech at murrain.net
AIM/Yahoo Messenger:pearlbear0
http://www.murrain.net/ for pgp public key





More information about the Courses mailing list