Changes between Version 43 and Version 44 of ResumedC


Ignore:
Timestamp:
Sep 9, 2009, 7:13:21 PM (15 years ago)
Author:
Thanatermesis
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ResumedC

    v43 v44  
    66
    77
    8 === Comments ===
     8== Comments ==
    99
    1010The 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.
     
    4040
    4141
    42 === Variables ===
     42== Variables and Data types ==
    4343
    4444Variables 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)
    4545
    46 If you need a ''Global variable''' in your entire code, you need to declare them out of any function
     46If you need a ''Global variable'' in your entire code, you need to declare them out of any function
    4747
    4848Every 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.
     
    5050'''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'''
    5151
    52 ==== Extern ====
     52=== Extern ===
    5353Since it is the default value out of any function (global mode), you should ignore it. You can use it too for say that a variable is declared from a different (outside) file
    5454
    55 ==== Static ====
     55=== Static ===
    5656You can use ''static'' inside a function or outside it, it has two different meanings:
    5757 * 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
     
    6262
    6363
    64 ==== Void ====
     64=== Void ===
    6565 * In a function declaration means that the function returns no value
    6666 * When used in a pointer declaration (called ''pointer to void''), void defines a generic pointer, so it returns an address, it is commonly used when a function is called with differents types of data, specially pointers of types differents, and also very used on these ways:
     
    7575
    7676
    77 ==== Conversion ====
    78 
    79 You can convert a variable type to other at any time by:
    80 {{{
    81 #!C
    82 int value = 5;
    83 printf ("%f", (float)value );
    84 }}}
    85 
    86 
    87 
    88 
    89 
    90 
    91 === Functions ===
    92 A function can be (re)called too (inside himself), this is known as ''recursion''
    93 
    94 ==== Basic example ====
    95 
    96 {{{
    97 #!C
    98 float triangle (float width, float height)
    99  ...
    100 }}}
    101  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))
    102 
    103 
    104 ==== Structure of a function ====
    105 {{{
    106 #!C
    107 [class_of_memorization] [type] name( [arguments] )
    108 // class: It will be static or extern (see Variables section)
    109 // type: It will be anything (void, int, char, a pointer...), except an array
    110 // arguments: Arguments to pass to a function, if any
    111 }}}
    112 
    113 
    114 ==== Modification of variables by pointers ====
    115 This is a example of a function call and modification by using pointers:
    116 {{{
    117 #!C
    118 void convert_to_42 ( int *input )
    119 {
    120    *input = 42;
    121 }
    122 
    123 int value = 12;
    124 convert_to_42 ( &value );
    125 }}}
    126 
    127 
    128 ==== Prototype of Functions ====
    129 When there's no arguments for the function, just put a ''void'' itself inside the (), so for the content of arguments.
    130 
    131 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
    132 {{{
    133 #!C
    134 double calc( double base, double exponent);
    135 }}}
    136 
     77
     78
     79=== Constants ===
     80
     81The '''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.
     82
     83{{{
     84#!C
     85/* The variable answer_p is a constant and can't change, but the pointer address can be changed. */
     86const char *answer_p = "Forty-Two";
     87
     88/* If we want to have the inverse to this, we need to put the const AFTER the *, so: */
     89char *const answer_p = "Forty-Two";
     90   /* Now the pointer can't change the address value, but the data (Forty-Two) can be changed */
     91
     92/* Finally, we can set both in constant with something like: */
     93const char *const answer_p = "Forty-Two";
     94}}}
     95
     96
     97=== Volatile ===
     98
     99With '''volatile''' you can declare something that will be modified by an external program, on the same way, if you declare ''const volatile foo'', the variable ''foo'' its a constant that cannot be modified by the program itself, but can be modified by an external program
     100
     101
     102
     103=== Restrict ===
     104
     105It is used for pointers, it is used for optimization, using '''restrict''' on a pointer you are only allowed to access to the object with this pointer
     106
     107
     108
     109=== Typedef ===
     110
     111With typedef we can define our own variable types, for example:
     112{{{
     113#!C
     114typedef static long int mylongintstatic;
     115
     116/* example of declare a variable of this new type: */
     117mylongintstatic var1 = 5;
     118}}}
     119
     120Note that ''typedef'' could be considered similar to the #define statement, but can define more complex objects like:
     121{{{
     122#!C
     123/* group is a type of 10-elements array */
     124typedef int group[10]
     125}}}
    137126
    138127
     
    165154The ''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:
    166155 * 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)
    167 
    168 
    169 
    170 
    171 
    172 
    173 
    174 
    175 === Constants ===
    176 
    177 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.
    178 
    179 {{{
    180 #!C
    181 /* The variable answer_p is a constant and can't change, but the pointer address can be changed. */
    182 const char *answer_p = "Forty-Two";
    183 
    184 /* If we want to have the inverse to this, we need to put the const AFTER the *, so: */
    185 char *const answer_p = "Forty-Two";
    186    /* Now the pointer can't change the address value, but the data (Forty-Two) can be changed */
    187 
    188 /* Finally, we can set both in constant with something like: */
    189 const char *const answer_p = "Forty-Two";
    190 }}}
    191 
    192 
    193 
    194 
    195 
    196 === Relational Operators ===
    197 {{{
    198 #!C
    199 number = 5;
    200 result = number++; /* first is evaluated the expression, after is incremented, result is 5 */
    201 
    202 number = 5;
    203 result = ++number; /* first is incremented, after is evaluated the expression, result is 6 */
    204 
    205 total += 2;
    206    /* is like */
    207    total = total + 2;
    208 
    209 /* There is also: */
    210    -=, *=, /=, %=
    211 }}}
    212 
    213 
    214 
    215 
    216156
    217157
     
    253193}}}
    254194
     195
     196
    255197==== Packaged structures ====
    256 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
     198Imagine 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
    257199
    258200Normal structure, total: '''48''' bits
     
    279221
    280222
    281 
    282 
    283 
    284223=== Pointers ===
    285224
     
    306245 a[i] , *(a+i) , *(pa+i) ,   pa[i]     // In the same method way, the 'i' element of an array (no-pointer)
    307246}}}
     247
     248
     249
    308250
    309251==== Double pointers ====
     
    333275
    334276
    335 ==== Operators to access to memory ====
     277
     278
     279
     280
     281=== Conversion ===
     282
     283You can convert a variable type to other at any time by:
     284{{{
     285#!C
     286int value = 5;
     287printf ("%f", (float)value );
     288}}}
     289
     290
     291
     292
     293
     294
     295== Functions ==
     296A function can be (re)called too (inside himself), this is known as ''recursion''
     297
     298Basic example:
     299{{{
     300#!C
     301float triangle (float width, float height)
     302 ...
     303}}}
     304 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))
     305
     306
     307=== Structure of a function ===
     308{{{
     309#!C
     310[class_of_memorization] [type] name( [arguments] )
     311// class: It will be static or extern (see Variables section)
     312// type: It will be anything (void, int, char, a pointer...), except an array
     313// arguments: Arguments to pass to a function, if any
     314}}}
     315
     316
     317=== Modification of variables by pointers ===
     318This is a example of a function call and modification by using pointers:
     319{{{
     320#!C
     321void convert_to_42 ( int *input )
     322{
     323   *input = 42;
     324}
     325
     326int value = 12;
     327convert_to_42 ( &value );
     328}}}
     329
     330
     331=== Prototype of Functions ===
     332When there's no arguments for the function, just put a ''void'' itself inside the (), so for the content of arguments.
     333
     334When 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
     335{{{
     336#!C
     337double calc( double base, double exponent);
     338}}}
     339
     340
     341
     342
     343
     344
     345
     346
     347
     348
     349== Operators ==
     350
     351=== Relational Operators ===
     352{{{
     353#!C
     354number = 5;
     355result = number++; /* first is evaluated the expression, after is incremented, result is 5 */
     356
     357number = 5;
     358result = ++number; /* first is incremented, after is evaluated the expression, result is 6 */
     359
     360total += 2;
     361   /* is like */
     362   total = total + 2;
     363
     364/* There is also: */
     365   -=, *=, /=, %=
     366}}}
     367
     368
     369
     370
     371
     372
     373=== The , Operator ===
     374It can be used specially to include multiple operators, like in a for statement:
     375{{{
     376#!C
     377for (two = 0, three = 0 ; two < 10 ; two +=2, three += 3)
     378}}}
     379
     380
     381
     382
     383
     384=== Operators to access to memory ===
    336385
    337386|| '''Operator''' || '''Description''' || '''Example''' || '''Result''' ||
     
    345394
    346395
    347 === Typedef ===
    348 
    349 With typedef we can define our own variable types, for example:
    350 {{{
    351 #!C
    352 typedef static long int mylongintstatic;
    353 
    354 /* example of declare a variable of this new type: */
    355 mylongintstatic var1 = 5;
    356 }}}
    357 
    358 Note that ''typedef'' could be considered similar to the #define statement, but can define more complex objects like:
    359 {{{
    360 #!C
    361 /* group is a type of 10-elements array */
    362 typedef int group[10]
    363 }}}
    364 
    365 
    366 
    367 
    368 
    369 
    370 === Misc ===
     396
     397
     398
     399
     400
     401== Misc ==
    371402
    372403The '''header''' files should be limited to have only types and definitions.
     
    402433
    403434
    404 ==== The , Operator ====
    405 It can be used specially to include multiple operators, like in a for statement:
    406 {{{
    407 #!C
    408 for (two = 0, three = 0 ; two < 10 ; two +=2, three += 3)
    409 }}}
    410 
    411 ==== The ?: Construct ====
     435
     436
     437
     438
     439
     440=== The ?: Construct ===
    412441It can be used like a replacement of ''if, else...'' but also specially to set values, like:
    413442{{{
     
    432461
    433462
    434 === Optimizations ===
     463== Optimizations ==
    435464
    436465An 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:
     
    472501
    473502
    474 ==== How to Optimize ====
     503=== How to Optimize ===
    475504 * Loop Ordering: Nested loops should be ordered with the simplest loop outermost and the most complex loops innermost
    476505 * Powers of 2: Use a power of 2 when doing integer multiply or divide. Most compilers substitute a shift for the operation.
     
    498527
    499528
    500 === Compiler ===
     529== Compiler ==
    501530
    502531If you want to see the preprocessor messages (values), you can see them when compiling by using the -E option (in gcc)
     
    508537
    509538
    510 === Debugging ===
    511 
    512 ==== GDB ====
     539== Debugging ==
     540
     541=== GDB ===
    513542
    514543Basic Commands
     
    547576
    548577
    549 ==== Messages ====
     578=== Messages ===
    550579 * Segfault: The program has tried to dereference a pointer containing a bad value
    551580 * Stack Overflow: The program has tried to use too many temporary variables. Common problem when there's a infinite loop recursiontes ==