Programming Supps 1
C Data Types Declaring variables: int  i, j, k; float  m = 0.0; double  n, p = 0.0; // pointer to int int  *ip; OpenGL Equivalents: GLint int GLuint unsigned int GLfloat float GLdouble  double  [etc.] +3.2 x 10 32 -3.2 x 10 32 float  +very big -very big double  65536 0 unsigned int  +32767 -32768 int  strings char  Max Min Type
Arrays 1-D Arrays: int  Array1[] = { 0, 0, 0 }; int  Array2[3]; int  i; for  (i=0; i<3; i++) { Array2[i] = 0; }; --------------------------- int  num = 10; int  Array0[num];   // Error!!! #define  MAX 10 // #define replaces string w. val. int  Array0[MAX];  // OK!!! 2-D Arrays: // Create 2 x 3 matrix // with 6 elements int  Array4[2][3]; int  i, j; for  (i=0; i<2; i++) for  (j=0; j<3; j++) { Array4[i][j] = 0; }; Note that array indices start at 0
Declaring Functions type  FunctionName ( parameters ) { Local variables; C statements; }; type is data type returned (int, float, double, etc.) Both type and parameters can be  void  (return nothing, or have no parameters) Function can call functions ----------------------------- int  CountToNMinus1( int  n) { int  i; for  (i=0; i<n; i++) { // print to console printf( “%i\n” , i); } return  0; }; ----------------------------- void  PrintHello( void ) { printf (“Hello!\n” ); }; -----------------------------
Testing Conditionals For a variable, zero = false, any non-zero number = true Equal / Not Equal / Less Than / Greater Than: x == y x != y x < y, x > y Compound Expressions: ( x == y ) && ( x > z )  AND ( x != y ) || ( x >= z )  OR The Quick Switch: ( x > y ) ? x = 5 : x = 0 if ( x > y) x = 5; else x = 0;
Control Structure 1 IF / IF … ELSE if  ( true ) { DoFirstThing(); DoSecondThing(); }; if  ( true ) DoSomething(); else DoSomethingElse(); SWITCH switch  ( key ) { case  ‘a’: case  ‘A’: DoFirstThing(); DoSecondThing(); break ; case  ‘b’: DoSomething(); break ; default : break ; };
Control Structure 2 FOR int  i, j; for  (i=0; i<5; i++) for  (j=5; j>0; j--) { // i counts up // j counts down printf( “%i %j\n” , i, j); }; The “ ++ ” / ” -- ” is shortcut used to increment / decrement value of int variables WHILE int  i = 0; int  StayInLoop = 1;  while  ( StayInLoop ) { i+=2; // Make sure you have // exit condition! if   ( i > 200 )   StayInLoop = 0 ; }; “ +=“ increments by n
Main() Function All C programs need a main() function #include  <stdlib.h> #include  <stdio.h> int  main( int  argc,  char  **argv) { // Use arguments for error-checking if  ( argc < 3 ) { printf( “usage: MyTest param_file pic_file\n ”); exit(0); }; printf( “Hello World!\n” ); // Compiler expect some int value to be returned return  0; }; argc (argument count) is number of arguments passed in at command line. This includes the executable. argv (argument value) is value of each argument (as strings) passed in at command line. argv[0] is the executable name. The “**” are pointers!
Executable: MyTest argc = 3 argv[0] = “MyTest” argv[1] = “param.txt” argv[2] = “pic.jpg” if  ( argc < 3 ) { printf( “usage: MyTest param_file pic_file\n” ); exit(0); };
More info … http://irc.essex.ac.uk/www.iota-six.co.uk/c/ http://www.cs.cf.ac.uk/Dave/C/
This slide left intentionally blank

Lập trình C

  • 1.
  • 2.
    C Data TypesDeclaring variables: int i, j, k; float m = 0.0; double n, p = 0.0; // pointer to int int *ip; OpenGL Equivalents: GLint int GLuint unsigned int GLfloat float GLdouble double [etc.] +3.2 x 10 32 -3.2 x 10 32 float +very big -very big double 65536 0 unsigned int +32767 -32768 int strings char Max Min Type
  • 3.
    Arrays 1-D Arrays:int Array1[] = { 0, 0, 0 }; int Array2[3]; int i; for (i=0; i<3; i++) { Array2[i] = 0; }; --------------------------- int num = 10; int Array0[num]; // Error!!! #define MAX 10 // #define replaces string w. val. int Array0[MAX]; // OK!!! 2-D Arrays: // Create 2 x 3 matrix // with 6 elements int Array4[2][3]; int i, j; for (i=0; i<2; i++) for (j=0; j<3; j++) { Array4[i][j] = 0; }; Note that array indices start at 0
  • 4.
    Declaring Functions type FunctionName ( parameters ) { Local variables; C statements; }; type is data type returned (int, float, double, etc.) Both type and parameters can be void (return nothing, or have no parameters) Function can call functions ----------------------------- int CountToNMinus1( int n) { int i; for (i=0; i<n; i++) { // print to console printf( “%i\n” , i); } return 0; }; ----------------------------- void PrintHello( void ) { printf (“Hello!\n” ); }; -----------------------------
  • 5.
    Testing Conditionals Fora variable, zero = false, any non-zero number = true Equal / Not Equal / Less Than / Greater Than: x == y x != y x < y, x > y Compound Expressions: ( x == y ) && ( x > z ) AND ( x != y ) || ( x >= z ) OR The Quick Switch: ( x > y ) ? x = 5 : x = 0 if ( x > y) x = 5; else x = 0;
  • 6.
    Control Structure 1IF / IF … ELSE if ( true ) { DoFirstThing(); DoSecondThing(); }; if ( true ) DoSomething(); else DoSomethingElse(); SWITCH switch ( key ) { case ‘a’: case ‘A’: DoFirstThing(); DoSecondThing(); break ; case ‘b’: DoSomething(); break ; default : break ; };
  • 7.
    Control Structure 2FOR int i, j; for (i=0; i<5; i++) for (j=5; j>0; j--) { // i counts up // j counts down printf( “%i %j\n” , i, j); }; The “ ++ ” / ” -- ” is shortcut used to increment / decrement value of int variables WHILE int i = 0; int StayInLoop = 1; while ( StayInLoop ) { i+=2; // Make sure you have // exit condition! if ( i > 200 ) StayInLoop = 0 ; }; “ +=“ increments by n
  • 8.
    Main() Function AllC programs need a main() function #include <stdlib.h> #include <stdio.h> int main( int argc, char **argv) { // Use arguments for error-checking if ( argc < 3 ) { printf( “usage: MyTest param_file pic_file\n ”); exit(0); }; printf( “Hello World!\n” ); // Compiler expect some int value to be returned return 0; }; argc (argument count) is number of arguments passed in at command line. This includes the executable. argv (argument value) is value of each argument (as strings) passed in at command line. argv[0] is the executable name. The “**” are pointers!
  • 9.
    Executable: MyTest argc= 3 argv[0] = “MyTest” argv[1] = “param.txt” argv[2] = “pic.jpg” if ( argc < 3 ) { printf( “usage: MyTest param_file pic_file\n” ); exit(0); };
  • 10.
    More info …http://irc.essex.ac.uk/www.iota-six.co.uk/c/ http://www.cs.cf.ac.uk/Dave/C/
  • 11.
    This slide leftintentionally blank