Your SlideShare is downloading. ×
  • Like
Lập trình C
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Lập trình C

  • 2,403 views
Published

Lạp trình C

Lạp trình C

Published in Technology , Art & Photos
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,403
On SlideShare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
43
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Programming Supps 1
  • 2. 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
  • 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 ” , i);
    • }
    • return 0;
    • };
    • -----------------------------
    • void PrintHello( void ) {
    • printf (“Hello! ” );
    • };
    • -----------------------------
  • 5. 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;
  • 6. 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 ;
    • };
  • 7. 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 ” , 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
    • 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 ”);
    • exit(0);
    • };
    • printf( “Hello World! ” );
    • // 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 ” ); 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 left intentionally blank