Changes between Version 27 and Version 28 of ResumedC


Ignore:
Timestamp:
May 27, 2009, 12:54:21 AM (16 years ago)
Author:
Thanatermesis
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ResumedC

    v27 v28  
    3030
    3131
    32 === Basic Things ===
    33 
    34 To declare a string in a variable, you need to use strcpy, so
    35 {{{
    36 #!C
    37    name = "Me";   /* Illegal */
    38    strcpy (name, "Me");    /* Legal */
    39 }}}
    40 
    41 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 ".
    42 
    43 
    44 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:
    45 {{{
    46 #!C
    47    fgets(first, sizeof(first), stdin);
    48    first[strlen(first)-1] = '\0';
    49 }}}
    50 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
    51 
    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 it 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...,,
     32
     33
     34
     35
     36=== Variables ===
     37
     38Variables 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)
     39
     40If you need a ''Global variable''' in your entire code, you need to declare them out of any function
     41
     42Every 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.
     43
     44'''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'''
     45
     46==== Extern ====
     47You should '''ignore''' it since its the default when there's no anything set. It means that you can use and/or re-declare a function on other ''files'' of the code
     48
     49==== Static ====
     50You can use ''static'' inside a function or outside it, it has two different meanings:
     51     * 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
     52     * Outside a function (global mode): You will use it only in the actual file (compilation unit), good for optimization but you can't use it on other files
     53
     54
     55==== Void ====
     56 * In a function declaration means that the function returns no value
     57 * 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, and also very used on these ways:
     58{{{
     59#!C
     60void *malloc( unsigned int ); /* function prototype for malloc */
     61
     62string_ptr = malloc(80); /* We call malloc in order to allocate 80 bytes of memory,
     63                            it returns the address obtained, that is copied to the
     64                            string_ptr variable (so, the pointer to this address) */
     65}}}
     66
     67
     68==== Conversion ====
     69
     70You can convert a variable type to other at any time by:
     71{{{
     72#!C
     73int value = 5;
     74printf ("%f", (float)value );
     75}}}
     76
     77
     78
     79
     80
     81
     82=== Functions ===
     83A function can be (re)called too (inside himself), this is known as ''recursion''
     84
     85==== Basic example ====
     86
     87{{{
     88#!C
     89float triangle (float width, float height)
     90 ...
     91}}}
     92 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))
     93
     94
     95==== Structure of a function ====
     96{{{
     97#!C
     98[class_of_memorization] [type] name( [arguments] )
     99// class: It will be static or extern (see Variables section)
     100// type: It will be anything (void, int, char, a pointer...), except an array
     101// arguments: Arguments to pass to a function, if any
     102}}}
     103
     104
     105==== Modification of variables by pointers ====
     106This is a example of a function call and modification by using pointers:
     107{{{
     108#!C
     109void convert_to_42 ( int *input )
     110{
     111   *input = 42;
     112}
     113
     114int value = 12;
     115convert_to_42 ( &value );
     116}}}
     117
     118
     119==== Prototype of Functions ====
     120When there's no arguments for the function, just put a ''void'' itself inside the (), so for the content of arguments.
     121
     122When 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
     123{{{
     124#!C
     125double calc( double base, double exponent);
     126}}}
     127
    60128
    61129
     
    93161
    94162
    95 === Variables ===
    96 
    97 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)
    98 
    99 If you need a ''Global variable''' in your entire code, you need to declare them out of any function
    100 
    101 Every 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.
    102 
    103 '''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'''
    104 
    105 ==== Extern ====
    106 You should '''ignore''' it since its the default when there's no anything set. It means that you can use and/or re-declare a function on other ''files'' of the code
    107 
    108 ==== Static ====
    109 You can use ''static'' inside a function or outside it, it has two different meanings:
    110      * 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
    111      * Outside a function (global mode): You will use it only in the actual file (compilation unit), good for optimization but you can't use it on other files
    112 
    113 
    114 ==== Void ====
    115  * In a function declaration means that the function returns no value
    116  * 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, and also very used on these ways:
    117 {{{
    118 #!C
    119 void *malloc( unsigned int ); /* function prototype for malloc */
    120 
    121 string_ptr = malloc(80); /* We call malloc in order to allocate 80 bytes of memory,
    122                             it returns the address obtained, that is copied to the
    123                             string_ptr variable (so, the pointer to this address) */
    124 }}}
    125 
    126163
    127164
     
    166203
    167204
    168 
    169 
    170 
    171 === Functions ===
    172 A function can be (re)called too (inside himself), this is known as ''recursion''
    173 
    174 ==== Basic example ====
    175 
    176 {{{
    177 #!C
    178 float triangle (float width, float height)
    179  ...
    180 }}}
    181  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))
    182 
    183 
    184 ==== Structure of a function ====
    185 {{{
    186 #!C
    187 [class_of_memorization] [type] name( [arguments] )
    188 // class: It will be static or extern (see Variables section)
    189 // type: It will be anything (void, int, char, a pointer...), except an array
    190 // arguments: Arguments to pass to a function, if any
    191 }}}
    192 
    193 
    194 ==== Modification of variables by pointers ====
    195 This is a example of a function call and modification by using pointers:
    196 {{{
    197 #!C
    198 void convert_to_42 ( int *input )
    199 {
    200    *input = 42;
    201 }
    202 
    203 int value = 12;
    204 convert_to_42 ( &value );
    205 }}}
    206 
    207 
    208 ==== Prototype of Functions ====
    209 When there's no arguments for the function, just put a ''void'' itself inside the (), so for the content of arguments.
    210 
    211 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
    212 {{{
    213 #!C
    214 double calc( double base, double exponent);
    215 }}}
    216205
    217206
     
    341330
    342331
    343 === Conversion ===
    344 
    345 You can convert a variable type to other at any time by:
    346 {{{
    347 #!C
    348 int value = 5;
    349 printf ("%f", (float)value );
    350 }}}
    351 
    352 
    353 
    354 
    355 
    356 
    357 
    358332=== Misc ===
    359333
     
    361335
    362336When you free some memory, remember to set the pointer to NULL after in order to avoid possible errors by using freed memory.
     337
     338To declare a string in a variable, you need to use strcpy, so
     339{{{
     340#!C
     341   name = "Me";   /* Illegal */
     342   strcpy (name, "Me");    /* Legal */
     343}}}
     344
     345When 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 ".
     346
     347
     348A 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:
     349{{{
     350#!C
     351   fgets(first, sizeof(first), stdin);
     352   first[strlen(first)-1] = '\0';
     353}}}
     354Remember that a string contains first a newline (if the input has included a newline) and all the strings finishes by the '\0' (NULL) character
     355
     356The '''break''' statement is for exit from a ''loop'', and the '''continue''' statement is for (re)start the loop again (continue from the start)
     357
     358The '''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:
     359{{{
     360#!C
     361if (var == 0)
     362}}}
     363 ,,Maybe this sounds strange by doing it 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...,,
     364
     365
     366
     367
     368
     369
     370
    363371
    364372