Character Input and Output C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Introduction to C Programming
Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
UNIT 15 File Processing.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
UNIT 12 UNIX I/O Redirection.
Lesson 2 Building a programming repertoire. Announcements  Homework due Monday  Will get considerably more difficult, so take advantage of relative.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
Engineering Computing I Chapter 1 – Part B A Tutorial Introduction continued.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
C and Data Structures Baojian Hua
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Pointers and Arrays C and Data Structures Baojian Hua
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Character Input and Output
CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Lecture 13. Outline Standard Input and Output Standard Input and Output (I/O)– Review & more Buffered/unbuffered input Character I/O Formatted I/O Redirecting.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Outline Symbolic Constants Character Input and Output if … else switch…case.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
File Handling Spring 2013Programming and Data Structure1.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
File IO and command line input CSE 2451 Rong Shi.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
CS 1704 Introduction to Data Structures and Software Engineering.
C is a high level language (HLL)
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CPT: Chars/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
INTRODUCTION Every language has some features that provides interaction between the program and the user of the program. C language uses the reference.
Plan for the Day: I/O (beyond scanf and printf)
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
The C “switch” Statement
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
The C “switch” Statement
Structured Programming (Top Down Step Refinement)
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
The switch Statement Topics Multiple Selection switch Statement
The switch Statement Topics Multiple Selection switch Statement
The switch Statement Topics Multiple Selection switch Statement
Characters and Strings Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
EECE.2160 ECE Application Programming
Presentation transcript:

Character Input and Output C and Data Structures Baojian Hua

Overview We have talked about: Basic data types and C control structures This lecture: Basic character input and output Three examples: echo input directly to output character counting line counting

Char IO Including the Standard Input/Output (stdio) library #include Makes names of functions, variables, and macros visible Read a single character Read and returns a single character from the text stream “ standard in ” (stdin) int getchar (); Write a single character Writes a single character c to “ standard out ” (stdout) int putchar (int c);

Sample Code #include int main () { int c; c = getchar (); putchar (c); return 0; }

Read Ten Chars #include int main () { int c; int i; for (i=0; i<10; i++) { c = getchar(); putchar (c); } return 0; }

Infinite IO #include int main () { int c; for (;;) { c = getchar (); putchar (c); } return 0; } // or a while version #include int main () { int c; while (1){ c = getchar (); putchar (c); } return 0; }

Conditional IO #include int main () { int c; c = getchar (); while (c != ‘a’) { c = getchar (); putchar (c); } return 0; }

Character Counting /* count characters in input */ #include int main () { long nc = 0; while (getchar() != EOF) nc++; printf("%ld\n", nc); return 0; } EOF: Platform dependent: Ctrl-D on Linux Ctrl-Z on Windows

Line Counting /* count lines in input */ #include int main() { long numLines = 0; int c; while ((c=getchar()) != EOF) if (c == ‘\n’) numLines++; printf("%ld\n", numLines); return 0; }

Arrays Thus far, we have seen: Characters are just small integers (0-255) More operations ==, != Control structures Nested controls Next, we consider how to count the number of ocurrences of characters ‘ 0 ’ to ‘ 9 ’

A First (Stupid) Try #include int main() { long num0, num1, …, num9; num0 = num1 = … = num9 = 0; int c; while ((c=getchar()) != EOF) { if (c == ‘0’) num0++; else if (c == ‘1’) num1++; …; } printf (…); return 0; }

Arrays #include int main() { long num[10]; int c, i; for (i=0; i<10; i++) num[i] = 0; while ((c=getchar()) != EOF) if ((c >= ‘0’) && (c <= ‘9’)) num[c-’0’]++; return 0; }

Essence of Array An array variable a is just the address of the the first array element a[0] So when we pass an array to other functions, or we operate on the array variable, we are really operating on a pointer, not on array elements More on this later

An Example #include void foo (long[] a) { a[0] = 999; return; } int main () { long num[5]; num[0] = 0; foo (num); printf (“%ld\n”, num[0]); return 0; } ? ? ? ? ? num

An Example #include void foo (long a[]) { a[0] = 999; return; } int main() { long num[5]; num[0] = 0; foo (num); printf (“%ld\n”, num[0]); return 0; } 0 ? ? ? ? num

An Example #include void foo (long a[]) { a[0] = 999; return; } int main() { long num[5]; num[0] = 0; foo (num); printf (“%ld\n”, num[0]); return 0; } 0 ? ? ? ? a num

An Example #include void foo (long a[]) { a[0] = 999; return; } int main() { long num[5]; num[0] = 0; foo (num); printf (“%ld\n”, num[0]); return 0; } 999 ? ? ? ? a num

An Example #include void foo (long a[]) { a[0] = 999; return; } int main() { long num[5]; num[0] = 0; foo (num); printf (“%ld\n”, num[0]); return 0; } 999 ? ? ? ? num