Learning process

  • Thread starter Karsten Kvistad
  • Start date
K

Karsten Kvistad

Hi!
I'm currently teaching myself the C programming language. The problem is
I don't know what to do next. I know file I/O, pointers, functions and
the like. I do not know how to make .h files, Makefiles or anything else
required for large projects. Should I learn how to do this, sockets,
GTK, curses or what?
 
J

Joona I Palaste

Karsten Kvistad said:
Hi!
I'm currently teaching myself the C programming language. The problem is
I don't know what to do next. I know file I/O, pointers, functions and
the like. I do not know how to make .h files, Makefiles or anything else
required for large projects. Should I learn how to do this, sockets,
GTK, curses or what?

Well, .h files are syntactically identical to .c files. The only thing
that is usually different is that .h files should not contain any actual
executable code, only definitions.
Makefiles are not really a C concept. Make is a tool for compiling or
building just about anything. I've used it to make LaTeX documents. You
don't have to use make to compile C. Especially single source file
programs can be compiled just fine with one compiler call.
If I were you I'd look into .h files and makefiles first. Sockets, GTK
and curses are not part of C, they're platform-specific add-ons. It's
important to understand the language itself before tackling those.
 
R

Richard Heathfield

Karsten said:
Hi!
I'm currently teaching myself the C programming language. The problem is
I don't know what to do next.

One thing you might want to do before too long is to check that your teacher
knew what he was talking about. :)

This newsgroup can help you do that.
I know file I/O, pointers, functions and
the like. I do not know how to make .h files,

Most people use a text editor for this. More importantly for you to
discover: what /is/ a header?

Answer: a header is an interface description for a module. Now that you know
that, what do you think belongs in a header?
 
K

Karsten Kvistad

Richard said:
Most people use a text editor for this.

I figured so... I meant that I don't know what should go into them.

If this was a joke, I apologise for my lack of insight :p
 
J

Joona I Palaste

I figured so... I meant that I don't know what should go into them.
If this was a joke, I apologise for my lack of insight :p

You figured correctly, they are made with a text editor. See my previous
post for an answer to your additional question. Basically, put the
following in .h files:

* Macro definitions
* Structure and union definitions
* Typedefs
* Extern function or variable declarations

....and *very* little else. Especially, do NOT put any real definition
of a variable or function, that would actually take up space in the
object file. That would only cause linking problems if you happen to
#include the same .h file from two .c files.
 
M

Morris Dovey

Karsten said:
Hi!
I'm currently teaching myself the C programming language. The problem is
I don't know what to do next. I know file I/O, pointers, functions and
the like. I do not know how to make .h files, Makefiles or anything else
required for large projects. Should I learn how to do this, sockets,
GTK, curses or what?

Karsten...

In brief, header (.h) files are repositories for information
about code and data in your translation unit that you choose to
make available to other translation units.

Generally this information consists of declarations (not
definitions!) and descriptive comments to help others understand
what and why you've included in the header.

Makefiles, sockets, GTK, curses, and any 'what' that aren't
actual elements of the C language can and should be addressed in
other newsgroups. My <off-topic> suggestion would be to learn to
use the make utility as early as possible - doing so may
facilitate your efforts with all of the subjects you seem
interested in investigating. </off topic>

The "what to do next" answer will be highly dependent on your
personal interests. You're on a voyage of discovery - look
around, explore, investigate, take time to play, tinker, and
experiment...
 
C

CBFalconer

osmium said:
I think it would be more accurate to say "A header often contains
an interface description for a module".

IMO if it contains anything more it is being misused.
 
O

osmium

Richard said:
Answer: a header is an interface description for a module. Now that you know
that, what do you think belongs in a header?

I think it would be more accurate to say "A header often contains an
interface description for a module".
 
R

Richard Heathfield

Karsten said:
I figured so... I meant that I don't know what should go into them.

If this was a joke, I apologise for my lack of insight :p

Er, yes, it was, but the rest of my article was intended seriously.

Just to give you a simple idea of what might go into a header, take a look
at:

http://users.powernet.co.uk/eton/c/fgetdata.h
http://users.powernet.co.uk/eton/c/fgetword.c
http://users.powernet.co.uk/eton/c/fgetline.c

(fgetword() reads an entire word of text, no matter how large, into a
buffer, resizing the buffer as necessary. fgetline() does the same for
lines.)

To use these functions, the programmer must #include "fgetdata.h" - but what
is /in/ fgetdata.h? Let's see...

Well, okay, there's a huge comment at the beginning. We'll skip over that,
and then we find this:

#ifndef FGETDATA_H_ /* See Note A */
#define FGETDATA_H_ 1 /* See Note A */
#include <stdio.h> /* See Note B */

/* Next three lines: See Note C */
#define FGDATA_BUFSIZ BUFSIZ /* adjust to taste */
#define FGDATA_WRDSIZ sizeof("floccinaucinihilipilification")
#define FGDATA_REDUCE 1

/* This space intentionally left blank -
See Note D */

int fgetline(char **line, size_t *size, size_t maxrecsize, FILE *fp,
unsigned int flags); /* See Note E */
int fgetword(char **word, size_t *size, const char *delimiters, size_t
maxrecsize, FILE *fp, unsigned int flags); /* See Note E */

#endif /* See Note A */

Note A: the header is protected by "inclusion guards". These stop the
header's main contents from being copied ad nauseam into a translation unit
if it happens to have been #included a bit too enthusiastically. Working
out how it works is diverting, and shouldn't take you more than 2 minutes.

Note B: Since the header refers to the FILE type, it's a good idea to
include the necessary header for that type. This means your calling code
doesn't have to remember to #include <stdio.h> before #including /this/
header.

Note C: Symbolic constants that you want to make available to the caller can
go here, as shown.

Note D: This module didn't need any new types. If you had any, you'd want to
define them here.

Note E: Function prototypes. These are perhaps the most easily-recognised
constituents of a header. :)

That's pretty much all I put in headers, really - comments, preprocessing
directives, typedefs, and function prototypes. I see no reason to clutter
them up with other stuff (on the whole). If you're heavily into global
data, though, you'll want to put your external declarations (NOT the
definitions) in a header too. (I'm /not/ heavily into global data.)
 
S

Servé Lau

Karsten Kvistad said:
Hi!
I'm currently teaching myself the C programming language. The problem is
I don't know what to do next. I know file I/O, pointers, functions and
the like. I do not know how to make .h files, Makefiles or anything else
required for large projects. Should I learn how to do this, sockets,
GTK, curses or what?

It's very useful to familiarize yourself with standard C library functions
in math.h, stdlib.h, string.h etc. These headers are also good examples of
how header files work :)
 
C

CBFalconer

Joona said:
.... snip ...

You figured correctly, they are made with a text editor. See my
previous post for an answer to your additional question. Basically,
put the following in .h files:

* Macro definitions

Only if they are to be visible outside the .c file
* Structure and union definitions

Only if they are to be visible outside the .c file
* Typedefs

Only if they are to be visible outside the .c file
* Extern function or variable declarations

Only if they are to be visible outside the .c file
...and *very* little else. Especially, do NOT put any real
definition of a variable or function, that would actually take
up space in the object file. That would only cause linking
problems if you happen to #include the same .h file from two
.c files.

"Header" is a misnomer. They would be better called interface
specifications. However, all is grist to the #include mill.
 
K

Kevin Goodsell

Karsten said:
Hi!
I'm currently teaching myself the C programming language. The problem is
I don't know what to do next. I know file I/O, pointers, functions and
the like. I do not know how to make .h files, Makefiles or anything else
required for large projects. Should I learn how to do this, sockets,
GTK, curses or what?

I think Richard's advice is particularly good - once you think you've
learned C, it's a very good idea to assume your teacher/book/whatever
was completely wrong, and you need to verify basically everything you
think you know. I know I was taught a number of things that were
incorrect, and I also misunderstood a number of things.

Other than that, you should consider that knowing the mechanics of a
language is only the first step to being able to use the language well.
The next part - learning idioms, good design & code organization,
security and portability issues, etc. - is much more difficult.

I suggest that the very next thing you do should be to read this group's
FAQ. It has a lot of great information and can help you gain a better
understanding of the language as a whole, as well as answering questions
you may have had while you were learning, and making you aware of many
issues that you may not have considered before. The URL is:

http://www.eskimo.com/~scs/C-faq/top.html

-Kevin
 
O

osmium

CBFalconer said:
IMO if it contains anything more it is being misused.

So be it. My compiler includes other header files, which makes things much
more convenient for me, as a user. YMMV.

I can also see where including macros in a header file might be a good idea.
 
M

Malcolm

Servé Lau said:
It's very useful to familiarize yourself with standard C library functions
in math.h, stdlib.h, string.h etc. These headers are also good examples > of how header files work :)
The problem is that stdlib headers are often not designed to be
human-readable.

A typical header looks like this

#ifndef strsup_h
#define strsup_h

#define myisoperator(ch) ( (ch) == '+' || (ch) == '-' || (ch) == '/' | (ch)
== '*') ? 1 : 0

int mystrcount(const char *str, char ch);
char *mystrdup(const char *str);
int mystrreplace(char *dest, size_t len, const char *src, const char
*search, const char *replace);

#endif
 
P

Peter Pichler

CBFalconer said:
IMO if it contains anything more it is being misused.

True, but it may contain less ;-)

Not that such a design would be excessively useful.

Peter
 
P

Peter Pichler

Malcolm said:
A typical header looks like this

#ifndef strsup_h
#define strsup_h

#define myisoperator(ch) ( (ch) == '+' || (ch) == '-' || (ch) == '/' | (ch)
== '*') ? 1 : 0
^
Move this to the end of the line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top