1 / 15

C workshop #2

C workshop #2. functions pointers structures files. Functions. Return-value function-name( parameters ) { … return value; } or void function-name( parameters ) { … return ; (optional) } Calling the function: I = function-name( 10, 20 );. Function example.

nitza
Download Presentation

C workshop #2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C workshop #2 functionspointers structures files

  2. Functions • Return-value function-name( parameters ) { … returnvalue; }orvoid function-name( parameters ) { … return; (optional)} • Calling the function:I = function-name( 10, 20 );

  3. Function example #include <stdio.h> int Add2( int A, int B ) { int C; C = A + B; return C; } void main() { int I; I = Add2( 10, 20 ); printf("Add2 function returns = %d\n", I); } Output: Add2 function returns = 30

  4. Private variables • By default each function has its own private variables #include <stdio.h> int Add2( int A, int B ) { int C; C = A + B; A = 20 return C; } void main() { int A = 0, B = 1, C = 3; A = Add2( 10, 20 ); printf(”A=%d, B=%d, C=%d\n", A,B,C); }

  5. Global variables • A variable that is defined outside of all functions can be used by any function #include <stdio.h> int Offset; int AddWithOffset( int A, int B ) { int C; C = A + B + Offset; Offset--; return C; } void main() { int A; Offset = 33; A = AddWithOffset( 10, 20 ); printf(”A=%d, B=%d, C=%d\n", A,B,C); }

  6. structures • Organize variables under same ‘umbrella’ struct { double Fahrenheit, Celsius; int I; } Temperature; struct { int X,Y; } Point[ 10 ]; • examples Point[I].X = 10; Temperature.Fahrenheit = 10.22; Temperature.I = 10;

  7. #include <stdio.h> void main() { int I; struct { int X,Y; } Point[ 10 ]; for ( I = 0 ; I < 10 ; I++ ) Point[I].X = I; for ( I = 0 ; I < 10 ; I++ ) Point[I].Y = Point[9 - I].X * 100; for ( I = 0 ; I < 10 ; I++ ) printf( "Point %d = (%d,%d)\n", I, Point[I].X, Point[I].Y ); } Output: Point 0 = (0,900) Point 1 = (1,800) Point 2 = (2,700) Point 3 = (3,600) Point 4 = (4,500) Point 5 = (5,400) Point 6 = (6,300) Point 7 = (7,200) Point 8 = (8,100) Point 9 = (9,0)

  8. Pointers • A varaiable that points to a memory location • Example #1int I,J;int *pI;I = 10; J = 30;pI = &I;*pI = 44;printf(“%d\n”, I, J ); • Example #2int A[20];int *pI;for ( I = 0 ; I < 20 ; I++ ) A[I] = I * 3; pI = &A[0];*pI++ = 44; *pI++ = 33; pI++; *pI++ = 55; printf(“%d,%d,%d,%d\n”, A[0], A[1], A[2], A[3] ); Output: 44

  9. Pointers continue • Using pointers to return multiple values from a function • Example #include <stdio.h> int Func( int I, int J, int *Ret ) { *Ret = I + J; if ( I > J ) return 0; return 1; } void main() { int I,J; J = Func( 10, 20, &I ); printf("%d, %d\n", I, J); } Output: 0, 30

  10. Pointers continue even more... • Using pointers to pass structures to and from a function #include <stdio.h> typedef struct { int X,Y; } POINT; int Func( POINT *P ) { return P->X + P->Y; } void main() { int J,K,L; POINT Point[10]; for ( J = 0 ; J < 10 ; J++ ) { Point[J].X = J*2; Point[J].Y = J*3 + 1; } K = Func( &Point[0] ); L = Func( &Point[1] ); printf("%d,%d\n", K,L); } Output: 1,6

  11. fopen, fprintf, fclose • Use ‘pointer’ to predetermined structureFILE *F; • To create / open file: F = fopen( “file_name”, “type” );examples:F = fopen(“file1.dat”, “w” );F_Read = fopen(“file2.dat”,”r” ); • ALWAYS need to close the file before program terminatesfclose( F ); • One way to write to a file – very similar to printf();fprintf( F, “test\n” );

  12. File write example #1 A new file by the name ‘file1.dat’ is created, and its content is: ABCDE Second Line 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, #include <stdio.h> void main() { FILE *F; int I; F = fopen("file1.dat", "w" ); fprintf( F, "ABCDE\n" ); fprintf( F, "Second Line\n" ); for ( I = 0 ; I < 10 ; I++ ) fprintf( F, "%d, ", I); fclose(F); }

  13. File read example #2 The content of the file ‘num10.dat’: 10 20 30 40 99 32 1 999 -22 4423 #include <stdio.h> void main() { FILE *F; int I, J; F = fopen("num10.dat", "rt" ); printf("Reading from the file:\n"); for ( I = 0 ; I < 10 ; I++ ) { fscanf( F, "%d" , &J ); printf( "%d, ", J ); } fclose(F); } Output (on the monitor): Reading from the file: 10, 20, 30, 40, 99, 32, 1, 999, -22, 4423,

  14. The content of the file ‘num10.dat’: 1,10.2 3, 20 5, 30.33 1,2 22 , 333 45,46 9 40.11 3,-993.3333 99,33.2 Output (on the monitor): Reading from the file: 1,10.2 3,20 5,30.33 1,2 22,2 45,46 9,46 3,-993.333 File read example #3 #include <stdio.h> void main() { FILE *F; int I, J; char ST[200]; float FL; double D; F = fopen("num20.dat", "rt" ); printf("Reading from the file:\n"); for ( I = 0 ; I < 8 ; I++ ) { fgets( ST, sizeof(ST)-1, F ); sscanf( ST, "%d,%f" , &J, &FL ); D = FL; printf( "%d,%g \n", J, D ); } fclose(F); }

  15. Compiling under UNIX • File name: test.c • gcc test.corcc test.c • The file that it generates is a.out • To run it: ./a.out

More Related