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

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

Fundamentals of Programming

Fundamental of Programming (C)

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

Unit 4. Input/Output Functions

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

Introduction to Computing Lecture 03: Basic input / output operations

Formatted Output Pearson Education, Inc. All rights reserved.

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Fundamentals of Programming Session 8

CC112 Structured Programming

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

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

Introduction to Programming

Number Systems, Scalar Types, and Input and Output

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

C: How to Program. Week /Mar/05

Fundamentals of Programming. Lecture 11: C Characters and Strings

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

Input/Output Week 5:Lesson 16.1

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

AWK - PRETTY PRINTING

Chapter 2 - Introduction to C Programming

Fundamentals of C. Structure of a C Program

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

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

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

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

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

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

Chapter 8 - Characters and Strings

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

Advanced C Programming Topics

Basic Types and Formatted I/O

EC 413 Computer Organization

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

Fundamentals of Programming Session 4

Input/Output: Advanced Concepts

Formatted Input/Output

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

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

Computers Programming Course 5. Iulian Năstac

UNIT-I Input/ Output functions and other library functions

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Standard I/O in C and C++

THE FUNDAMENTAL DATA TYPES

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

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

Introduction to C. Systems Programming Concepts

Console Input and Output

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

Introduction to the C Programming Language

UNIT 3 OPERATORS. [Marks- 12]

BSM540 Basics of C Language

Lesson 3 Introduction to Programming in C

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

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Standard File Pointers

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

Programming for Engineers Introduction to C

ANSI C Programming Simple Programs

Should you know scanf and printf?

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Fundamental of Programming (C)

C: How to Program. Week /May/28

Basic Elements of C. Staff Incharge: S.Sasirekha

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

Intermediate Programming, Spring 2017*

11 Console Input/Output

Programming in C++ 4. The lexical basis of C++

Reserved Words and Identifiers

File IO and command line input CSE 2451

بسم اهلل الرمحن الرحيم

Time (self-scheduled): Location Schedule Your Exam: What to bring:

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

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

Structured Programming. Dr. Mohamed Khedr Lecture 4

WARM UP LESSONS BARE BASICS

(T) x. Casts. A cast converts the value held in variable x to type T

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

211: Computer Architecture Summer 2016

Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE

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

Data Types. Data Types. Integer Types. Signed Integers

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

Fundamentals of Programming Session 19

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

CSC 1107: Structured Programming

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Chapter 2, Part I Introduction to C Programming

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Chapter 1 & 2 Introduction to C Language

BSM540 Basics of C Language

Formatting functions in C Language

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark]

Chapter 3. Fundamental Data Types

Transcription:

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU

Outline 2 printf and scanf Streams (input and output) gets, puts getchar, putchar

Formatting output with printf 4 Syntax printf("format control string", other arguments); Format control string could include Conversion specifications (begin with %) Flags Filed widths Precisions Literal characters Other arguments: optional Each one corresponds to a conversion specification

Example of printf 5 conversion specification int numdogs = 2, numcats = 1; printf("i have %d dogs and %d cats\n", numdogs, numcats); format control string argument

Printing integers 6 Positive, negative, and zero Integer conversion specification: %d: signed decimal integer %i: signed decimal integer (different from %d in scanf) %o: unsigned octal integer %u: unsigned decimal integer %x: unsigned hex integer (0-9, a-f) %X: unsigned hex integer (0-9, A-F) %h or %l: placed before any integer conversion specifier to indicate a short or long integer

Example of printing integer 7 01 /* Fig 9.2: fig09_02.c*/ 02 /* Using the integer conversion specifiers */ 03 #include <stdio.h> 04 05 int main(void) 06 { 07 printf("%d\n", 455); 08 printf("%i\n", 455); 09 printf("%d\n", +455); 10 printf("%d\n", -455); 11 printf("%hd\n", 32000); 12 printf("%ld\n", 2000000000L); 13 printf("%o\n", 455); 14 printf("%u\n", 455); 15 printf("%u\n", -455); 16 printf("%x\n", 455); 17 printf("%x\n", 455); 18 return 0; 19 } /* end main */ Output: 455 455 455-455 32000 2000000000L 707 455 4294955841 1c7 1C7

Printing floating-point number 8 Floating point or exponential notation 153.2 = 1.532 x 10 2 = 1.532E2 Floating point conversion specification: %e or %E: Display floating-point in exponential notation %f: Display floating-point in fixed-point notation %g or %G: Display floating-point in either exponential or fixed-point based on the magnitude of the value %L: Placed before any floating-point conversion specifier to indicate a long double

Example of printing floating-point 9 01 /* Fig 9.4: fig09_04.c*/ 02 /* Printing with floating-point conversion specifiers */ 03 #include <stdio.h> 04 05 int main(void) 06 { 07 printf("%e\n", 1234567.89); 08 printf("%e\n", +1234567.89); 09 printf("%e\n", -1234567.89); 10 printf("%e\n", 1234567.89); 11 printf("%f\n", 1234567.89); 12 printf("%g\n", 1234567.89); 13 printf("%g\n", 1234567.89); 14 15 return 0; 16 } /* end main */ Output: 1.2345678e+006 1.2345678e+006-1.2345678e+006 1.2345678E+006 1234567.890000 1.234567e+006 1.234567E+006

Printing Strings and Characters 10 %c: Print character Single quotes for character constant ( z, a, or \n ) Print the first character of a string %s: printf("%c, string ); // output: s Print string Double quotes for strings ( z, string ) Can not print characters s s s s \0

Example of printing strings 11 01 /* Fig 9.5: fig09_05.c*/ 02 /* Printing string and characters */ 03 #include <stdio.h> 04 05 int main(void) 06 { 07 char ch = 'a'; /* char */ 08 char string[] = "This is a string."; /* char array */ 09 const char *stringptr = "Also a string"; /* char pointer */ 10 11 printf("%c\n", ch); 12 printf("%s\n", "This is a string."); 13 printf("%s\n", string); 14 printf("%s\n", stringptr); 15 16 return 0; 17 } /* end main */ a This is a string. This is a string. Also a string Output

Other conversion specifiers 12 %p Display the address of a pointer value %% Print a percent sign (%)

Printing with field widths 13 Size of field in which data is printed If the width is larger than data, default right justified If the field width is too small, it is increased to fit data Minus sign occupies one character in the field Integer width inserted between % and conversion specifier %4d: field width is 4 printf( print %4d, 23); p r i n t 2 3

Example of field widths 14 printf( %4d\n, 1); printf( %4d\n, 12); printf( %4d\n, 123); printf( %4d\n, 1234); printf( %4d\n, 12345); printf( %4d\n, -1); printf( %4d\n, -12); printf( %4d\n, -123); printf( %4d\n, -1234); printf( %4d\n, -12345); 1111 1112 1123 1234 12345 11-1 1-12 -123-1234 -12345

Printing with precision 15 Usage format % + dot (.) + precision number + conversion specifies Example: %.4d, %.4f Definition varies with data types For integer Minimal number of digits to print If data is too small, prefixed with zeros Example: printf( %.4d\n, 12); => ourput: 0012 For floating point Number of digits to appear after decimal Example: printf( %.4f\n, 12.34) => output: 12.3400 For string Maximum number of characters to be written from string

Example of precision 16 int i = 123; printf( %.4d\n%.9d\n, i, i); double f = 1234.5678; printf( %.3f\n%.3e\n%.4g\n, f, f, f,); char s*+ = happy birthday ; printf( %.11s\n, s); 0123 000000123 1234.567 1.234e+003 1235 happy birth

Flags 17 Placed right after (%) Several flags may be combined Flag Description - Left justify the output + Display + in front of a positive value (space) Display a space in front of a positive value # Display 0 in front of an octal integer Display 0x or 0X in front of a hex integer 0 Pad a field with leading zeros

Example of flags 18 printf( %5d students\n, 100); printf( %-5d students\n, 100); printf( %05d students\n, 100); printf( %+d\n%d\n, 100, 100); printf( % d\n%d\n, 100, -100); printf( %#o\n, 1427); printf( %#x\n%#x\n, 1427, 1427); 00100 students 10000 students 00100 students +100 100 100-100 02623 0x593 0X593

Escape sequences 19 Escape sequence Output \ Single quote ( ) \ Double quote ( ) \? Question mark (?) \\ Backslash (\) \a Bell \b Backspace (Move the cursor back one position) \f Move the cursor to the start of the next page \n Move the cursor to 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 output with scanf 20 Syntax scanf("format control string", &var1, &var2, ); int a; scanf( %d, &a); // read integer double f; scanf( %lf, &f); // read double floating point char ch; scanf( %c, &ch); // read character char s[10]; scanf( %s, s); // read string (should notice the size of string )