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

Laurel Fan laurel at sdf.lonestar.org
Mon Feb 25 11:25:09 EST 2002


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:

int main()
{
  printf("foo");
}

this perl code:

#!/usr/bin/perl
print "foo";

and cat'ing a file created in this way:

cat > foo
foo^D

does the same thing.

Basically, it does print the line, but without the \n, it leaves it on
the last line.  Depending on your shell/terminal, it may or may not
overwrite what is on the last line (if you had included a \n, the
stuff you printed would be on the second to last line, and there would
be nothing on the last line) with the prompt.  Some shells will go to
the beginning of the last line, or clear the last line, and then print
the prompt, and some will not (and some terminals will allow you to go
to the beginning of the line, and some will not).

If I have bash with no fancy prompt, it looks like this:

bash-2.05a$ ./foo
foobash-2.05a$ 

If I have zsh with a fancy prompt, it looks like this:

dreadnought> ./foo                                                            ~
dreadnought>                                                                  ~

Also try redirecting the output to a file, and looking at the file

./foo > outputfile

If you cat it, what happens?

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)?

> 2) In trying to do exercise 1, I wanted to use the sqrt function. I looked 
> at the (short) man page for it. This is the code I have:

[code snipped]

> On compiling, it barfs with this error:
> /home/mpm/tmp/ccU4dc3d.o: In function `main':
> /home/mpm/code/cprograms/ex6-1.c:26: undefined reference to `sqrt'
> collect2: ld returned 1 exit status
> 
> So what am I doing wrong?

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

THe long answer: Linking essentially tells the compiler (actually,
part of the compiler called the linker, which is called "ld", which is
why it complains about ld) where the functions you call but do not
implement are.  "-lm" tells the linker, "I need to link with the 'm'
library", and then it goes and looks for files called "libm.a", which
it will probably find in /usr/lib.

You don't have to do a -l thing for every function you call but do not
implement, because some functions, like printf, are in the libc (the C
library), which is automatically linked in.

Even though you #include'd math.h, all that contains are the function
prototypes, not the implementations; it just tells the compiler what
the functions look like, so it can compile your code before linking.
(If you look at the file /usr/include/math.h, you can see what you're
including.  The code is sort of hard to understand though.)

-- 
laurel at sdf.lonestar.org
SDF Public Access UNIX System - http://sdf.lonestar.org



More information about the Courses mailing list