Blue said:
I am a freshman computer science student teaching myself how to
program in C, since my university only teaches Java for the first 1.5
years. I have one or two (or 4) small questions, because I'm finding
C much more difficult than I did the first semester of Java.
1) I have noticed that it is common for C programmers to shorten names
of functions, variables, and other things. Java's tradition (or so
I've been told) is to write descriptive names for variables that allow
for easy readability and expandability of a program. Will anyone
fault me if I get in the habit of doing this in C?
Readability is favoured in any language, but it is probably true that C
tradition is to be a bit terser than Java. Part of this comes from the
examples set out by their respective Standard Libraries.
Another thing is capitalization, this is probably more extensively used
in Java (and most other OO-languages, for that matter), than C.
All this is in large part a matter of taste... It would probably be a
good idea to just have a look at a lot of C programs to get a taste for
what is considered good practice (although this differs, of course).
If I eventually
have to have someone look at my code, will it be a problem that
capitalizations follow Java standards?
Depends on where you do it. If you were a colleague of mine, I'd try to
talk you out of it ;-)
Do long variable names damage the speed of a program at runtime?
No. Depending on the compiler options (debug information), long
variables /may/ impact the executable size. Also, to be portable, bear
in mind that the C standard prescribes rather low limits on significant
characters in an identifier (31 for external identifiers, 63 for
internal identifiers, in C99).
2) Are structures the closest things C has to the classes provided by
C++ and Java?
Yes; a C struct is comparable to a Java class with only public data members.
On a certain level of abstraction, classes can be viewed as a way of
grouping "structs" and functions that act on them together. In fact, as
you may know, in C++ "structs" /can/ have member functions; in fact, the
only difference between structs and classes in C++ is that in a struct,
all members are public by default. (This is not relevant to C, but
perhaps useful to bear in mind, to get the relationship between structs
and classes clearly into view).
3) Do you have any general advice for making sense of header
files/preprocessor directives?
My advice would be: try to understand the compiler chain. Although the C
standard prescribes preciously little about this, you would typically
find that a C compiler works in several stages on a source file to
produce an executable file:
1) pre-processor: expands #... directives, comments
2) compiler (proper): transforms code to assembly
3) assembler: makes linkeable "object" files
4) linker: glues several object files into an executable
This process is normally performed start-to-finish, but any modern
compiler will have options to stop the translation after any of these
steps. This can be a very instructive exercise.
Your problem seems to lie in step #1, the preprocessor. One thing you
really need to keep in mind is that it can only do rather simple
transformations (such as macro expansions, and verbatim inclusion of
other files), and it knows next to nothing about C (other than what an
identifier looks like).
What to put in header files is in principle arbitrary (since #include
does a verbatim inclusion), although a rather clear convention has emerged.
To understand the convention, it is important to understand the
distinction between the "declaration" and the "definition" of something;
the declaration states 'what it looks like', and gives enough
information to use something from the outside (e.g., types of
parameters); while the definition of something corresponds to the "how":
the implementation.
This takes some getting used to, since (in Java) the declaration and
definition are usually the same thing.
Example:
/* File: fib.h
a declaration; in casu, a function prototype */
int fib(const int n);
/* End of fib.h */
/* File: fib.c
a definition (implementation). Note the inclusion of the header file,
the compiler will check correspondence between declared prototype and
definition */
#include "fib.h"
int fib(const int n)
{
return (n<2) ? n : fib(n-1)+fib(n-2);
}
/* End of fib.c */
Normally, you put the public declarations of some code in a header (.h)
file, and the private declarations (and the definitions of both public
and private things) in an accompanying C file.
Of all the things that I have seen in
C, this is the one thing that really confuses me. I have read four
different explanations of how they work, and they still trouble me
greatly. I have to use trial and error to see if what I'm doing is
correct. Do any of you all know of a website that does a really good
job of explaining preprocessor directives?
No tips for that, other than that it may be useful to experiment a bit
with the stand-alone C preprocessor (traditionally called 'cpp' on
Unix/Linux systems).
Although I can understand where the confusion comes from (the
preprocessor has no counterpart in Java), it may be reassuring that it
is really quite simple. At first, don't get intimidated by the details
of the preprocessor, just make sure you understand #define, #undef, and
#include. This suffices for 95+% of cases.
4) Any general advice for someone just starting out? Places to go,
people to see, etc.?
For a real thorough understanding, you could consider getting the
standard (online electronic version of the C99 standard is USD 18).
Other good books are listed in the FAQ.
Furthermore, this forum is a great place to ask questions concerning
anything related to C (standard C that is, as soon as you deviate to
platform specifics you will be told in no uncertain terms). Make the
effort to check the FAQ first, and if that doesn't help, there's bound
to be people here that can assist.
Thank you all for your help, in advance. I think I've followed all
the forums rules but please tell me if I have not. Thanks again.
Good luck... Learning C is not easy, but it can be a rewarding
experience. Unlike in Java, there is just a very thin layer between the
bare metal and the language. I for one am conviced that learning C will
help your Java skills as well, as you should get a better grasp on what
goes on "below the hood" of the Java compiler and VM.
Best regards,
Sidney