You are on page 1of 26

Input and Output

Knowledge:
Understand various types of input and output syntaxes

Skill:
Develop a program to read/capture input and display output

Computer Science Department

FTSM

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

printf 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

TK1913-C Programming

printf Function
Example:
printf(TK1913 C Programming\n\n);
printf(List of Students\n);
printf(Ahmad bin Ali);

What does \n
mean?

TK1913
TK1913 C
C Programming
Programming

List of Students
_ of Students
List
Ahmad bin Ali _
_
TK1913-C Programming

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

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

printf Function - String


Format for string: %s

Example:

Output format

Value (a string constant)

printf( %s , Display a string\n );

Similar to:
printf( Display a string\n );

Output format

Normally, it is used to display an array of characters

Example:

char name[ ] = Nadiah;


printf( %s , name );
Output
TK1913-C Programming
format

Value (an array of characters)


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

printf Function - String


Example:
printf( Name: %s\nStudent No: %s, Ali Bakar,
A92333);

Name: Ali Bakar


Student No: A92333_

TK1913-C Programming

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
char1 = U;
char2 = K;
char2 K
?
char3 = M;
char3 M
?
printf(%c %c %c, char1, char2, 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);
5 digits
Value is:

56342
Min 3 digits

10 characters

TK1913-C Programming

14

printf Function - Integer


Example:
printf(Value is:%10.4d, 56342);

Value is:56342

Min 4 characters

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);
6 digits (default)
Value is:32.678473
9 characters

TK1913-C Programming

Min 5 characters

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>
age 21
?
void main( ) {
int age;
height 1.73
?
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 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:
printf(Key-in a character and a number: );
scanf(%c%d, &char, &num);
printf(Character: %c\n, char);

char

m
?

num 103
?

printf(Number: %d\n, num);


Key-in a character and a number: m103
Character: m
_
Number:
103
_
TK1913-C Programming

23

scanf Function
Example:
#include <stdio.h>

day

16
?

month 12
?
void main( ) {
int day, month, year;
?
scanf(%d %d %d, &day, &month, &year); year 2005
printf(Day: %d, Month: %d, Year: %d, day, month,
year);
}
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 !! Thats all?
Whats next???

OPERATORS &
EXPRESSIONS on the way
TK1913-C Programming

26

You might also like