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 | |
| 81 | 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. |
| 82 | |
| 83 | {{{ |
| 84 | #!C |
| 85 | /* The variable answer_p is a constant and can't change, but the pointer address can be changed. */ |
| 86 | const 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: */ |
| 89 | char *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: */ |
| 93 | const char *const answer_p = "Forty-Two"; |
| 94 | }}} |
| 95 | |
| 96 | |
| 97 | === Volatile === |
| 98 | |
| 99 | With '''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 | |
| 105 | It 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 | |
| 111 | With typedef we can define our own variable types, for example: |
| 112 | {{{ |
| 113 | #!C |
| 114 | typedef static long int mylongintstatic; |
| 115 | |
| 116 | /* example of declare a variable of this new type: */ |
| 117 | mylongintstatic var1 = 5; |
| 118 | }}} |
| 119 | |
| 120 | Note 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 */ |
| 124 | typedef int group[10] |
| 125 | }}} |
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 | | |
335 | | ==== Operators to access to memory ==== |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | === Conversion === |
| 282 | |
| 283 | You can convert a variable type to other at any time by: |
| 284 | {{{ |
| 285 | #!C |
| 286 | int value = 5; |
| 287 | printf ("%f", (float)value ); |
| 288 | }}} |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | == Functions == |
| 296 | A function can be (re)called too (inside himself), this is known as ''recursion'' |
| 297 | |
| 298 | Basic example: |
| 299 | {{{ |
| 300 | #!C |
| 301 | float 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 === |
| 318 | This is a example of a function call and modification by using pointers: |
| 319 | {{{ |
| 320 | #!C |
| 321 | void convert_to_42 ( int *input ) |
| 322 | { |
| 323 | *input = 42; |
| 324 | } |
| 325 | |
| 326 | int value = 12; |
| 327 | convert_to_42 ( &value ); |
| 328 | }}} |
| 329 | |
| 330 | |
| 331 | === Prototype of Functions === |
| 332 | When there's no arguments for the function, just put a ''void'' itself inside the (), so for the content of arguments. |
| 333 | |
| 334 | 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 |
| 335 | {{{ |
| 336 | #!C |
| 337 | double 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 |
| 354 | number = 5; |
| 355 | result = number++; /* first is evaluated the expression, after is incremented, result is 5 */ |
| 356 | |
| 357 | number = 5; |
| 358 | result = ++number; /* first is incremented, after is evaluated the expression, result is 6 */ |
| 359 | |
| 360 | total += 2; |
| 361 | /* is like */ |
| 362 | total = total + 2; |
| 363 | |
| 364 | /* There is also: */ |
| 365 | -=, *=, /=, %= |
| 366 | }}} |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | === The , Operator === |
| 374 | It can be used specially to include multiple operators, like in a for statement: |
| 375 | {{{ |
| 376 | #!C |
| 377 | for (two = 0, three = 0 ; two < 10 ; two +=2, three += 3) |
| 378 | }}} |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | === Operators to access to memory === |