1 Homework HW6 On line – due next class Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Programming in C #2 Input / Output.
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.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Binary Search Tree For a node: The left subtree contains nodes with keys less than the node's key. The right subtree contains nodes with keys greater than.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Chapter 18 I/O in C.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
Lecture Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
 Integers and Characters  Character Strings  Input and Output.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
CSE1301 Computer Programming: Lecture 6 Input/Output.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
CS 1704 Introduction to Data Structures and Software Engineering.
CSCI 130 Basic Input and Output Chapter 9. The printf ( ) function Printf(“\nThe value of x is %d”, x); Displayed to screen (assume x = 12): –The value.
C is a high level language (HLL)
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
CSC 482/582: Computer Security
File Access (7.5) CSE 2031 Fall July 2018.
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Plan for the Day: I/O (beyond scanf and printf)
Plan of the Day: More on type conversions scanf printf format strings
Programming in C Input / Output.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 08- printf and scanf
Formatted Input/Output
Programming in C Input / Output.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Miscellaneous functions
Lecture Starting K&R Chapter 7 and Appendix B
Programming in C Input / Output.
Weeks 9-10 IO System Calls Standard IO (stdin, stdout) and Pipes
Formatted Input/Output
Formatted Input/Output
Module 12 Input and Output
Introduction to C EECS May 2019.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Professor Jodi Neely-Ritz University of Florida
Format String Vulnerability
Chapter 18 I/O in C.
Presentation transcript:

1 Homework HW6 On line – due next class Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass

2 Standard Library Functions Learn as much as possible about all standard library functions  K&R Appendix B On line, you can get documentation about standard library functions with man command % man printf For more information, use grep on standard library function names in standard.h files % grep strcmp /usr/include/*

3 UNIX grep, Glass Pg 219+ UNIX command “grep” finds matches for a specified string in identified filenames If no filenames present, grep searches stdin Some options in grep: -iignore case (“TEXT” same as “text”) -nadds line numbers to display -vgives only lines that don’t match -w only matches complete words

4 UNIX “Pipes”, Glass Pg 84+ Remember redirection for stdin and stdout? % command filename2 In general, you can invoke a program and pass stdout from that program as stdin to another program via UNIX pipe – symbol | % command1 arguments | command2 arguments % grep pattern filename(s) | more

5 Standard Input /Output, 7.1 C code for reading stdin char getchar (void) gets a character from stdin C code for writing to stdout void putchar(char) puts a character to stdout stdin and stdout can be redirected or piped %./tail <tail.in | more % cat tail.in |./tail >filename2

6 Formatted Output, printf, K&R 7.2 Formats and prints out internal values. int printf(char *format, arg1, arg2,...); printf has a variable length argument list (as many arguments after the first one as % conversions in the format string) We will learn how to do this shortly Return from printf is number of characters printed Haven't used this up till now, but it may be useful if there is some error or limit truncation

7 Formatted Output, printf Between the % and the conversion character, there are a number of other characters which may exist. In the order they must be placed, they are: - (minus sign)left adjust printing of argument + (plus sign)print with a leading sign character 0 (zero)pad with leading zeros instead of spaces m (number m)minimum field width. (dot)separates min field width & precision p (integer p)precision: max chars for string min digits for int h or l (letters)h for short int, l for long int ORDER for %d is: %[-][+][0][m][.][p][h|l]d, Note: No embedded spaces allowed!

8 Formatted Output, printf Figure out what these would do: %10d, %-10d, %hd, %e, %10ld, %10.p Experiment for 10 minutes with a program, using different formats Learn string precision given on pg. 154 Also, to print at most max characters from string s (max is int type var or const), use * after % and include the int max as an argument before s: printf("%.*s", max, s);

9 Formatted Output, printf Can print string literal as format string with no “%s” printf("hello, world!\n"); Could also print a string variable as format string: char s[ ] = "hello, world"; printf(s); If string s has a % character in it, this is unsafe! printf will look for another argument after format string s. Better to write out a variable string s as: printf("%s", s);

10 Formatted Output, sprintf See sprintf in K&R Appendix B, pg 245 Function sprintf works same as printf, but it writes to a specified string, e.g. char array[ ], with trailing ‘\0’ int sprintf(char *string, char *format, arg1, arg2, …); Note: int return value does not include trailing ‘\0’ Recall how we wrote itoa() and itox() functions No functions like this in C library! Use sprintf() to print int into a string using %d or %x

11 Formatted Input, scanf This is the opposite of printf. Reads in variables from stdin using conversion format string. See pg. 246 (and prior pg 245 which explains everything). int scanf(char *format, …); Return value from scanf( ) is number of successfully scanned tokens Not successful if scanf can't parse any value brought in from stdin according to the specified format

12 Formatted Input, scanf Must call scanf with a POINTER to each variable so that values can be set by scanf which is a function! int age, weight; char lname[100]; while(some condition){ printf("Input your last name, age, and weight”); cnt = scanf("%s %d %d", lname, &age, &weight); } Note: lname is an array and is already a pointer

13 Formatted Input, scanf Scanf is useful to allow you to read in int or double value AS A NUMBER (instead of a character string leaving you to do your own conversion in your code) scanf() always see a character sequence in stdin: it just does its own conversion to int or double

14 Formatted Input, scanf However, scanf is FLAWED, because it ignores '\n' characters. Can get very confusing if the user enters too few arguments on an input line being parsed by scanf (Prompt) Input your last name, age, and weight: (User input) Clinton 52 User gets no response after carriage return. User retries, remembers to enter weight this time (User input) Clinton

15 Formatted Input, scanf scanf sees Clinton 52 Clinton since the user entered carriage return is seen as white space scanf thinks weight has bad value and returns 2 as number of successfully scanned tokens scanf does not “consume” the token Clinton and the value of age will be uninitialized The re-entry of Clinton will be processed and returned after the incomplete Clinton 52 Always check the return value from scanf!

16 Formatted Input, scanf Use scanf only for programs needing only ONE input item, usually "quick and dirty" programs with no input checking. Can't code defensively with scanf( ): can't count number of tokens parsed ON A LINE - scanf doesn't care about input lines Best approach is to read a line into an array s[ ] and use "sscanf( )" to parse the arguments in line This also allows you to try to interpret things in more than one way

17 Formatted Input, sscanf See sscanf in K&R Appendix B, pg 246 Function sscanf works same as scanf, but it reads from a specified string, e.g. char array[ ], with a trailing ‘\0’ int sscanf(char *string, char *format, &arg1, &arg2, …); Recall how we wrote function atoi, axtoi to convert a decimal or hex character string s to an integer i? –Use sscanf(s, "%d", &i) for atoi –Use sscanf(s, "%x", &i) for axtoi

18 Formatted Input, scanf/sscanf Note: with both scanf and sscanf, if you put specific characters in the format string, the functions must see exactly those specific characters in the user input cnt = sscanf(s, "%d/%d/%d", &month, &day, &year); Expects input to look exactly like this: 07/23/96 If not, cnt value returned by sscanf is less than 3

19 Variable Length Argument Lists Both printf and scanf have an argument (the format string) that defines the number and type of the remaining arguments in the list Concept similar to “Overloading” in Java C does not support multiple declarations of the same function each with different lists How is it supported in C? Look at stack frame after a function call!

20 Typical Stack frame Decreasing AddressesStack Pointer Before Call Arg1Arg1 Return Data e.g.PC, other Registers, etc Function’s Automatic Variables Stack Pointer After Call Code provides the location of the last fixed argument in call sequence to va_start From fixed arguments, the code must determine the number of additional arguments to access via offsets from stack pointer and va_arg can work its way back up the stack to get each argument Arg2Arg2 Arg3Arg3 Address 0xffffffff

21 Variable Length Argument Lists Use va_ macro package inside function with a variable length argument list to get args void foo (int n, …) /* note ellipsis … */ { va_list ap;/* variable name ap */ va_start(ap, n);/* n is last named arg */ Now ap points just before first unnamed arg

22 Variable Length Argument Lists Each call to va_arg( ) advances pointer ap by one argument and returns value by type: ival = va_arg(ap, int); fval = va_arg(ap, float); sval = va_arg(ap, char *); Function must clean up before returning: va_end(ap);