Introduction to C CSE 2031 Fall 2010 7/15/2019 8:26 AM.

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
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.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
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.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
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.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
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.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
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.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp.
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.
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.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
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.
CS 1704 Introduction to Data Structures and Software Engineering.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Formatted Input and Output
INC 161 , CPE 100 Computer Programming
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Input/output.
TMF1414 Introduction to Programming
File Access (7.5) CSE 2031 Fall July 2018.
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Programming in C Input / Output.
Input and Output Lecture 4.
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.
CSI 121 Structured Programming Language Lecture 7: Input/Output
Lecture 13 Input/Output Files.
Formatted Input/Output
Chapter 9 - Formatted Input/Output
Programming Assignment #1 12-Month Calendar—
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Chapter 4 Managing Input and Output Operations
Programming in C Input / Output.
Formatted Input/Output
Formatted Input/Output
Introduction to C EECS May 2019.
Presentation transcript:

Introduction to C CSE 2031 Fall 2010 7/15/2019 8:26 AM

History Widely used, powerful, and fast. Both started at AT&T Bell Labs. UNIX was written in assembly, later changed to C. Many variants of UNIX.

C vs. Java Java-like (actually Java has a C-like syntax), some differences No //, only /* */ multi-line and no nesting No garbage collection No classes No exceptions (try … catch) No type strings

First C Program #include <stdio.h> main() { printf(“hello, world \n”); } Note: #include <filename.h> replaces the line by the actual file before compilation starts.

Special Characters \n New line \t Tab \” Double quote \\ The \ character \0 The null character \’ Single quote

More Examples We will discuss more programs given in Chapter 1 in class. We will then learn basic input and output in C.

Basic Input and Output CSE 2031 Fall 2010 15 July 2019

Basic I/O Every program has a standard input and output. Usually, keyboard and monitor, respectively. Can use > and < for redirection printf(“This is a test %d \n”, x) scanf(“%x %d”, &x, &y) %d %s %c %f %lf integer string character float double precision

getchar( ) (7.1) To read one character at a time from the standard input (the keyboard by default): int getchar(void) returns the next input char each time it is called; returns EOF when it encounters end of file. EOF input: Ctrl-d (Unix) or Ctrl-z (Windows). EOF value defined in <stdio.h> is -1.

putchar(c) (7.1) Puts the character c on the standard output (the screen by default). int putchar(int) returns the character written; returns EOF if an error occurs.

Example #include <stdio.h> #include <ctype.h> main() /* convert input to lower case*/ { int c while ((c = getchar()) != EOF) putchar(tolower(c)); return 0; }

I/O Redirection prog < infile prog reads characters from infile instead of the standard input. otherprog | prog Output from otherprog is the input to prog. prog > outfile prog writes to outfile instead of the standard output. prog | anotherprog puts the standard output of prog into the standard input of anotherprog.

printf( ) (7.2) int printf(char *format, arg1, arg2, ...); converts, formats, and prints its arguments on the standard output under control of the format. returns the number of characters printed (usually we are not interested in the returned value).

printf( ) Examples printf(“:%s:”, “hello, world”); printf(“:%10s:”, “hello, world”); printf(“:%.10s:”, “hello, world”); printf(“:%-10s:”, “hello, world”); printf(“:%.15s:”, “hello, world”); printf(“:%-15s:”, “hello, world”); printf(“:%15.10s:”, “hello, world”); printf(“:%-15.10s:”, “hello, world”);

printf Conversions

Output Formatting with printf( ) A minus sign, which specifies left adjustment of the converted argument. A number that specifies the minimum field width. The converted argument will be printed in a field at least this wide. If necessary it will be padded on the left (or right, if left adjustment is called for) to make up the field width. A period, which separates the field width from the precision. A number, the precision, that specifies the maximum number of characters to be printed from a string, or the number of digits after the decimal point of a floating-point value, or the minimum number of digits for an integer.

scanf( ) (7.4) scanf( ) is the input analog of printf( ). To read an integer: int num; scanf("%d”, &num); &num is a pointer to num. To read a char and a float: char c; float f; scanf("%c %f”, &c, &f);

scanf Conversions

scanf( ) int scanf(char *format, arg1, arg2, ...); reads characters from the standard input, interprets them according to the specification in format, and stores the results through the remaining arguments. stops when it exhausts its format string, or when some input fails to match the control specification. returns the number of successfully matched and assigned input items (e.g., to decide how many items were found). returns 0 if the next input character does not match the first specification in the format string (i.e., an error). On the end of file, EOF is returned. Note: arg1, arg2, ... must be pointers!

Next time ... Types, Operators and Expressions (Chapter 2)