1 C workshop #2 functions pointers structures files.

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

Numerical Recipes The Art of Scientific Computing (with some applications in computational physics)
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
Structures Spring 2013Programming and Data Structure1.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File I/O Lesson CS1313 Spring File I/O Lesson Outline 1.File I/O Lesson Outline 2.File I/O Using Redirection #1 3.File I/O Using Redirection #2.
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
ISP - 2 nd Recitation Functions Pointers Structs Files Code Examples Homework!
C++ for the MFE class at UC Berkeley Session # Yuli Kaplunovsky MFE, MBA, MS-Tax, CFA (408)
C workshop Yuli Kaplunovsky - Today - Introduction to C Recommended book: The C programming Language / Kernighan & Ritchie.
C Language Summary HTML version. I/O Data Types Expressions Functions Loops and Decisions Preprocessor Statements.
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
1 C workshop #5 Files, Interface to Excel & Matlab, Introduction to C++, More Financial Applications (Newton-Raphson), Cholesky decomposision By: Yuli.
Guide To UNIX Using Linux Third Edition
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 File Handling in C Lecture 17c 20/3/01.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
Characters and Strings File Processing Exercise C Programming:Part 3.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Chapter 3 Input and Output
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
THE C PROGRAM WORKSHOP. Workshop Features Door for expected results to go out Door for requests to go in Door for unexpected results to go out Bins for.
Functions & Pointers in C Jordan Erenrich
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
1 CSC2100B Data Structure Tutorial 1 Online Judge and C.
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
Pointers Value, Address, and Pointer. Values and Addresses int x, y, z; y x z values of x,
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4;
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
Structs & typedef. Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example:
Ahmet Sacan - Ceng C-Programming: A Crash-Course in Pointers, Dynamic Memory, Structs, & Files Ceng302: Intro to DBMS Ahmet Sacan.
 Dynamic Memory Allocation  Linked Lists  void main() {{ ◦ FILE *file; ◦ char file_name[] = “file.txt”; ◦ if ((file = fopen(file_name, "w")) ==
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
Chapter 8 Arrays, Strings and Pointers
Command Line Arguments
Structs & typedef Already seen in tirgul.
C Short Overview Lembit Jürimägi.
C Basics.
Programming in C Input / Output.
What you need for the 1st phase of project
Programming in C Input / Output.
Programming in C Pointer Basics.
FILE HANDLING IN C.
Govt. Polytechnic,Dhangar
Local Variables, Global Variables and Variable Scope
File Input and Output.
File Handling.
File Access (7.5) CSE 2031 Fall January 2019.
Programming in C Pointer Basics.
File Access (7.5) CSE 2031 Fall February 2019.
Programming in C Input / Output.
File Handling in C.
Quick Review EECS May 2019.
Programming in C Pointer Basics.
C Programming - Lecture 3
Structs & typedef Already seen in tirgul.
Files Chapter 8.
Presentation transcript:

1 C workshop #2 functions pointers structures files

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 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 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 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 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 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 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 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 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 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 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 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’:

14 The content of the file ‘num10.dat’: 1,10.2 3, 20 5, ,2 22, , , ,33.2 Output (on the monitor): Reading from the file: 1,10.2 3,20 5, ,2 22,2 45,46 9,46 3, #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 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