CS 125 Lecture 9 Martin van Bommel. Output printf used to display integers, reals (floats and doubles), and strings printf(” control string ”, exp 1,

Slides:



Advertisements
Similar presentations
Chapter Three Arithmetic Expressions and Assignment Statements
Advertisements

C Programming. printf int printf ( const char * format,... ); printf ("Characters: %c \n", 'a'); printf ("Decimals: %d %f\n", 1977, 3.14); specifierOutputExample.
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2013 Lecture 6: printf() formatting.
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.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck.
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()
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.
CS201 – Expressions & I/O. Precedence What is the value of * 4 ? 20 or 14 ? Without parentheses, there are a set of rules to fall back on. Unary.
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.
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.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
 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.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
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 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
 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.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
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.
Formatted Output CSE 1310 – Introduction to Computers and Programming 1.
Chapter 7 Formatted input and output. 7.1 introduction Tax: This result is correct; but it would be better Maybe as $13, Make formatting.
 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Discussion 2 Number Systems and their importance.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
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.
CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel.
Standard I/O Functions – printf() Without specifying any display formatting, the printf() function will use DEFAULT setting: Print in a field of minimum.
Copyright © Curt Hill Formatting Reals Outputs other than normal.
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.
CS 160 Lecture 4 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Simple Console Output CS 21a. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
+ 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.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
ECE Application Programming
Formatted Input and Output
ECE Application Programming
Standard Input - Output
TMF1414 Introduction to Programming
Unit-4, Chapter-2 Managing Input and Output Operations
By: Syed Shahrukh Haider
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.
System.out.println for console output
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Lecture 13 Input/Output Files.
Conversion Check your class notes and given examples at class.
Data Types and Expressions
Presentation transcript:

CS 125 Lecture 9 Martin van Bommel

Output printf used to display integers, reals (floats and doubles), and strings printf(” control string ”, exp 1, exp 2,…) printf(”Measurement is ”); printf(”%d ft %d in\n”,feet,inch);

Format Codes FormatPurpose %d Decimal Integer %f Floating point %lf Double (Long float) %e Exponential (d.dddddde  xx) %g General (uses shorter of %lf or %g ) %s String % Percent Sign

Formatted Output Need to align numbers in columns NameNumberSalary John Joan Field width - number of positions for value Alignment - left or right aligned Precision - number of digits beyond decimal

Formatted printf printf(”%sw.pf”, variable ); s - sign for alignment (none=right, - =left) w - field width - number of characters used (padded with blanks) p - precision - digits after decimal f - format code ( d,f,e,g,s)

Format Example NameNumberSalary John Joan printf(”%-8s%5d%10.2f”,n,m,s);

Crafting a Program Use comments to tell readers what they need to know, including difficult code Use indentation to show bodies of loops Use meaningful names Use convention for names - lastNumber Use standard idioms when appropriate Avoid unnecessary complexity

Designing for Change #define construct - symbolic constants #define symbol value symbol - name for symbolic constant value - replaces symbol in precompilation Why? Easier to modify program