Lập trình C

2,535
-1

Published on

Lạp trình C

Published in: Technology, Art & Photos
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,535
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
44
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Lập trình C

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

    Clipping is a handy way to collect important slides you want to go back to later.

×