190 likes | 199 Views
This course covers topics such as intro to C for C++ programmers, types in C (int and floating point), C I/O, ASCII code, error checking, character processing, strings as arrays, and strings and pointers.
E N D
Introduction to Computer Organization & Systems COMP 21000 Topics: • Intro to C for C++ programmers • Types in C: int and floating point • C I/O John Barr
Output: printf printf(“score = %d\n”,score);
Input: scanf scanf(“%d %d”,&exam1, &exam2);
What if you replace %c with %d? A program to process a character variable #include <stdio.h> char ch; main (){ scanf(“%c”,&ch); printf(“Original character: %c\n”, ch); ch++; printf(“Following character: %c\n”, ch); }
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
ASCII We’ll talk more about this next week! These assignments also work in UNICODE!
Error Checking • What if the user enters the wrong type? • C will convert characters to int • But will (normally) only read a single character • Example #include <stdio.h> int main() { int x = 0; while (x != -1){ printf("Enter an integer: "); scanf("%d", &x); } return 0; } See what happens on the next slide This example is in Student/comp210/examples/charErr.c
Error Checking Admins-Computer-52:~/Classes/CS210/PractSoln barr$ !g gcc testPurge0.c Admins-Computer-52:~/Classes/CS210/PractSoln barr$ !a a.out Enter an integer: 5 Enter an integer: 6 Enter an integer: a Enter an integer: Enter an integer: Enter an integer: How to stop infinite loops: ^c ^z then us ps and kill -9 Continues forever
Error Checking • What if the user enters the wrong type? • C will convert characters to int • But will (normally) only read a single character #include <stdio.h> #include <stdio_ext.h> int main() { int x = 0; while (x != -1){ printf("Enter an integer: "); scanf("%d", &x); __fpurge(stdin); } return 0; } __fpurgeworks in Linux. Note the two underscores before the namefpurge See “man __fpurge” Note:fpurgeis only available in BSD 4.4.
Char #include <stdio.h> intlower(int c) { if (c >= 'A' && c <= 'Z') return(c + 'a' - 'A'); else return(c); } intmain() { int c; printf("Enter some characters. To end input hit ^D on a new line\n"); /* read char until end of file */ while ( (c = getchar()) != EOF) putchar(lower(c)); } ^d is the EOF symbol See /home/barr/Student/comp210/examples/str1.c
Strings as arrays #include <stdio.h> intstrequal(char x[ ], char y[ ]) { inti=0; if (x == y) return(1); while (x[i] == y [i]) { if (x[i] == '\0') return (1); i++; } return(0); } Continued on next slide See /home/barr/Student/comp210/examples/str2.c
Strings as arrays int main() { char str1[10],str2[10]; printf("Enter a string\n"); scanf("%s ",str1); printf("\nEnter another string\n"); scanf("%s ”,str2); if (strequal(str1,str2)) printf(“The strings are equal\n”); else printf(“The strings are not equal\n”); } Very tricky, this C: the space after %s means ignore the newline character.
Strings and Pointers #include <stdio.h> #include <ctype.h> int main() { char alpha[ ] = "abcdefghijklmnopqrstuvwxyz"; char *str_ptr; str_ptr = alpha; while(*str_ptr != '\0') printf ("%c ",toupper(*str_ptr++) ); printf("\n"); } An array name is a pointer Strings always end with the ‘\0’ character “toupper” is a function defined in the ctype.h library.
Strings: readline #include <stdio.h> #define SIZE 100 intreadline(char s[ ],int max) { intc,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); }
Strings: readline intmain() { char line[SIZE]; int n; while ( (n = readline(line, SIZE) ) > 0) printf("n = %d \t line = %s\n", n, line); }
Strings and dynamic memory #include <string.h> /* compare two strings character by character using array syntax */ intstrequal(char x[ ],char y[ ]) { inti=0; if (x == y) return(1); while (*(x +i) == *(y + i)) { if (*(x + i) == '\0') return (1); i++; } return(0); }
Strcmp is a library function strequal is a user defined function Strings and dynamic memory int main() { char *str1,*str2; str1 = (char *) malloc(20 * sizeof(char)); str2 = (char *) malloc(20 * sizeof(char)); printf("Enter a string or ! to halt \n"); scanf("%s",str1); while (strcmp(str1, "!") != 0){ printf("\nEnter another string\n"); scanf("%s",str2); if (strequal(str1,str2)) printf("The strings are equal\n"); else printf("The strings are not equal\n"); printf("Enter a string or ! to halt \n"); scanf("%s",str1); } }
Use #define to put in debug statements If the name DEBUG is #defined, then #ifdef is true and the printf is included. Strings and pointers intstrequal(char *x, char *y) ; // function prototype #define DEBUG 1 intmain() { char *str1,*str2; str1 = (char *) malloc(10 * sizeof(char)); str2 = (char *) malloc(10 * sizeof(char)); printf("Enter a string or ! to halt \n"); scanf("%s",str1); #ifdef DEBUG printf("This is a debug statement. str1 is %s \n", str1); #endif while (strcmp(str1, "!") != 0){ printf("\nEnter another string\n"); scanf("%s",str2); if (strequal(str1,str2)) printf("The strings are equal\n"); else printf("The strings are not equal\n"); printf("Enter a string or ! to halt \n"); scanf("%s",str1); } }
*x is the contents of what x points at Return true if at end of string Strings and pointers #include <stdio.h> #include <string.h> intstrequal(char *x, char *y) { if (x == y) return(1); while (*x++ == *y++) { if (*x == '\0' && *y == '\0') return (1); } return(0); }