Cheat Sheet for C

Cheet Sheet for C

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

Cheat Sheet for Golang
Cheat Sheet for BASH