Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 C workshop #2 functions pointers structures files.

Similar presentations


Presentation on theme: "1 C workshop #2 functions pointers structures files."— Presentation transcript:

1 1 C workshop #2 functions pointers structures files

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

3 3 Function example #include 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 4 Private variables By default each function has its own private variables #include 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 5 Global variables A variable that is defined outside of all functions can be used by any function #include 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 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 7 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) #include 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 ); }

8 8 Pointers A varaiable that points to a memory location Example #1 int I,J; int *pI; I = 10; J = 30; pI = &I; *pI = 44; printf(“%d\n”, I, J ); Example #2 int 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 9 Pointers continue Using pointers to return multiple values from a function Example #include 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 10 Pointers continue even more... Using pointers to pass structures to and from a function #include 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 11 fopen, fprintf, fclose Use ‘pointer’ to predetermined structure FILE *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 terminates fclose( F ); One way to write to a file – very similar to printf(); fprintf( F, “test\n” );

12 12 File write example #1 #include 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); } 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,

13 13 File read example #2 Output (on the monitor): Reading from the file: 10, 20, 30, 40, 99, 32, 1, 999, -22, 4423, #include 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); } The content of the file ‘num10.dat’: 10 20 30 40 99 32 1 999 -22 4423

14 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 #include 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); } File read example #3

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


Download ppt "1 C workshop #2 functions pointers structures files."

Similar presentations


Ads by Google