Standard I/O Functions – printf() Without specifying any display formatting, the printf() function will use DEFAULT setting: Print in a field of minimum.

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
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.
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
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 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
Printing. printf: formatted printing So far we have just been copying stuff from standard-in, files, pipes, etc to the screen or another file. Say I have.
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.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
 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.
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.
 Pearson Education, Inc. All rights reserved Formatted Output.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 4 Input and Output.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Introducing Python CS 4320, SPRING Format: Field widths and Alignment The string representation of a value can be padded out to a specific width.
Formatting Screen Output How do I make my numbers look pretty?
1 Pertemuan 4 Fungsi-Fungsi Bahasa C Matakuliah: T0016/Algoritma dan Pemrograman Tahun: 2005 Versi: versi 2.
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.
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.
DATA TYPE AND DISPLAY Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
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.
Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.
Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP C Part III.
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Variables Symbol representing a place to store information
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CS 125 Lecture 9 Martin van Bommel. Output printf used to display integers, reals (floats and doubles), and strings printf(” control string ”, exp 1,
CS 1704 Introduction to Data Structures and Software Engineering.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Introduction to Programming Lecture 5: Interaction.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
C Formatted Input/Output
Formatted Input and Output
ECE Application Programming
Formatted Input/Output
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
TMF1414 Introduction to Programming
Chapter 3: I/O Management
Formatted Input/Output
OUTPUT STATEMENTS GC 201.
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.
Formatted Input/Output
Programming in C Input / Output.
Lecture 13 Input/Output Files.
Formatted Input/Output
Chapter 4 Managing Input and Output Operations
Making the PRINTF Look Better in G05 !!
Programming in C Input / Output.
Formatted Input/Output
Conversion Check your class notes and given examples at class.
Formatted Input/Output
Presentation transcript:

Standard I/O Functions – printf() Without specifying any display formatting, the printf() function will use DEFAULT setting: Print in a field of minimum length, beginning on the current position on the current line.

Standard I/O Functions – printf() Example: printf(“%s”, “Value of variable “); printf(“%c”, ‘a’); printf(“%s”, “ is: “); printf(“%f\n”, 1.25); printf(“%s%c%s%f\n”, “Value of variable”,’a’,” is: “, 1.25); Output Value of variable a is: Note that the element is printed starting from the last space of element printed earlier.

Standard I/O Functions – printf() Format SpecifierMinimum field %d, %ld, number of digits that form the decimal Example: 15 – 2 spaces, 1500 – 4 spaces -15 – 3 spaces, – 5 spaces

Standard I/O Functions – printf() Format SpecifierMinimum field %x, %Xnumber of digits that form the hexadecimal Example 4 (1 space – 4) 40 (2 spaces – 28) -4 (8 spaces – fffffffc) -40 (8 spaces – ffffffd8) Note that when it is a negative number, 8 spaces are used.

Standard I/O Functions – printf() Format SpecifierMinimum field %c1 %fnumber of digits that form the integer part 1 space for decimal point 6 digit for fractional part Example 1.25 (8 spaces – ) (9 spaces – )

Standard I/O Functions – printf() Format SpecifierMinimum field %sDepends on number of characters Example: “Hello, World!” (13 spaces – Hello, World!)

Standard I/O Functions – printf() Consider this example: printf(“I am %5d years old\n”, 28); Iam28yearsold 5 spaces are reserved for the decimal number Right Alignment - spaces at right side will be used first

Standard I/O Functions – printf() Consider this example: printf(“I am %5d years old\n”, 28); Iam28yearsold 5 spaces are reserved for the decimal number Right Alignment - spaces at right side will be used first

Standard I/O Functions – printf() What’s the output?: for (i = 0; i < 20; i += 5) printf(“%2d x %2d = %3d\n”, i, i, i *i); 5 x 5 = 25 0 x 0 = 0 15 x 15 = x10 = 100 Output

Standard I/O Functions – printf() What’s the output?: printf(“%8s5s\n”,”Decimal”,”Hex” for (i = 10; i < 16; i ++) printf(“%8d%5X\n”, i, i); Decimal Hex 10 A 11 B 12 C 13 D 15 E 16 F

Standard I/O Functions – printf() Consider this example: printf(“%-8s%-5s\n”, “Name”,”Age”); Name Age %-8s - 8 spaces are reserved for string ‘-’ sign signify left alignment %-5s - 5 spaces reserved for string left alignment

Standard I/O Functions – printf() What’s the output?: printf(“%-8s5s\n”, ”Decimal”, ”Hex”); Decimal Hex Output

Standard I/O Functions – printf() Consider this example: printf(“RM %8.2f\n”, ); %8.2f - 8 spaces are reserved for the floating point number where 2 of the spaces are for the fractional/precision part. Right Alignment - spaces at right side will be used first Simply change to %-8.2f to have left alignment If the number of spaces for fractional part is not specified, (example : %8f), default (6) would be used. If the spaces specified are not enough (example: %3f), default setting would be used. RM

Standard I/O Functions – printf() What’s the output printf(“%-3s%7.3f\n”,” RM”, ); RM printf(“%-.2f\n”, ); printf(“%4.2f\n”, );

Standard I/O Functions – printf() Can the field width be specified by a variable? YES! Consider this example for (i = 0; i < 4; i++) printf(“%.*f\n”, i, ); Output The asterisk ‘*’ is a variable width determined by variable i

Standard I/O Functions – printf() What’s output for (i = 1; i < 4; i++) printf(“%-*d%*d\n”, i, i, i, i); Output

Standard I/O Functions – printf() Develop an interactive program that can be used to teach multiplication to grade school students. The program should prompt the user to enter two three- digit integers, verify that they are indeed three-digit integers, and then print the product of the integers according to the following format 435 x times 435 is times 435 is times 435 is Add the products to get 64380