Computer Science Department FTSM Input and Output Knowledge: Understand various types of input and output syntaxes Skill: Develop a program to read/capture.

Slides:



Advertisements
Similar presentations
Standard I/O Lesson Outline
Advertisements

C Programming. printf int printf ( const char * format,... ); printf ("Characters: %c \n", 'a'); printf ("Decimals: %d %f\n", 1977, 3.14); specifierOutputExample.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Computer Science Department FTSM Control Structure: Selection (Part 2) Knowledge: Understand various concepts of selection control structure Skill: Be.
Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 4 Outline 4.0 Reading.
Operators and Expressions
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
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.
Basic Input - Output. Output functions  printf() – is a library function that displays information on-screen. The statement can display a simple text.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
 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.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
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.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Yu Yuanming CSCI2100B Data Structures Tutorial 3
Data Types. Data types Data type tells the type of data, that you are going to store in memory. It gives the information to compiler that how much memory.
C Programming Lecture 5 : Basic standard I/O Lecture notes : courtesy of Ohio Supercomputing Center, science and technolgy support.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
C OMPUTER P ROGRAMMING 1 Assignment. A SSIGNMENT We have used gets to input a value into variable The second way to give a variable a value is known as.
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
1 MT258 Computer Programming and Problem Solving Tutorial 03.
Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.
Sudeshna Sarkar, IIT Kharagpur 1 I/O in C + Misc Lecture –
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
+ 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.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
INC 161 , CPE 100 Computer Programming
Decisions Chapter 4.
INTRODUCTION Every language has some features that provides interaction between the program and the user of the program. C language uses the reference.
Arrays Declarations CSCI N305
TMF1414 Introduction to Programming
Chapter 2 Overview of C.
Module 7: Input/Output Operations ITEI102 Introduction to Programming
C programming language
By: Syed Shahrukh Haider
OUTPUT STATEMENTS GC 201.
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.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
פרטים נוספים בסילבוס של הקורס
CSCE 206 Lab Structured Programming in C
Character & String Knowledge
Computer Science Department
Introduction to C Programming
C Data Types and Variable
CSCE 206 Lab Structured Programming in C
Introduction to C Programming
Getting Started With Coding
Presentation transcript:

Computer Science Department FTSM Input and Output Knowledge: Understand various types of input and output syntaxes Skill: Develop a program to read/capture input and display output

TK1913-C Programming2 TK1913-C Programming 2 printf()scanf() There are various functions available in C library that can be used for input and output activities. The two functions that will be explained here are printf() and scanf() printf() printf() function is used for displaying characters on output devices (normally video display) scanf() scanf() function is used for reading/capturing characters from input devices (normally keyboard) Input and Output

TK1913-C Programming3 TK1913-C Programming 3 printf Function printf General format for printf function: printf( output_format,[value_list] ); output_format output_format tells function about the format that should be followed when displaying output value_list value_list is a list of variables, constants, expressions or combination of them, which values are parts of the displayed output

TK1913-C Programming4 TK1913-C Programming 4 printf Function Example: printf(“TK1913 C Programming\n\n”); printf(“List of Students\n”); printf(“Ahmad bin Ali”); Example: printf(“TK1913 C Programming\n\n”); printf(“List of Students\n”); printf(“Ahmad bin Ali”); TK1913 C Programming _ TK1913 C Programming List of Students _ TK1913 C Programming List of Students Ahmad bin Ali _ What does \n mean?

TK1913-C Programming5 TK1913-C Programming 5 printf Function printf() printf() function can be used to display values of variable, constant and others output_format To display a value, the format of the value has to be stated in the output_format. For example, the position of the value when the output is displayed

TK1913-C Programming6 TK1913-C Programming 6 printf Function Formats for displaying output values: %sstring %s for string %ccharacter %c for character %dinteger %d for integer %ffloatdouble %f for float/double %efloatdouble %e for float/double (scientific notation)

TK1913-C Programming7 TK1913-C Programming 7 printf Function - String %s Format for string: %s Example: printf( “%s”, “Display a string\n” ); Similar to: printf( “Display a string\n” ); Example: printf( “%s”, “Display a string\n” ); Similar to: printf( “Display a string\n” ); Normally, it is used to display an array of characters Example: char name[ ] = “Nadiah”; printf( “%s”, name ); Example: char name[ ] = “Nadiah”; printf( “%s”, name ); Output formatValue (a string constant)Output format Value (an array of characters)

TK1913-C Programming8 TK1913-C Programming 8 printf Function - String Example: printf( “Name: %s\nStudent No: %s”, “Ali Bakar”, “A92333”); Example: printf( “Name: %s\nStudent No: %s”, “Ali Bakar”, “A92333”); Name: Ali Bakar Student No: A92333_ Output format

TK1913-C Programming9 TK1913-C Programming 9 printf Function - String Example: printf( “Name: %s\nStudent No: %s”, “Ali Bakar”, “A92333”); Example: printf( “Name: %s\nStudent No: %s”, “Ali Bakar”, “A92333”); Name: Ali Bakar Student No: A92333_

TK1913-C Programming10 TK1913-C Programming 10 printf Function - Character %c Format for character: %c Example: printf(“%c %c %c”, ‘U’, ‘K’, ‘M’); Example: printf(“%c %c %c”, ‘U’, ‘K’, ‘M’); U K M_

TK1913-C Programming11 TK1913-C Programming 11 printf Function - Character Example: printf(“%c%c%c”, ‘U’, ‘K’, ‘M’); Example: printf(“%c%c%c”, ‘U’, ‘K’, ‘M’); UKM_

TK1913-C Programming12 TK1913-C Programming 12 printf Function - Character Example: char1 = ‘U’; char2 = ‘K’; char3 = ‘M’; printf(“%c %c %c”, char1, char2, char3); Example: char1 = ‘U’; char2 = ‘K’; char3 = ‘M’; printf(“%c %c %c”, char1, char2, char3); U K M_ char1 U char2 ? char3 ? char1 U char2 K char3 ? char1 U char2 K char3 M

TK1913-C Programming13 TK1913-C Programming 13 printf Function - Integer %d Format for integer: %d General format:%[<min_field_width>.<min_digit>]d Example: printf(“Value is:%10.6d”, 56342); Example: printf(“Value is:%10.6d”, 56342); Value is: characters 6 digits

TK1913-C Programming14 TK1913-C Programming 14 printf Function - Integer Example : printf(“Value is:%10.3d”, 56342); Example : printf(“Value is:%10.3d”, 56342); Value is: characters 5 digits Min 3 digits

TK1913-C Programming15 TK1913-C Programming 15 printf Function - Integer Example : printf(“Value is:%10.4d”, 56342); Example : printf(“Value is:%10.4d”, 56342); Value is: characters Min 4 characters

TK1913-C Programming16 TK1913-C Programming 16 printf Function - Float %f Format for float: %f General format:%[<min_field_width>.<decimal_places>]f Example: printf(“Value is:%10.4f”, ); Example: printf(“Value is:%10.4f”, ); Value is: characters 4 digits

TK1913-C Programming17 TK1913-C Programming 17 printf Function - Float Example: printf(“Value is:%10f”, ); Example: printf(“Value is:%10f”, ); Value is: characters 6 digits (default)

TK1913-C Programming18 TK1913-C Programming 18 printf Function - Float Example: printf(“Value is:%10.5f”, ); Example: printf(“Value is:%10.5f”, ); Value is: characters 5 digits

TK1913-C Programming19 TK1913-C Programming 19 Example: printf(“Value is:%5f”, ); Example: printf(“Value is:%5f”, ); Value is: digits (default) printf Function - Float 9 characters Min 5 characters

TK1913-C Programming20 TK1913-C Programming 20 Example: printf(“Value is:%.3f”, ); Example: printf(“Value is:%.3f”, ); Value is: digits printf Function - Float

TK1913-C Programming21 TK1913-C Programming 21 printf Function - Float Example: #include void main( ) { int age; float height; age = 21; height = 1.73; printf(“Ali is %d years old and his height is %.5f meters\n”, age, height);} Example: #include void main( ) { int age; float height; age = 21; height = 1.73; printf(“Ali is %d years old and his height is %.5f meters\n”, age, height);} Ali is 21 years old and his height is meters _ age ? height ?

TK1913-C Programming22 TK1913-C Programming 22 scanf Function scanf General format for scanf function: scanf( input_format, list_of_variables ); input_format input_format tells function about the format that should be followed when capturing data list_of_variables list_of_variables are locations in memory, in which the captured data is kept input_format input_format should contain specification of each intended input data User needs to key-in data based on the format and specification set by the program

TK1913-C Programming23 TK1913-C Programming 23 scanf Function Example: printf(“Key-in a character and a number: “); scanf(“%c%d”, &char, &num); printf(“Character: %c\n”, char); printf(“Number: %d\n”, num); Example: printf(“Key-in a character and a number: “); scanf(“%c%d”, &char, &num); printf(“Character: %c\n”, char); printf(“Number: %d\n”, num); Key-in a character and a number: char ? num ? m Key-in a character and a number: m Key-in a character and a number: m103 Character: m _ Key-in a character and a number: m103 Character: m Number: 103 _

TK1913-C Programming24 TK1913-C Programming 24 scanf Function Example: #include void main( ) { int day, month, year; scanf(“%d %d %d”, &day, &month, &year); printf(“Day: %d, Month: %d, Year: %d”, day, month, year); } Example: #include void main( ) { int day, month, year; scanf(“%d %d %d”, &day, &month, &year); printf(“Day: %d, Month: %d, Year: %d”, day, month, year); } month ? year ? day ? Day: 16, Month: 12, Year: 2005_

TK1913-C Programming25 TK1913-C Programming 25 Conclusion & Discussion Your Project Any problem during lab and tutorial sessions??

TK1913-C Programming26 TK1913-C Programming 26 End of Lecture 5 Yes !! That’s all? What’s next??? OPERATORS & EXPRESSIONS on the way …