Computer Science Department

Slides:



Advertisements
Similar presentations
Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify.
Advertisements

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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Computer Science Department FTSM Input and Output Knowledge: Understand various types of input and output syntaxes Skill: Develop a program to read/capture.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
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.
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
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.
C language--Introduction. History 1970: B by Ken Thompson at AT&T Bell Lab 1972: C by Dennis Ritchie and Ken Tompson at At&T Bell Lab for UNIX 1978: “The.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Lecture 3: Getting Started & Input / Output (I/O)
Chapter 9 - Formatted Input/Output
Arithmetic Expressions
CSCE 206 Structured Programming in C
‘C’ Programming Structures and Commands
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
INC 161 , CPE 100 Computer Programming
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
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
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
I/O in C + Misc Lecture Sudeshna Sarkar, IIT Kharagpur.
Lecture2.
By: Syed Shahrukh Haider
OUTPUT STATEMENTS GC 201.
Visit for more Learning Resources
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.
The C “switch” Statement
Functions Declarations CSCI 230
CSI 121 Structured Programming Language Lecture 7: Input/Output
توابع ورودي-خروجي.
The C “switch” Statement
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
פרטים נוספים בסילבוס של הקורס
אבני היסוד של תוכנית ב- C
INC 161 , CPE 100 Computer Programming
CSCE 206 Lab Structured Programming in C
Character & String Knowledge
Chapter 2 - Introduction to C Programming
Chapter 9 - Formatted Input/Output
The switch Statement Topics Multiple Selection switch Statement
Chapter 2 - Introduction to C Programming
Relational, Logical, and Equality Operators
Control Structure: Selection (Part 2)
Introduction to C Programming
Chapter 2 - Introduction to C Programming
C Data Types and Variable
Character Arrays char string1[] = “first”;
CSCE 206 Lab Structured Programming in C
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Course Outcomes of Programming In C (PIC) (17212, C203):
Introduction to C Programming
Computer Science Department
Getting Started With Coding
Presentation transcript:

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

Input and Output 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() function is used for displaying characters on output devices (normally video display) scanf() function is used for reading/capturing characters from input devices (normally keyboard) TK1913-C Programming 2

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

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

printf Function printf() function can be used to display values of variable, constant and others 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 Programming 5

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

printf Function - Float Example: #include <stdio.h> 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);} age ? 21 height ? 1.73 Ali is 21 years old and his height is 1.73000 meters _ TK1913-C Programming 21

scanf Function General format for scanf function: scanf( input_format , list_of_variables ); input_format tells function about the format that should be followed when capturing data list_of_variables are locations in memory, in which the captured data is kept 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 Programming 22

scanf Function Example: ? m 103 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 103 Key-in a character and a number: m103 Character: m _ Key-in a character and a number: m103 Key-in a character and a number: m103 Character: m Number: 103 _ TK1913-C Programming 23

scanf Function ? 16 12 2005 Example: day month year #include <stdio.h> 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 16 12 2005 16 12 2005 16 12 2005 Day: 16, Month: 12, Year: 2005_ TK1913-C Programming 24

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

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