![]() |
(Click on the title bar to return to main programming page) |
MY C NOTES:
Here are some of the notes that I have written
about the C language, which may grow in the future. These notes
come with absolutely no warranty, which means that I am not
responsible to the consequences that will be unleased to the user
of these notes! So use them at your own risk! Also, some of these
notes are imcomplete, hard to understand, or maybe inaccurate.
foovar++ is not the same as
foovar += 1
When you use these a parameter:
"foovar++" actually passes the value of the
"foovar" before its increment, whereas "foovar +=
1" increases foovar by one, then passes the new value of
"foovar".
(*foovar)++ versus *foovar++
"(*foovar)++" actually increases the
value that is pointed to the memory location at
"foovar", whereas "*foovar++" increases the
pointer "foovar" to the next entry, and if passed as a
parameter, it returns the value that is pointed at *foovar before
the pointer increment.
*foovar is the same as foovar[0]
That is right! You can manipulate an element that
appears after a pointer by enclosing the offset in brackets. To
manipulate the next element that appears after the element that
is pointed by "*foovar", you use "foovar[1]".
Do not try "*foovar->fooelement", because I have
gotten compiler errors. So I use foovar[0]->fooelement for the
time being.
DEBUGGING TRICKS!
You can enclose debug reporters (bit of added
code that report debugging information) inside sets of #ifdef DEBUG and #endif. This way, you can
add a -D parameter
with DEBUG to your
compiler command line, if you need to debug your program. When
you finish, just remove the -DDEBUG
parameter, and you can keep the debugging data in the program for
later use if you wanted to.
One type of debug reporter, is the use of fprintf(stderr, ...); which sends information to the error "console". When you use an Integrated Development Environment (IDE) such as RHIDE, all the information that is send to file stderr will appear inside a window when the program ends execution (This may vary on your choice of IDEs). This is how is set out:
#ifdef DEBUG
fprintf(stderr, "<filename of .c file>: <function
name> does this!");
#endif
This is how it can be implemented in a code for the purpose of debugging:
void foo_increase_i () {
I++;
#ifdef DEBUG
fprintf(stderr,"foo.c: foo_increase_i does i++. Now I is
%u\n",i);
#endif
}
When i increases by one in foo_increase_i with the -DDEBUG switch, it will output to the error "console" a track report about the program activity.
You do not have to use DEBUG as a macro for debugging. You can use an unique macro name for each type of debugging in your program. For example, you can use DEBUG_FOO to help you with FOO routines, and use DEBUG_BAR to help with your BAR routines. Here is an example:
void bar_decease_a () {
a--;
#ifdef DEBUG_BAR
fprintf(stderr,"foo.c: foo_increase_i does i++. Now I is
%u\n",i);
#endif
}
void foo_add_two_to_i () {
a++
#ifdef DEBUG_FOO
fprintf(stderr,"foo.c: foo_increase_i does i++. Now I is
%u\n",i);
#endif
}
When you need to see what BAR routines are reporting, you need to use -DDEBUG_BAR with your compiler, of when you need to see what FOO routines are reporting, you need to use -DDEBUG_FOO with your compiler. Also, you can specify both -DDEBUG_FOO and -DDEBUG_BAR if you needed to see both FOO and BAR routines are doing.
A word of note! Debugging reporters will only help you when you place them in the correct places in the program, and report the appropriate information that can assist debugging. When a routine is found to be buggy, you can place debug reporters to the routines that buggy routine uses, and to routines that calls the buggy routines, plus the buggy routine itself.