printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); return 0;

Similar documents
INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Fundamentals of Programming

Fundamental of Programming (C)

Introduction to Computing Lecture 03: Basic input / output operations

Unit 4. Input/Output Functions

Formatted Output Pearson Education, Inc. All rights reserved.

Formatted Input/Output

Introduction to Programming

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Input/Output Week 5:Lesson 16.1

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

AWK - PRETTY PRINTING

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Should you know scanf and printf?

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

UNIT-I Input/ Output functions and other library functions

Number Systems, Scalar Types, and Input and Output

Advanced C Programming Topics

C: How to Program. Week /Mar/05

BSM540 Basics of C Language

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Standard I/O in C and C++

Stream Model of I/O. Basic I/O in C

Java Basic Datatypees

THE FUNDAMENTAL DATA TYPES

Fundamentals of C. Structure of a C Program

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Structured programming. Exercises 3

Input/output functions

EC 413 Computer Organization

Fundamentals of Programming. Lecture 11: C Characters and Strings

Chapter 2 - Introduction to C Programming

Basic Types and Formatted I/O

File I/O. Last updated 10/30/18

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Lecture 4. Console input/output operations. 1. I/O functions for characters 2. I/O functions for strings 3. I/O operations with data formatting

Input/output functions

Input/Output: Advanced Concepts

Reserved Words and Identifiers

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

11 Console Input/Output

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Chapter 1 & 2 Introduction to C Language

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

int main(void) { int a, b, c; /* declaration */

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Outline. Computer Programming. Preprocessing in C. File inclusion. Preprocessing in C

Programming and Data Structures

CC112 Structured Programming

CSC 1107: Structured Programming

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

Fundamentals of Programming Session 4

CSC 1107: Structured Programming

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

CSC 1107: Structured Programming

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

BSM540 Basics of C Language

Lecture 3. More About C

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer variable of type FILE.

WARM UP LESSONS BARE BASICS

Chapter 9 Formattazione Input/Output

Chapter 8: Character & String. In this chapter, you ll learn about;

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

CS1100 Introduction to Programming

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Control Flow Statements. Execute all the statements grouped in the brackets. Execute statement with variable set to each subscript in array in turn

C Programming

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Fundamentals of Programming Session 19

Getting started with Java

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer of type FILE.

Library Functions. General Questions

Console Input and Output

Input / Output Functions

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Programming for Engineers Introduction to C

Output with printf Input. from a file from a command arguments from the command read

The Design of C: A Rational Reconstruction (cont.)

Introduction to C Language

ANSI C Programming Simple Programs

211: Computer Architecture Summer 2016

16.216: ECE Application Programming Fall 2011

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Display Input and Output (I/O)

Work relative to other classes

Transcription:

Chapter 9: Formatted Input/Output ================================= * All input and output is performed with streams - sequences of characters organized into lines. * Each line consists of zero or more characters and ends with the newline character. * When program execution begins, three streams are connected to the program automatically. 1. Standard input stream: is connected to the keyboard 2. Standard output stream: is connected to the screen 3. Standard error stream: is connected to the screen for error messages. * Operating system often allow these streams to be redirected to other devices. Formatting Output and Printf ---------------------------- * Every "printf" call contains a format control string that describes the output format. * The format control string consists of: 1. conversion specifiers (begins with a percent sign and ends with a conversion specifier). 2. flags 3. field widths 4. precisions 5. literal characters * The "printf" function has the form: printf(format-control-string, other arguments); * The format-control-string describes the output format, and other-arguments(optional) correspond to each conversion specification in the format-control-string. Printing Integers ----------------- d Display a signed decimal integer. i Display a signed decimal integer (different with "d" with "scanf"). o Display an unsigned octal integer. u Display an unsigned decimal integer. x or X Display an unsigned hexadecimal integer (x for a-f and X for A-F). h or l Place before any integer conversion specifier to indicate that a short or long integer is displayed respectively. printf("%d\n", 455); printf("%i\n", 455); printf("%d\n", +455); printf("%d\n", -455); printf("%hd\n", 32000); printf("%ld\n", 2000000000); printf("%o\n", 455); printf("%u\n", 455); printf("%u\n", 455); printf("%x\n", 455); printf("%x\n", 455); 1

Printing Floating-Point Numbers ------------------------------- e or E Display a floating-point value in exponential notation. f Display floating-point values. g or G Display a floating-point value in either the floatingpoint form "f" or the exponential form "e" (or "E"). L Place before any floating-point conversion specifier to indicate that a "long double" floating-point value is displayed. * Values printed with the conversion specifiers "e", "E", and "f" are output with 6 digits of precision to the right of the decimal point by default. * "f" always prints at least one digit to the left of the decimal point. * "e" and "E" always print exactly one digit to the left of the decimal point. * e.g. printf("%e\n", 1234567.89); printf("%e\n", +1234567.89); printf("%e\n", -1234567.89); printf("%e\n", 1234567.89); printf("%f\n", 1234567.89); printf("%g\n", 1234567.89); printf("%g\n", 1234567.89); Printing Strings and Characters ------------------------------- * Conversion specifier "c" requires a "char" argument. * Conversion specifier "s" requires a pointer to "char" as an argument. * Conversion specifier "s" casuses characters to be printed until a terminating "NULL" ('\0'). char character = 'A'; char string[] = "This is a string; char *stringptr = "This is also a string"; printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); Other Conversion Specifiers --------------------------- p Display a pointer value in an implementation defined manner. n Store the number of characters already output in the current "printf" statement. A pointer of an integer is 2

supplied as the corresponding argument. Nothing is displayed. % Display the percent character. int *ptr; int x = 12345, y; ptr = &x; printf("the value of ptr is %p\n", ptr); printf("the address of x is %p\n\n", &x); printf("total characters printed on this line is: %n", &y); printf(" %d\n\n", y); y = printf("this line has 28 charcters."); printf("%d characters were printed\n\n", y); printf("printing a %% in a format control string\n"); Printing with Field Widths and Precisions ----------------------------------------- * If the field width is larger than the data being printed, the data will normally be right-justified within that field. * An integer representing the field width is inserted between the percent sign (%) and the conversion specifier in the conversion specification. * The field width is automatically increased to print values wider than the field, and the minus sign for a negative value uses one character position in the field width. printf("%4d\n", 1); printf("%4d\n", 12); printf("%4d\n", 123); printf("%4d\n", 1234); printf("%4d\n\n", 12345); printf("%4d\n", -1); printf("%4d\n", -12); printf("%4d\n", -123); printf("%4d\n", -1234); printf("%4d\n", -12345); * Precision meanings for different data types: integer The minimum number of digits to be printed. Zero are prefixed to the printed value until the total number of digits is equivalent to the precision. 3

floating-point The number of digits to appear after the (e, E, and f) decimal point. floating-point The maximum number of significant digits to be (g and G) printed. string (s) The maximum number of characters to be written from the string * To use precision, place a decimal point (.) followed by an integer representing the precision between the percent sign and the conversion specifier. printf("%.4d\n\t%.9d\n\n", i, i); * When a floating-point value is printed with a precision smaller than the original number of decimal places in the value, the value is rounded. printf("%9.3f", 123.456789); * It is possible to specify the field width and the precision using integer expressions in the argument list following the format control string. printf("%*.*f", 7, 2, 98.736); Using Flags in the Printf Format COntrol String ----------------------------------------------- * Function "printf" also provides flags to supplement its output formatting capabilities. - Left-justify the output within the specified field + Display a plus sign preceding positive values and a minus sign preceding negative values. space Print a space before a positive value not printed with the "+" flag. # Prefix "0" to the output value when used with the octal conversion specifier "o". Prefix "0x" or "0X" to the output value when used with the hexadecimal conversion specifiers "x" and "X". Force a decimal point for a floating-point number printed with "e", "E", "f", "g" or "G" that does not contain a fractional part. For "g" and "G" specifiers, tailing zeros are not eliminated. 0 Pad a field with leading zeros. * To use a flag in a format control string, place the flag immediately to the right of the percent sign. * Several flags may be combined in one conversion specification. printf("%10s%10d%10c%10f\n\n", "hello", 7, 'a', 1.23); printf("%-10s%-10d%-10c%-10f\n\n", "hello", 7, 'a', 1.23); printf("%d\n%d\n", 786, -786); printf("%+d\n%+d\n", 786, -786); 4

printf("% d\n% d\n", 547-547); int c = 1427; float p = 1427.0 printf("%#o\n", c); printf("%#x\n", c); printf("%#x\n", c); printf("%g\n", p); printf("%#g\n", p); printf("%+09d", 452); printf("%09d", 452); Printing Literals and Escape Sequences -------------------------------------- * Various control characters must be represented by escape sequences. * An escape sequence is represented by a backslash (\) followed by a particular escape character. \' Output the single quote (') character. \" Output the double quote (") character. \? Output the question mark (?) character. \\ Output the backslash (\) character. \a Cause an audible (bell) or visual alert. \b Move the cursor back one position on the current line. \f Move the cursor to the start of the next logical page. \n Move the cursor to the beginning of the next line. \r Move the cursor to the beginning of the current line. \t Move the cursor to the next horizontal tab position. \v Move the cursor to the next vertical tab position. Formatting Input with Scanf --------------------------- * Every "scanf" statement contains a format control string that describes the format of the data to be input. * The "scanf" function is written in the following form: scanf(format-control-string, other-arguments); Integers d i Read an optionally signed decimal integer Read an optionally signed decimal, octal, or hexadecimal integer. 5

o Read an octal (unsigned) integer. u Read an unsigned decimal integer. x or X Read a hexadecimal (unsigned) integer. h or l Place before any of the integer conversion specifiers to indicate that a "short" or "long" integer is to be input. Floating-point numbers e, E, f, g, Read a floating-point value. or G l or L Place before any of the floating-point conversion specifiers to indicate that a "double" or "long double" value is to be input. Characters and strings c Read a character. No null ('\0') is added. s Read a string. Scan set [scan char] Scan a string for a set of characters that are stored in an array. Miscellaneous p Read a pointer address n Store the number of characters input so far % Skip a percent sign (%) in the input *E.g. /* Try -70-70 070 0x70 70 70 70 */ int a, b, c, d, e, f, g; printf("enter seven integers: "); scanf("%d%i%i%i%o%u%x", &a, &b, &c, &d, &e, &f, &g); printf("the input displayed as decimal integers is: \n"); printf("%d %d %d %d %d %d %d\n", &a, &b, &c, &d, &e, &f, &g); /* Try 1.27987 1.27987e+03 3.38476e-06 */ float a, b, c; printf("enter three floating-point numbers: \n"); scanf("%e%f%g", &a, &b, &c); printf("here are the numbers entered in plain\n"); printf("floating-point notation:\n"); printf("%f %f %f \n", a, b, c); /* Try Sunday */ char x, y[9]; printf("enter a string: "); scanf("%c%s", &x, y); printf("the input was:\n"); printf("the character \"%c\" ", x); 6

* E.g printf("and the string \"%s\"\n", y); /* Try ooeeooahah */ char z[9]; printf("enter string: "); scanf("%[aeiou]", z); printf("the input was \"%s\"\n", z); /* Try String */ char z[9]; printf("enter a string: "); scanf("%[^aeiou]", z); printf("the input was \"%s\"\n", z); /* Try 123456 */ int x, y; printf("enter a six digit integer: "); scanf("%2d%d", &x, &y); printf("the integers input were %d and %d\n", x, y); int month1, day1, year1, month2, day2, year2; printf("enter a date in the form mm-dd-yy: "); scanf("%d%*c%d%*c%d", &month1, &day1, & year1); printf("month = %d day = %d year = %d\n\n", month1, day1, year1); printf("enter a date in the form mm/dd/yy: "); scanf("%d%*c%d%*c%d", &month2, & day2, & year2); printf("month = %d day = %d year = %d\n\n", month2, day2, year2); * "scanf" provides the assignment suppression character "*", which enables "scanf" to read any type of data from the input and discard it without assigning it to a variable. 7