wiki:ResumedC

Version 20 (modified by Thanatermesis, 15 years ago) ( diff )

--

Note: This is not pretended to be a Howto, also not really public, it is only a page that I have used to take some notes of an interesting book of C that I'm reading. You are allowed to edit and correct this document if you see anything wrong, also order it a bit or make it better, but this not pretended to be a big document so try to maintain it compact. Thanks


Comments

The comments are very important for yourself. Basically, you need to comment all your functions and declarations and especially your variables in order to know *what* exactly they are and to remember the special things about these variables.

It is recommended to learn how doxygen works (5 minutes reading a howto in google, it is very basic) so that you know special techniques about commenting your source code. This will aid in build proper documentation of your API/source directly from these comments

The special things that you need to have in mind when you code is: clarity of code, simplicity, and summary

You can do a more understandable code in a lot of things, for example, who reads the code is not supposed to know in the perfection entirely the functions, so you can write code like this:

/* Wrong way: */
if (strcmp(string1, string2)
 ...

/* Easy-to-understand way: */
if ((strcmp(string1, string2) == 0)
 ...

By separating the source code in multiple files, you have your code a lot more organized and there's a lot more easy to read and specially to search something between the code.

Basic Things

To declare a string in a variable, you need to use strcpy, so

   name = "Me";   /* Illegal */
   strcpy (name, "Me");    /* Legal */

When you assign a single character you need to enclose it in single-quotes ', if you want to assign a string (with the end-of-string (NULL) char included), you need to use double-quotes ".

A good way to remove the newline value from variables (when you use a input-line entry system) is by simply set the end-of-string (NULL) value to the newline place, like:

   fgets(first, sizeof(first), stdin);
   first[strlen(first)-1] = '\0';

Remember that a string contains first a newline (if the input has included a newline) and all the strings finishes by the '\0' (NULL) character

The break statement is for exit from a loop, and the continue statement is for (re)start the loop again (continue from the start)

The if statement is true when the value is non-zero, zero means no data or null, so that means false. If you need to check if a value is zero you can simply do:

if (var == 0)

Maybe this sounds strange by doing it this way but if var is equal to 0, then the result of the () is 1 (true), if you don't understand why is 1 or how this works, you need to start reading a new book of C again...

Arrays

A two dimensional matrix looks like:

   mtrx_var[2][4];

Notice that this form is illegal:

   mtrx_var[2,4];

This is also valid:

   int mtrx_var[2][4] = 
       {
          {1, 2, 3, 4},
          {10, 20, 30, 40}
       };

The counters of the arrays start by 0, so if you need to create an array of 4 elements you need to declare 4 and use them from 0 to 3, this may sound strange:

  • This maybe sound strange, like why i need to declare 4 then instead of 3 ?: simple, because you are declaring 4, and you still having 4 (0 to 3 = 4)

Variables

Variables inside functions (or main) are not heritable between functions, if you want to call a function with a variable you can use the arguments on the function call (or a pointer for argument)

If you need a Global variable in your entire code, you need to declare them out of any function

Every time you enclose a variable declaration inside a block {}, it is limited to this section (will not work outside of it). If you have variables declared out of any function and/or out of main(), it is supported by all the entire application. Also, if you declare a variable inside a block that is already declared outside of it, then for inside the block it is used only the new declared one, not the one declared out of it, however, this practice is not good since may be very confusing.

signed: means with sign, so, values negative and positive, for example if the char variable has a limit of 255 values, using the sign mode we can use from -128 (negative) to 127 (positive), and using unsigned we can use from 0 to 255

Extern

You should ignore it since its the default when there's no anything set. It means that you can use and/or re-declare a function on other files of the code

Static

You can use static inside a function or outside it, it has two different meanings:

  • Inside a function: You can pre set a value to a variable (static int foo = 5), then the first time you enter on the function it will be set to 5, but the next times it not set again, it just remember the last value used
  • Outside a function (global mode): You will use it only in the actual file (compilation unit), good for optimization but you can't use it on other files

Constants

The constant variables are constants, so they can't change, by convention they are in upper-case. You should use it where ever possible, it improves the robustness of the program, better readability, and also improves (just a bit) the optimization, it is a kind of rule and correct programming. When you are sure that a variable doesn't (or don't want to) change in a scope of the program, you can use it.

/* The variable answer_p is a constant and can't change, but the pointer address can be changed. */
const char *answer_p = "Forty-Two";

/* If we want to have the inverse to this, we need to put the const AFTER the *, so: */
char *const answer_p = "Forty-Two";
   /* Now the pointer can't change the address value, but the data (Forty-Two) can be changed */

/* Finally, we can set both in constant with something like: */
const char *const answer_p = "Forty-Two";

Relational Operators

number = 5;
result = number++; /* first is evaluated the expression, after is incremented, result is 5 */

number = 5;
result = ++number; /* first is incremented, after is evaluated the expression, result is 6 */

total += 2;
   /* is like */
   total = total + 2;

/* There is also: */
   -=, *=, /=, %=

Functions

A function can be (re)called too (inside himself), this is known as recursion

Basic example

float triangle (float width, float height)
 ...

This function means: it needs to return a float variable, it is called with two parameters of type float too, this function can't modify directly the variables (width or height), for that is needed to use pointers (because we have the address (of memory) to that data instead the data value, so we can modify its content (data))

Structure of a function

[class_of_memorization] [type] name( [arguments] )
// class: It will be static or extern (see Variables section)
// type: It will be anything (void, int, char, a pointer...), except an array
// arguments: Arguments to pass to a function, if any

Modification of variables by pointers

This is a example of a function call and modification by using pointers:

void convert_to_42 ( int *input )
{
   *input = 42;
}

int value = 12;
convert_to_42 ( &value );

Prototype of Functions

When there's no arguments for the function, just use the void value

When there's arguments, you should add too a name of variable after it, the mean of this is just to be more easy to understand when reading the headers file for example

double calc( double base, double exponent);

Structures

A structure can contains any variable types sections, and also arrays and sub-structures

/* Note: 'time' is a previously-defined structure' */
struct time lap[MAX_LAPS];
  /* This call creates an array called 'lap' of MAX_LAPS elements that are structures of type 'time' */

/* You can also set values like */
lap[count].hour = hour;
  /* This set the value 'hour' on the 'hour' (structure element) of the 'count' (array element) of lap */

/* Initialization of an array of structures: */
struct time start_stop[2] = 
   {
      { 10, 0 },
      { 12, 0 }
   };

Packaged structures

Image an application that stupidly takes a lot of storage but very few 'cpu' usage and you will need to optimize it for data, imagine that it has 3 variables, seen is used to know if the element is seen, and list is used to know if the element is on the list, and number is the number of the element (the data). Between number needs to be a full int that requires 16 bits (or a little less than it), you can set seen and list to use only 1 bit each one, and number to use 14 instead of 16, on that way, you will have the full size of the structure 3 times smaller than originally

Normal structure, total: 48 bits

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
seen . . . . . . . . . . . . . . . .
list . . . . . . . . . . . . . . . .
number . . . . . . . . . . . . . . . .

Packaged structure, total 16 bits

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[seen] [list] [number . . . . . . . . . . . . number]
struct item {
   unsigned int seen:1;
   unsigned int list:1;
   unsigned int number:14;
   }

The bad point of this usage is that it the access to this data is pretty more slow than in normal mode, so do this only when is really needed to save storage space.

Pointers

With pointers you can modify directly the data of a variable (see the functions section)

You can access to a lot more fast way to structure elements and other big pieces that are inside big ones, so you can optimize your code with the correct usage of pointers,

If you want to have a pointer to an array, you need to point to the first array of the variable, so:

/* array of 5 elements */
char arr[5];

/* pointer 'arr_p' to the array 'arr' */
char *arr_p = &arr[0];

*(array_p + 1) /* Equivalent */
(*array_p + 1 ) /* Not Equivalent */

Double pointers

The double pointer can be used for 2 different things:

  • A pointer that points to a pointer, so that you can change the value of the original pointer (address value)
    • To modify the value of a variable using pointers in a function, you need to pass the address of that variable: Same thing when you try to modify the address of a pointer (pointer of a pointer)
  • A two-dimensional array: like in argv, a matrix, or a list of char*, so (*char[])
/* Pointer to a pointer */
void foo( char ** ptr)
{
   *ptr = malloc(255);  // allocate some memory, obtain a new address for ptr
   strcpy( *ptr, "Hello World");  // assign some data to our new addressed pointer
}
int main()
{
   char *ptr = 0;  // we set a new pointer to null
   foo( &ptr );  // we call the function with the address (null) of our pointer
   /* now (after to call the function to assign the pointer), we have 'ptr' with a new address and allocated memory */
}

int main(int argc, char **argv) /* note that '*argv[]' is equivalent to '**argv'  */
  /* but since it doesn't means the pointer of a pointer, we should use *argv[] in preference because is more understandable to what really means */

Typedef

With typedef we can define our own variable types, for example:

typedef static long int mylongintstatic;

/* example of declare a variable of this new type: */
mylongintstatic var1 = 5;

Note that typedef could be considered similar to the #define statement, but can define more complex objects like:

/* group is a type of 10-elements array */
typedef int group[10]

Conversion

You can convert a variable type to other at any time by:

int value = 5;
printf ("%f", (float)value );

Misc

The header files should be limited to have only types and definitions

Optimizations

An array do increments of the array level in order to search/find something, by using pointers is a lot faster than generate an index operation, for example:

while ((*array_p) != 0)
   ++array__p;

Imagine a structure that the total size of every structure are 226 bytes, that's a lot of data to move around all the time, so by declaring an array of pointers you will use now the pointers and browse them, instead of the arrays, on this case, you are moving 4 bytes around, instead of 226.

struct mailing {
   char name[60];
   char address[120];
   ...
} list[MAX_ENTRIES];

struct mailing *list_ptrs[MAX_ENTRIES];
int current;

for (current = 0; current = number_of_entries; ++current)
   list_ptrs[current] = &list[current];
/* Now we have in every list_prts[element], a pointer to the equivalent list[element] array of structures */

Compiler

If you want to see the preprocessor messages (values), you can see them when compiling by using the -E option (in gcc)

If you want to pass something defined (to use with #ifdef, for example) in the moment of the compilation, you will add options to the compiler (gcc) like -DDEBUG or -DMAX=10

Debugging

GDB

Basic Commands

l/list [file:]function/line/from,to List/print the actual piece of code
b/break [file:]function/line set a breakpoint at function (in file)
r/run [arglist] run the programm, with arglist if specified
bt backtrace: display the program stack
fr/frame number we go to the selected frame number (like the ones resulting in a backtrace
p/print expr Show the value of the variables, if you want to see the pointed value use *pointer
x examine memory in different data formats/types (string, int, hex, etc), see: help x
c continue running the programm after a stop (like a breakpoint)
n/next execute next program line (after stopping); OVER any function call in the line
s/step execute next program line (after stopping); INTO any function call in the line

Extra Commands

help show a list of extra help
info show things about your program
help status list of available status states of the program
info source display information about the current source file
info locals show values of local variables of actual function
info args show arguments of actual function
set args [arguments] set commandline arguments for the program
set listsize number set the default number of lines for the command list
info breakpoints show all the defined breakpoints
tbreak line/function set a temporal breakpoint; it is removed after to reach it
delete [bp_number|range] delete breakpoints; no parameters = delete all of them
disable [bp_number|range] disable breakpoints; use enable to enable them again
watch set a watchpoint to an expression, the program stops when the value of the expression changes, use rwatch to read it at any time

Books

  • The book of C by Kernighan & Ritchie
  • Practical C Programming (very good to understand correctly C and do good practices on it) in o'reilly
  • Mastering C Algorithms (advanced C) in O'Reilly

References

Note: See TracWiki for help on using the wiki.