Changes between Version 3 and Version 4 of ResumedC


Ignore:
Timestamp:
Apr 28, 2009, 4:16:04 PM (16 years ago)
Author:
Thanatermesis
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ResumedC

    v3 v4  
    44----
    55
    6 == Comments ==
     6
     7
     8=== Comments ===
    79
    810The 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
     
    1214The special things that you need to have in mind when you code is: clarity of code, simplicity, and summary
    1315
     16You 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:
     17{{{
     18#!C
     19/* Wrong way: */
     20if (strcmp(string1, string2)
     21 ...
    1422
    15 == Basic Things ==
     23/* Easy-to-understand way: */
     24if ((strcmp(string1, string2) == 0)
     25 ...
     26}}}
     27
     28By 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.
     29
     30
     31
     32=== Basic Things ===
    1633
    1734To declare a string in a variable, you need to use strcpy, so
     
    3350Remember that a string contains first a newline (if the input has included a newline) and all the strings finishes by the '\0' (NULL) character
    3451
    35 == Arrays ==
     52The '''break''' statement is for exit from a ''loop'', and the '''continue''' statement is for (re)start the loop again (continue from the start)
     53
     54The '''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:
     55{{{
     56#!C
     57if (var == 0)
     58}}}
     59 ,,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.,,
     60
     61
     62
     63
     64=== Arrays ===
    3665
    3766A two dimensional matrix looks like:
     
    5786}}}
    5887
     88The ''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:
     89 * 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)
    5990
    60 == Variables ==
    6191
    62 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'''
     92
     93
     94
     95
     96=== Variables ===
     97
     98Variables 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)
     99
     100If you need a ''Global variable''' in your entire code, you need to declare them out of any function
     101
     102Everytime 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.
     103
     104'''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.
     105
     106'''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'''
    63107
    64108The '''constant''' variables are constants, so they can't change, by convention they are in upper-case
    65109
     110
     111
     112
     113
     114=== Relational Operators ===
     115{{{
     116#!C
     117++total;
     118   /* is like */
     119   total = total + 1;
     120
     121total += 2;
     122   /* is like */
     123   total = total + 2;
     124
     125/* There is also: */
     126   -=, *=, /=, %=
     127}}}
     128
     129
     130
     131
     132
     133=== Functions ===
     134
     135A basic function:
     136
     137{{{
     138#!C
     139float triangle (float width, float height)
     140 ...
     141}}}
     142 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))
     143
     144This is a example of a function call and modification by using pointers:
     145{{{
     146#!C
     147void convert_to_42 ( int *input )
     148{
     149   *input = 42;
     150}
     151
     152int value = 12;
     153convert_to_42 ( &value );
     154}}}
     155
     156A function can be (re)called too, this is known as ''recursion''
     157
     158
     159
     160
     161=== Compiler ===
     162
     163If you want to see the preprocessor messages (values), you can see them when compiling by using the -E option (in gcc)
     164
     165If you want to pass something defined (to use with #ifdef, for example)
     166
     167
     168
     169
     170
     171== Books ==
     172
     173 * The book of C by Kernighan & Ritchie
     174 * Practical C Programming (very good to understand correctly C and do good practices on it) in o'reilly
     175 * Mastering C Algorithms (advanced C) in o'reilly
     176
     177== References ==
     178
     179 * C FAQ: http://c-faq.com
     180 * ##C (freenode) wiki: http://www.iso-9899.info/