Download presentation
Presentation is loading. Please wait.
1
Introduction to Computer Organization & Systems
John Barr Topics: Intro to C for C++ programmers Types in C: int and floating point C I/O
2
Output: printf printf(“score = %d\n”,score);
3
Input: scanf scanf(“%d %d”,&exam1, &exam2);
4
A program to process a character variable
What if you replace %c with %d? #include <stdio.h> char ch; main (){ scanf(“%c”,&ch); printf(“Original character: %c\n”, ch); ch++; printf(“Following character: %c\n”, ch); }
5
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 The ASCII code for a “s”
6
ASCII We’ll talk more about this next week!
These assignments also work in UNICODE!
7
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
8
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: How to stop infinite loops: ^c ^z then us ps and kill -9 Continues forever
9
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; __fpurge works in Linux. Note the two underscores before the name fpurge See “man __fpurge” Note: fpurge is only available in BSD 4.4.
10
Char ^d is the EOF symbol
#include <stdio.h> int lower(int c) { if (c >= 'A' && c <= 'Z') return(c + 'a' - 'A'); else return(c); } int main() 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
11
Strings as arrays Continued on next slide
#include <stdio.h> int strequal(char x[ ], char y[ ]) { int i=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
12
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.
13
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.
14
Strings: readline #include <stdio.h> #define SIZE 100 int readline(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); }
15
Strings: readline int main() { char line[SIZE]; int n; while ( (n = readline(line, SIZE) ) > 0) printf("n = %d \t line = %s\n", n, line); }
16
Strings and dynamic memory
#include <string.h> /* compare two strings character by character using array syntax */ int strequal(char x[ ],char y[ ]) { int i=0; if (x == y) return(1); while (*(x +i) == *(y + i)) if (*(x + i) == '\0') return (1); i++; } return(0);
17
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"); } Strcmp is a library function strequal is a user defined function
18
Strings and pointers Use #define to put in debug statements
int strequal(char *x, char *y) ; // function prototype #define DEBUG 1 int main() { 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"); } Use #define to put in debug statements If the name DEBUG is #defined, then #ifdef is true and the printf is included.
19
Strings and pointers *x is the contents of what x points at
#include <stdio.h> #include <string.h> int strequal(char *x, char *y) { if (x == y) return(1); while (*x++ == *y++) if (*x == '\0' && *y == '\0') return (1); } return(0); *x is the contents of what x points at Return true if at end of string
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.