scanf - Read formatted data from the standard input stream. int scanf ( const char* format [, argument]...) ;

Similar documents
FEEG Applied Programming 5 - Tutorial Session

The programming language C. sws1 1

Sources: On the Web: Slides will be available on:

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

System Calls and Standard I/O

Secrets of printf. 1 Background. 2 Simple Printing. Professor Don Colton. Brigham Young University Hawaii. 2.1 Naturally Special Characters

Pemrograman Dasar. Basic Elements Of Java

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window)

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

System Calls Related to File Manipulation

Number Representation

Module 816. File Management in C. M. Campbell 1993 Deakin University

C++ Input/Output: Streams

arrays C Programming Language - Arrays

Stacks. Linear data structures

The string of digits in the binary number system represents the quantity

This section describes how LabVIEW stores data in memory for controls, indicators, wires, and other objects.

Lex et Yacc, exemples introductifs

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

So far we have considered only numeric processing, i.e. processing of numeric data represented

The C Programming Language course syllabus associate level

First Bytes Programming Lab 2

Version 1.5 Satlantic Inc.

.NET Standard DateTime Format Strings

File Handling. What is a file?

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Numbering Systems. InThisAppendix...

The Answer to the 14 Most Frequently Asked Modbus Questions

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

CSI 333 Lecture 1 Number Systems

Formatting Numbers with C++ Output Streams

Lecture 2. Binary and Hexadecimal Numbers

Useful Number Systems

Chapter 2: Elements of Java

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Informatica e Sistemi in Tempo Reale

Unsigned Conversions from Decimal or to Decimal and other Number Systems

Programming languages C

Simple C Programs. Goals for this Lecture. Help you learn about:

COMPSCI 210. Binary Fractions. Agenda & Reading

Member Functions of the istream Class

3 Data Properties and Validation Rules

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

Data Storage: Each time you create a variable in memory, a certain amount of memory is allocated for that variable based on its data type (or class).

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Solution for Homework 2

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

How To Write Portable Programs In C

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

C++ Language Tutorial

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

HOMEWORK # 2 SOLUTIO

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Thermal Control Board User Manual 1

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

DNA Data and Program Representation. Alexandre David

CP Lab 2: Writing programs for simple arithmetic problems

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

Using Format Manager For Creating Format Files

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

HP Service Virtualization

Computer Science 281 Binary and Hexadecimal Review

Introduction to Java

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

Chapter 4: Computer Codes

Systems I: Computer Organization and Architecture

5 Arrays and Pointers

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

LOW LEVEL FILE PROCESSING

C Examples! Jennifer Rexford!

Computer Science 2nd Year Solved Exercise. Programs 3/4/2013. Aumir Shabbir Pakistan School & College Muscat. Important. Chapter # 3.

Base Conversion written by Cathy Saxton

Channel Access Client Programming. Andrew Johnson Computer Scientist, AES-SSG

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11

Basics of I/O Streams and File I/O

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Chapter Binary, Octal, Decimal, and Hexadecimal Calculations

C PROGRAMMING FOR MATHEMATICAL COMPUTING

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

PFB366 Profibus-DP Gateway User Manual

7th Marathon of Parallel Programming WSCAD-SSC/SBAC-PAD-2012

MS ACCESS DATABASE DATA TYPES

The New IoT Standard: Any App for Any Device Using Any Data Format. Mike Weiner Product Manager, Omega DevCloud KORE Telematics

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Version 4 MAT-File Format

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Chapter 7D The Java Virtual Machine

IPC. Semaphores were chosen for synchronisation (out of several options).

J a v a Quiz (Unit 3, Test 0 Practice)

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Transcription:

scanf - Read formatted data from the standard input stream. int scanf ( const char* format [, argument]...) ; Parameters format - Format control string. argument - Optional arguments. Return Value The number of fields converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered. Remarks The scanf function reads data from the standard input stream stdin and writes the data into the location given by argument. Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format. Type Characters for scanf functions Character Type of Input Expected Type of Argument c Single byte character. Pointer to char. d/ld Decimal integer/long integer. Pointer to int/long int. f Floating point value. Pointer to float. lf Double value. Pointer to double. int i, result; float fp; char c; Enter an int, a float and a char: 71 98.6 h The number of fields input is 3. The contents are: 71 98.599998 h. printf( "\n\nenter an int, a float and a char:\n"); result = scanf( "%d %f %c", &i, &fp, &c); printf( "\nthe number of fields input is %d.\n", result); printf( "The contents are: %d %f %c.\n", i, fp, c);

Always check for return value from scanf function!!! char ch; int num1, num2; int result; printf("\n"); result = scanf("%d %d", &num1, &num2); if (result == 0) printf("read no integers.\n"); else if (result == EOF) printf("ops, the file is empty.\n"); return 1; /* Return 1 for unsuccessful termination. */ printf("number of successful inputs: %d.\n", result); printf("\n"); result = scanf("%c", &ch); if (result == -1) /* EOF = -1 */ printf("read no character.\n"); return 1; printf("number of successful inputs: %d.\n", result); >test.exe < input.dat input.dat result variable value 3 4 h Number of successful inputs: 2. num1 = 3 num2 = 4 ch = 'h' 2.3 3 h num1 = 2 num2 = 4206636 ch = 'h' Ops, the file is empty. 4 5 Number of successful inputs: 2. Read no character. num1 = 4 num2 = 5 ch = ''

printf - Print formatted output to the standard output stream. int printf (const char* format [, argument] ); Parameters format - Format control (%[flags] [width] [.precision] type) argument - Optional arguments. Return Value The number of characters printed, or a negative value if an error occurs. Remarks The printf function formats and prints a series of characters and values to the standard output stream, stdout. Example: char gender = 'm'; int age = 34; int weight_lb = 165; int height_f = 5; int height_i = 11; int height_cm = 180; double weight_kg = 74.8427411; printf("athlete:lance Armstrong\n"); printf("gender:\t%c\n", gender); printf("age:\t%d\n", age); printf("weight:\t%d lbs/%.2lf kgs\n", weight_lb, weight_kg); printf("height:\t%d cms/%d feet %d inches\n", height_cm, height_f, height_i); Athlete:Lance Armstrong Gender: m Age: 34 Weight: 165 lbs/74.84 kgs Height: 180 cms/5 feet 11 inches

printf Type Field Characters Character Type Output format c int Specifies a single-byte character. d int Signed decimal integer. o int Unsigned octal integer. u int Unsigned decimal integer. x int Unsigned hexadecimal integer. e double Signed value having the form [ ]d.dddd e [sign]dd[d] where d is a single decimal digit, dddd is one or more decimal digits, dd[d] is two or three decimal digits depending on the output format and size of the exponent, and sign is + or. f double Signed value having the form [ ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision. p Pointer to void Prints the address of the argument in hexadecimal digits. The first optional field of the format specification is flags. A flag directive is a character that justifies output and prints signs, blanks, decimal points, and octal and hexadecimal prefixes. More than one flag directive may appear in a format specification. Flag Characters Flag Meaning Default Left align the result within the given field width. Right align. + Prefix the output value with a sign (+ or ) if the output value is of a signed type. 0 If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and appear, the 0 is ignored. blank (' ') Prefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear. Sign appears only for negative signed values ( ). No padding. No blank appears.

char ch = 'k'; int int1 = 10, int2 = -10; float f = 8.1234; double d1 = 12.123456789, d2 = -12.123456789, d3 = 0.123456789; printf("integer PRINTING:\n"); printf("=================\n"); printf("printing char: %c\n", ch); printf("printing decimal integer: %d\n", int2); printf("printing decimal integer: %d\n", int1); printf("printing unsigned octal integer: %o\n", int1); printf("printing unsigned decimal integer: %u\n", int2); printf("printing unsigned hexadecimal integer: %x\n\n", int1); printf("floating POINT PRINTING:\n"); printf("========================\n"); printf("printing float: %f\n", f); printf("printing double: %.9e\n", d1); printf("printing double: %.9e\n", d2); printf("printing double: %.9e\n", d3); printf("printing double: %lf\n\n", d1); printf("address PRINTING:\n"); printf("=================\n"); printf("printing address: %p\n\n", d1); printf("alignment:\n"); printf("==========\n"); printf("right(default) align: *%7d*\n", int1); printf("left align: *%-7d*\n\n", int1); printf("sign:\n"); printf("=====\n"); printf("sign prefix: %+d\n\n", int1); printf("zero PADDING:\n"); printf("=============\n"); printf("zero padding: %05d\n", int1); printf("ignoring zero padding: %-05d\n\n", int1); printf("blank PREFIX:\n"); printf("=============\n"); printf("no blank prefix: %d\n", int1); printf("blank prefix: *% d*\n", int1); printf("ignoring blank prefix: *% +d*\n\n", int1); INTEGER PRINTING: ================= Printing char: k Printing decimal integer: -10 Printing decimal integer: 10 Printing unsigned octal integ Printing unsigned decimal int Printing unsigned hexadecimal FLOATING POINT PRINTING: ======================== Printing float: 8.123400 Printing double: 1.212345679e Printing double: -1.212345679 Printing double: 1.234567890e Printing double: 12.123457 ADDRESS PRINTING: ================= Printing address: BA6E72C7 ALIGNMENT: ========== Right(default) align: * 10* Left align: *10 * SIGN: ===== Sign prefix: +10 ZERO PADDING: ============= Zero padding: 00010 Ignoring zero padding: 10 BLANK PREFIX: ============= No blank prefix: 10 Blank prefix: * 10* Ignoring blank prefix: *+10*