Version 4 (modified by 16 years ago) ( diff ) | ,
---|
Table of Contents
Note: This is not pretended to be a Howto, also not really public, it is just a page that i have used to take some notes of a interesting book of C that im reading, you are allowed to edit and correct this document if you see anything wrong, also order it a bit or made it better, but this not pretends 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 specially you need to comment your variables in order to know *what* exactly they are, and also for know possible special things of these variables
It is recommended to learn how works doxygen (5 minutes reading a howto in google, it is very basic) so that you can know special techniques to comment your source code that then you can build 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 this on 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 maybe 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
Everytime you enclose a variable declaration inside a block {}, it is limited to this section (not works out 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.
Static: By declaring a static variable (or function) out of any function (global mode), it will be used only in the actual file (not a real file, but more like a compilation unit), this is good for a better optimization but if you want to use it in other files, you need to declare it by extern. By other side, if you declare it in a block or a loop something like static int foo = 5; you can do operations to this variable without worry about the declaration of the value 5, because when is in mode static, it is only declared to the selected value the first time, the next times that are called is ignored.
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
The constant variables are constants, so they can't change, by convention they are in upper-case
Relational Operators
++total; /* is like */ total = total + 1; total += 2; /* is like */ total = total + 2; /* There is also: */ -=, *=, /=, %=
Functions
A basic function:
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))
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 );
A function can be (re)called too, this is known as recursion
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)
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
- C FAQ: http://c-faq.com
- ##C (freenode) wiki: http://www.iso-9899.info/