OT: Trying to learn C -- some questions

Michael C. Shultz ringworm at inbox.lv
Thu Nov 25 19:03:21 PST 2004


On Thursday 25 November 2004 04:38 pm, Tom Parquette wrote:
> Hi.  This is a little off topic but I'm hoping someone can provide
> some guidance and answer a few questions.  TIA.
>
> I'm trying to learn ANSI C using a book circa 1994.  It is written
> from a DOS perspective.
> Someone at work, who knows a little C, told me that the book was
> "close enough".
> I'm having some trouble with some of the "homework" in the book.
> I think I know some of the answers but I would like to confirm my
> understanding.
> Some of the following I have no clue about.
>
> 1) gcc complains that <conio.h> was not found.  If I comment out the
> #include, the program compiles.  Is this a DOSism or something else?
>
> 2) fprintf is described with stdprn being valid for a default
> printer. This does not seem to be valid in, at least, the FreeBSD
> world.  man fprintf did not really help.  I believe I have to create
> a stream for the print but I'm not clear on how to do it.
>
I suggest you learn one function well, then expand from there. fprintf 
is a good place to start. 

man 3 fprintf has the following:

LIBRARY
     Standard C Library (libc, -lc)

means this command is in libc, 

lib is always stripped when you use -l to include a library
when you compile so -lc means include libc.
-lc is automatically included when you compile with gcc
so you usually don't need it.

SYNOPSIS
     #include <stdio.h>

that means include stdio.h when you use fprintf

fprintf(stdout, "hello\n") will print to the screen
and so will fprintf(stderr, "hello\n").  You can replace
stdout/stderr with a filestream for example:

sample code:

#include	<stdio.h>

FILE*	fileStream;
fileStream	= fopen( "hello.txt", "w");
fprintf( stdout,"hello\n");
fprintf( fileStream,"hello\n");
fclose(fileStream);

save as hello.c
compile with
gcc hellow.c -o hello
run and it will print "hello" to the screen and create a file
"hello.txt" with hello printed in the file.

Once your really comfortable with fprintf then look up the man
page and google the next command you want to learn.  Google
beats any programming books in my opinion once you learn to use it.

If you get stuck on one specific command, feel free to email me directly
and I'll give you a pointer if I know it.

-Mike


More information about the freebsd-questions mailing list