Title: Input and Output
1Input and Output
Knowledge Understand various types of input and
output syntaxes Skill Develop a program to
read/capture input and display output
2Input 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)
3printf Function
- 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
4printf Function
- Example
- printf(TK1913 C Programming\n\n)
- printf(List of Students\n)
- printf(Ahmad bin Ali)
TK1913 C Programming List of Students Ahmad bin
Ali _
TK1913 C Programming _
TK1913 C Programming List of Students _
5printf 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
6printf 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)
7printf Function - String
Format for string s
- 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 )
8printf Function - String
- Example
- printf( Name s\nStudent No s, Ali Bakar,
A92333)
Name Ali Bakar Student No A92333_
9printf Function - String
- Example
- printf( Name s\nStudent No s, Ali Bakar,
A92333)
Name Ali Bakar Student No A92333_
10printf Function - Character
Format for character c
Example printf(c c c, U, K, M)
U K M_
11printf Function - Character
Example printf(ccc, U, K, M)
UKM_
12printf Function - Character
Example char1 U char2 K char3
M printf(c c c, char1, char2, char3)
U K M_
13printf Function - Integer
- Format for integer d
- General format
- ltmin_field_widthgt.ltmin_digitgtd
Example printf(Value is10.6d, 56342)
Value is 056342
14printf Function - Integer
Example printf(Value is10.3d, 56342)
Value is 56342
15printf Function - Integer
Example printf(Value is10.4d, 56342)
Value is56342
16printf Function - Float
- Format for float f
- General format
- ltmin_field_widthgt.ltdecimal_placesgtf
Example printf(Value is10.4f, 32.6784728)
Value is 32.6784
17printf Function - Float
Example printf(Value is10f, 32.6784728)
Value is 32.678473
18printf Function - Float
Example printf(Value is10.5f, 32.6784)
Value is 32.67840
19printf Function - Float
Example printf(Value is5f, 32.6784728)
Value is32.678473
20printf Function - Float
Example printf(Value is.3f, 32.6784728)
Value is32.678
21printf Function - Float
Example include ltstdio.hgt 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)
21
1.73
Ali is 21 years old and his height is 1.73000
meters _
22scanf 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
23scanf Function
Example printf(Key-in a character and a number
) scanf(cd, char, num) printf(Character
c\n, char) printf(Number d\n, num)
m
103
Key-in a character and a number m103
Key-in a character and a number m103 Character
m _
Key-in a character and a number m103 Character
m Number 103 _
24scanf Function
Example include ltstdio.hgt void main( ) int
day, month, year scanf(d d d, day,
month, year) printf(Day d, Month d,
Year d, day, month, year)
16
12
2005
16 12 2005
16 12 2005 Day 16, Month 12, Year 2005_
25Conclusion Discussion
- Your Project
- Any problem during lab and tutorial sessions??
26End of Lecture 5