Introduction to Computer Organization & Systems

Slides:



Advertisements
Similar presentations
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Advertisements

C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Computer Science 210 Computer Organization Introduction to C.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
CHAPTER 8 CHARACTER AND STRINGS
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
CSE1301 Computer Programming: Lecture 6 Input/Output.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
A First Book of ANSI C Fourth Edition Chapter 9 Character Strings.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2016 C Part IV.
Lecture2.
Computer Science 210 Computer Organization
INC 161 , CPE 100 Computer Programming
Computer Science 210 Computer Organization
Strings (Continued) Chapter 13
Input/output.
C Programming Tutorial – Part I
Introduction to C CSE 2031 Fall /3/ :33 AM.
A First Book of ANSI C Fourth Edition
Plan for the Day: I/O (beyond scanf and printf)
I/O in C + Misc Lecture Sudeshna Sarkar, IIT Kharagpur.
Module 2 Arrays and strings – example programs.
Exercises on String Operations
Strings A string is a sequence of characters treated as a group
Computer Science 210 Computer Organization
Arrays in C.
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Visit for more Learning Resources
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
The while Looping Structure
CSE1320 Files in C Dr. Sajib Datta
توابع ورودي-خروجي.
Software John Sum Institute of Technology Management
Functions, Part 2 of 3 Topics Functions That Return a Value
Lecture 11 Strings.
The switch Statement Topics Multiple Selection switch Statement
The while Looping Structure
Introduction to Computer Organization & Systems
Your questions from last session
Chapter 8 Character Arrays and Strings
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Introduction to Computer Organization & Systems
Introduction to Computer Organization & Systems
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Character Arrays char string1[] = “first”;
Introduction to C EECS May 2019.
Strings #include <stdio.h>
Characters and Strings Functions
The while Looping Structure
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Course Outcomes of Programming In C (PIC) (17212, C203):
C Characters and Strings
Introduction to Computer Organization & Systems
CSCE 206 Lab Structured Programming in C
Functions, Part 2 of 3 Topics Functions That Return a Value
The while Looping Structure
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

Introduction to Computer Organization & Systems John Barr Topics: Intro to C for C++ programmers Types in C: int and floating point C I/O

Output: printf printf(“score = %d\n”,score);

Input: scanf scanf(“%d %d”,&exam1, &exam2);

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); }

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”

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: 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; __fpurge works in Linux. Note the two underscores before the name fpurge See “man __fpurge” Note: fpurge is only available in BSD 4.4.

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

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

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 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); }

Strings: readline int main() { 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 */ 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);

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

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.

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