Lab 1 C Introduction C Developed by Bell

Lab 1. C Introduction § C: – Developed by Bell lab. in 1972. – a procedure-oriented programming language. § Developing environments: – – – Editing Preprocessing (header files) Compile (. obj) #include <stdlib. h> Link (. exe) #include <stdio. h> load #include <time. h> Execution #include <math. h> § Include: – Head files used in the main file. 1

§ Variable and constant: – Must be declared before use – e. g. int i=3; float f=15. 7; double x. – const int max=100; § Data type transfer: – – Assignment: x=1; Mathematic operations: y=i/5+2/3; Model transfer: i=(int)(x+0. 5); Function call: x=sum(a, b); § Basic input/output commands: Variable format printf(“t xxxx %vf 1 xxx %vf 2 xxx n”, var 1, var 2); scanf(“t %vf 1 %vf 2 ”, &var 1, &var 2); Address 2

§ Output format: § Mathematic operators: – Similar to those in Matlab %: Remainder 3

§ Others: – int i=3; – int a; § If/else: If (condition) { statements; } else { statements; } If (condition) { statements; } elseif (condition) { statements; } else { } 4

§ Practice 1: – Input three numbers from the keyboard. – Calculate the maximum, the minimum, and the average values. – Print the result on the screen. § For loops: for (initial setting; terminate conditions; index operations) { statements } 5

§ Array: Elements of the array Array name Function name § Function: – Call by value – Call by reference Return value 6

§ Define (preprocessor): – Increase readability – Increase the flexibility for paramter changes – Replace simple function 7

§ Practice 2: – Input two sequences from the keyboard. – Calculate the convolution of the sequences. – Print the result on the screen. § File operations (read): * int/float: use 4 bytes to store one value FILE * fp; //declare file pointer fp = fopen(“filename. dat", "rb"); // rb: read for bits for (i=0 ; i<512 ; i++) { fread(&temp, 1, 4, fp); // 1: one byte, 4: 4 x 1 bytes at a time x[i] = temp; // store the read value into an array } Or, FILE * fp; fp = fopen(“filename. dat", "rb"); fread(x, 4, sizeof(x), fp); 8

§ File operation (write): FILE * fp 1; fp = fopen(“filename. dat", “wb"); fwrite(x, 4, sizeof(x), fp 1); //declare file pointer // wb: write for bits // 4: 4 bytes a unit § File operations in Matlab: r = randint(1, n); s = r > 0. 5; fid = fopen(‘filename. dat', 'wb'); fwrite(fid, s, 'ubit 1'); % ubit 1: unsigned bit fclose(fid); % float/int 32 fid 2 = fopen(‘filename. dat', 'rb', 'n'); Buffer = fread(fid 2, n, 'ubit 1'); fclose(fid 2); 9

§ Practice 3: – – Store the values of inputted two sequences in two files. Read the values from the files. Calculate the convolution of the sequences. Store the result into another file. § Homework – Find out how to create a function with sequence input and sequence output. – Rewrite the convolution operations with a function. – Generate two sequences with Matlab and store them in the two files. – Use C to read the files and conduct convolution operations. – Store the result to another file and use Matlab to read it. 10
- Slides: 10