35 | | == Arrays == |
| 52 | The '''break''' statement is for exit from a ''loop'', and the '''continue''' statement is for (re)start the loop again (continue from the start) |
| 53 | |
| 54 | 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: |
| 55 | {{{ |
| 56 | #!C |
| 57 | if (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 === |
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 | |
| 98 | 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) |
| 99 | |
| 100 | If you need a ''Global variable''' in your entire code, you need to declare them out of any function |
| 101 | |
| 102 | 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. |
| 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''' |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | === Relational Operators === |
| 115 | {{{ |
| 116 | #!C |
| 117 | ++total; |
| 118 | /* is like */ |
| 119 | total = total + 1; |
| 120 | |
| 121 | total += 2; |
| 122 | /* is like */ |
| 123 | total = total + 2; |
| 124 | |
| 125 | /* There is also: */ |
| 126 | -=, *=, /=, %= |
| 127 | }}} |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | === Functions === |
| 134 | |
| 135 | A basic function: |
| 136 | |
| 137 | {{{ |
| 138 | #!C |
| 139 | float 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 | |
| 144 | This is a example of a function call and modification by using pointers: |
| 145 | {{{ |
| 146 | #!C |
| 147 | void convert_to_42 ( int *input ) |
| 148 | { |
| 149 | *input = 42; |
| 150 | } |
| 151 | |
| 152 | int value = 12; |
| 153 | convert_to_42 ( &value ); |
| 154 | }}} |
| 155 | |
| 156 | A function can be (re)called too, this is known as ''recursion'' |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | === Compiler === |
| 162 | |
| 163 | If you want to see the preprocessor messages (values), you can see them when compiling by using the -E option (in gcc) |
| 164 | |
| 165 | If 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/ |