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