310 likes | 442 Views
Introduction to Computer Organization & Systems. COMP 21000. Topics: Intro to C Types in C: int and floating point C I/O C file I/O. C Part II. The function of a compiler. The compiler as a program. The machine independence of a Level HOL6 language. C Programming & Systems Programming.
E N D
Introduction to Computer Organization & Systems COMP 21000 Topics: • Intro to C • Types in C: int and floating point • C I/O • C file I/O C Part II
C Programming & Systems Programming • Specific type of programming • Not used in the development of most applications • Emphasis is on conciseness and efficiency (space and time) • Ignores readability and maintainability • You will get fired if you program like this in most situations!
The three attributes of a C++/C variable • A name • A type • A value
Note the & Global variables are possible, but not in this class! A C++ program that processes three integer values C++ program C program #include <stdio.h> #define bonus 5 int exam1, exam2, score; main( ){ scanf(“%d %d”, &exam1, &exam2); score = (exam1 + exam2)/2 + bonus; printf(“score = %d\n”, score); }
Output: printf printf(“score = %d\n”,score);
Input: scanf scanf(“%d %d”,&exam1, &exam2); scanf(“ %d %d”,&exam1, &exam2); note that you can put a space before a ‘%’ character. This will cause scanf to skip a whitespace character (like a newline or ‘\n’ character) when it reads input.
What if you replace %c with %d? A program to process a character variable #include <stdio.h> main (){ char ch; scanf(“%c”,&ch); printf(“Original character: %c\n”, ch); ch++; printf(“Following character: %c\n”, ch); }
ASCII These assignments also work in UNICODE!
The ASCII code for a “s” A program to process a character variable (Cont’d) Using %c Using %d bash-2.03$ a.out s Original character: 115 Following character: 116
I/O* • There are three standard I/O “streams” in C • stdin • stdout • stderr • You can also create other streams • files • pipes • sockets * See K&R chapter 7 or H&S chapter 15
Variable fname is a file pointer Function fopen will open the file given as the first argument of the command line. Function fprintf writes to the file indicated by its first argument Function fscanf read from the file indicated by its first argument. File I/O* #include <stdio.h> // need this library for the exit() system call #include <stdlib.h> #define SIZE 100 int main() { int line[SIZE]; int n, i = 0; FILE *fptr; char *textName = "inFile.txt"; /* verify that we can open the file */ if ( (fptr= fopen(textName, "r")) == NULL) { fprintf(stderr, "Cannot open %s for reading ", textName); exit(1); } printf("Enter some lines. End your input with ^d on a new line: \n"); while ( (fscanf(fptr, "%d", &line[i]) ) != EOF) { printf("integer %d = %d\n", i, line[i]); i++; } close(fptr); printf ("Goodbye! \n"); } * This example is on the server at /home/barr/Student/Examples/week2/intIO.c
Arrays* // array.c #define SIZE 5 int main() { int line[SIZE]; int i, n; n = readints(line, SIZE); printf("The numbers you entered are: \n"); for (i = 0; i < n; i++) { printf("%d\n", line[i]); } } You do have to specify the size of an array that is a variable (unless you want to use dynamic memory) To pass an array, just use its name * This example is on the server at /home/barr/Student/Examples/week2/array.c
readints* // ex2.c #include <stdio.h> #define SIZE 5 int readints(int s[ ],int max) { int c,i=0; printf("Enter %d numbers: \n",max); while (i < max) scanf("%d",&s[i++]); return(i); } You don’t have to specify the size of an array that is a parameter (for 1D arrays) * This example is on the server at /home/barr/Student/Examples/week2/readints.c
Passing Arrays* // arry.c #include <stdio.h> // needed for malloc #include <stdlib.h> #define SIZE 5 int* readints(int max); int main() { int *line; int i; line = readints(SIZE); printf("The numbers you entered are: \n"); for (i = 0; i < SIZE; i++) { printf("%d\n", line[i]); } } must have a function prototype if you’re going to define the function after main( ) To receive an array, must use a pointer To receive an array, just use the pointer variable name notice the array syntax. * This example is on the server at /home/barr/Student/Examples/week2/arrayPassing.c
Passing Arrays Must return a pointer to an int int* readints(int max) { int c,i=0; int *newArray; newArray = (int *)malloc(sizeof(int)*SIZE); printf("Enter %d numbers: \n",max); while (i < max) { scanf("%d",&newArray[i++]); } return(newArray); } Must use a pointer variable Must allocate memory Return the pointer
2D arrays #include <stdlib.h> #include <stdio.h> #define ROWS 2 #define COLS 3 int main( ) { int matrix[ROWS][COLS]; int n, i, j; n = COLS; for (i = 0; i < ROWS; i++) { printf("Enter %d ints for row %d:\n", n, i); for (j = 0; j < COLS; j++) { scanf("%d", &matrix[i][j]); } } * This example is on the server at /home/barr/Student/Examples/week2/array2D.c
2D arrays (cont) printf( "The numbers are: \n"); for (i = 0; i < ROWS; i++){ for (j = 0; j < COLS; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } }
File I/O: reading 2D arrays*part 1** #include <stdlib.h> #include <stdio.h> #define ROWS 5 #define COLS 10 int main( ) { int matrix[ROWS][COLS]; int n, i, j; FILE *fname; char *textName = “ints.txt”; fname = fopen(textName, "r"); // error check; did the file open? if (fname == NULL) { fprintf(stderr, "Could not open %s\n", textName); exit(1); } fnameis the file descriptor variable here’s where we open the file * see K&R section 5.7 or H&S section 5.4.2 for a discussion of multi-dimensional arrays **See /home/barr/Student/Examples/week1/arrays2D.c
File I/O: reading part 2 for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { fscanf(fname, "%d", &matrix[i][j]); } } printf( "The numbers are: \n"); for (i = 0; i < ROWS; i++){ for (j = 0; j < COLS; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } here’s where we read in from the file
Strings • A string in C is a char array • This is a string with up to 30 characters: char line[30]; • To be a string a char array must have the character ‘\0’ as its last character • string constants are really arrays: char line[30] = “john”;
Strings • The C string library contains functions #include <string.c> char *strcpy (char *dest, char *src); Copy src string into dest string. char *strncpy(char *string1, char *string2, int n); Copy first n characters of string2 to stringl . intstrcmp(char *string1, char *string2); Compare string1 and string2 to determine alphabetic order. intstrncmp(char *string1, char *string2, int n); Compare first n characters of two strings. intstrlen(char *string); Determine the length of a string.
Strings char *strcat(char *dest, const char *src); Concatenate string src to the string dest. char *strncat(char *dest, const char *src, int n); Concatenate n chracters from string src to the string dest. char *strchr(char *string, int c); Find first occurrence of character c in string. char *strrchr(char *string, int c); Find last occurrence of character c in string. char *strstr(char *string2, char string*1); Find first occurrence of string string1 in string2. char *strtok(char *s, const char *delim) ; Parse the string s into tokens using delim as delimiter.
Variable fname is a file pointer Function fopen will open the file given as the first argument of the command line. Function fprintf writes to the file indicated by its first argument. File I/O strings* #include <stdio.h> #define SIZE 100 intmain(int argc, char *argv[ ]) { char line[SIZE]; int n; FILE *fname; char textName = “outFile.txt”; /* verify that we can open the file */ if ( (fname = fopen(textName, "w")) == NULL) { fprintf(stderr, "%s: cannot open %s for writing", argv[0], argv[1]); exit(1); } printf("Enter some lines. End your input with ^d: \n"); while ( (n = readline(line, SIZE) ) > 0) fprintf(fname, "n = %d \t line = %s\n", n, line); printf ("Goodbye! \n"); } * This example is on the server at /home/barr/Student/Examples/week2/file2.c
File I/O strings (cont)* /* This function will read an entire line including white space until either a newline character of the EOF character is found */ intreadline(char s[ ],int max) { int c,i=0; max--; while (i < max && (c = getchar()) != EOF && c != '\n') s[i++] =c; if (c == '\n') s[i++] = c; s[i] = '\0'; return(i); } To compile the previous slide together with this slide: > gcc –o fileExample file2.c readline.c * This example is on the server at /home/barr/Student/Examples/week2/readline.c
Argc indicates the number of command line arguments (including the program name). Argv is an array of pointers to char (strings) that lists all of the command line arguments. Command Line intmain(int argc, char *argv[ ]) { int j; if (argc != 2) { printf("factorial takes one integer argument\n"); return(1); /* abnormal termination. */ } /* ASCII string to integer conversion */ j = atoi(argv[1]); printf("factorial(%d) = %d\n", j, factorial(j)); return(0); }
Command Line #include <stdio.h> intfactorial (int n) { int ans = 1; while (n > 1) { ans *= n; n = n - 1; } return(ans); }