(AN INCOMPLETE) LIST OF COMMON "IDIOMS" DO YOU NEED TO KNOW ----------------------------------------------------------- 1. STRUCTURE OF A PROGRAM ------------------------ #include ..... /* FOR EACH HEADER FILE YOU NEED */ #typdef struct { TYPE1 FIELD1; TYPE2 FIELD2; ...... }TYPENAME; /* FOR EACH STRUCT DEFINITION YOU USE */ XXXXX FUNCTIONNAME( PARAMETER LIST) { } /* FOR EACH FUNCTION YOU ARE DEFINING */ main() { /* DECLARATION OF EACH VARIABLE YOU ARE USING */ /* STATEMENTS */ } ------------------------------------------------------------------ 2. GETTING INPUT & PRINTING OUTPUT: ----------------------------------- getting an integer: printf(PROMPT); scanf(" %i",&NAME); getting a character: printf(PROMPT); scanf(" %c",&NAME); getting a string: printf(PROMPT); scanf(" %s", NAME); printing an integer: printing a character: printing a string: ----------------------------------------------------------- 3. CONDITIONS: ------------- if (ThisIsTrue) do this otherwise do nothing: Multiple Choices: -------------------------------------------- ----------------- if (CONDITION_TO_TEST) switch(VARIABLENAME) { { /* DO THIS IF TRUE */ case VALUE1: /* DO THIS */;break; } case VALUE2: /* DO THIS */; break; ....... if (ThisIsTrue) do this otherwise do something ---------------------------------------------- default: /* IF NONE OF ABOVE, DO THIS */ if (CONDITION_TO_TEST) } { /*DO THIS IF TRUE */ } else { /* DO THIS IF FALSE */ } ------------------------------------------------------ 4. ARRAYS & STRINGS -------------------- declaring an array: TYPE NAME[NUMBER OF ELEMENTS] e.g. int myArray[10]; char myChars[30]; A string is just a variable length character array that is terminated by a '\0' character You must declare the maximum size including the '\0'. accessing an individual element of an array: NAME[INDEX] note that INDEX starts at 0. you cannot test 2 arrays for equality with ==, you must use a function e.g. array1 == array2 does not test if array1 contains the same as array2 you cannot assign one array to another with = except when it is first declared e.g. array1 = array2; will not work - you must copy array2 to array1 element by element ----------------------------------------------- 5. LOOPING ---------- for loop: for (DO_THIS_FIRST; CONTINUE_IF_THIS_IS_TRUE; DO_THIS_EACH_ITERATION) { /* DO THESE STATEMENTS EACH TIME */ } while loop: /* INITIALISE LOOP TEST */ while (CONTINUE_IF_THIS_IS_TRUE) { /* DO THESE STATEMENTS EACH TIME */ /* UPDATE LOOP TEST */ } looping through an array element by element: looping through a string element by element: int i; OR int i; char s[256]; char s[256]; for (i=0; s[i]!='\0'; i++) i=0; { while (s[i]!='\0') /* DO THESE STATEMENTS EACH TIME */ { } /* DO THESE STATEMENTS EACH TIME */ i++; } looping for input until a condition is satisfied: printf(PROMPT); /* GET INPUT */; while (CONDITION_IS_FALSE) { /* DO THESE STATEMENTS EACH TIME */ printf(PROMPT); /* GET INPUT */; } -------------------------------------------------- 6. FUNCTIONS ------------ definitions of functions that return an explicit value have the form : --------------------------------------------------------------------- RETURN_TYPE FUNCTIONNAME (PARAMETER_LIST) { /* DECLARE LOCAL VARIABLES I.E. NAMES NOT IN PARAMETER LIST */ /* PROCESSING STATEMENTS */ return VALUE; } RETURN_TYPE must be a simple type e.g. int, char, a pointer or a struct definitions of functions that do not return an explicit value have the form : --------------------------------------------------------------------------- void FUNCTIONNAME (PARAMETER_LIST) { /* DECLARE LOCAL VARIABLES I.E. NAMES NOT IN PARAMETER LIST */ /* PROCESSING STATEMENTS */ } RETURN_TYPE must be a simple type e.g. int, char, a pointer or a struct PARAMETER_LIST has the form: TYPE1 NAME1, TYPE2 NAME2, .... if parameter NAMEX is PASSED BY VALUE e.g. int minimum(int a, int b) : TYPEX is a simple type e.g. int, char or a struct NAMEX contains a copy of the variable passed into the function if parameter NAMEX is PASSED BY REFERENCE e.g. int strlength(char *s) : TYPEX is the address of the variable passed into the function Any changes made to *NAMEX in the function will affect the original. calling a function ------------------- that returns a result: e.g. x = getInt(); n = strlength(char *s) that doesn't return a result e.g. getString(myString, 255); toUpper(myString); --------------------------------------------- 7. STRUCTS ---------- defining a struct type ---------------------- #typdef struct { e.g. typedef struct TYPE1 FIELD1; { TYPE2 FIELD"; char partNumber[11]; ...... int inStock; }TYPENAME; int reorderLevel; } Part; declaring a struct variable --------------------------- TYPENAME VARIABLENAME e.g. Part item; accessing an individual field in a struct ----------------------------------------- VARIABLENAME.FIELDX e.g. item.inStock ----------------------------------------------------------- 8. POINTERS & DYNAMIC MEMORY ALLOCATION ---------------------------------------- for a variable declared with the name NAME in an expression, ----------------------------------------------------------- NAME is replaced by the current value of NAME i.e. the value at location &NAME &NAME is replaced by the address of NAME *NAME is replaced by the value at the address contained in NAME FOR AN ARRAY NAME , &NAME and &NAME[0] are all the same thing using pointers as function parameters ------------------------------------- e.g. void myFunction(int *x) e.g. void stringFunction(char *s) { { .... .......... /* give a value to the thing at x */ /* change value in s*/ *x = 53; s[i] = 'c'; } } main() main() { { int y; char str[256]; /* pass address into function */ /* note no & in the call*/ myFunction(&y); stringFunction(str); printf("%d\n", y); printf("%s\n", str); } } dynamic allocation ------------------ memory can be dynamically allocated with malloc() and released with free() these both require #include typedef struct { char partNumber[11]; int inStock; int reorderLevel; } Part; main() { Part * item; /* address of memory allocated */ item = malloc(sizeof(item)); free(item); } -------------------------------------------------------------- 9. STACKS & QUEUES ------------------- a stack is a LastInFirstOut dynamic data structure push() adds an item to the top of the stack pop() removes the top item from the stack a queue is a FirstInFirstOut dynamic data structure join() adds an item to the back of the queue leave() removes an item from the front of the queue ------------------------------------------------------------------ 10. SEPARATION COMPILATION --------------------------- to combine separate files into a single program 1. create a header file for the "library" file e.g. myLib.h 2. create a source file for the "library" file e.g. myLib.c 3. produce an object file for the "library" e.g. gcc -c myLib.c 4. write a main program that #includes "myLib.h" e.g. myProg.c 5. compile the main program with the "library" e.g. gcc -o myProg myProg.c myLib.o --------------------------------------------------------------------------