computer science department ftsm input and output knowledge: understand various types of input and...

Post on 24-Dec-2015

222 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Computer Science Department FTSMFTSM

Input and OutputInput and Output

Knowledge:Understand various types of input and output syntaxes

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

TK1913-C ProgrammingTK1913-C Programming 22

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()printf() and scanf()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 OutputInput and Output

TK1913-C ProgrammingTK1913-C Programming 33

printfprintf FunctionFunction General format for printfprintf function:

printf( output_format ,[value_list] );printf( output_format ,[value_list] );

output_formatoutput_format tells function about the format

that should be followed when displaying output

value_listvalue_list is a list of variables, constants,

expressions or combination of them, which

values are parts of the displayed output

TK1913-C ProgrammingTK1913-C Programming 44

printfprintf Function 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 What does \n\n mean?mean?

TK1913-C ProgrammingTK1913-C Programming 55

printfprintf Function Function printf()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_formatoutput_format. For example,

the position of the value when the output is

displayed

TK1913-C ProgrammingTK1913-C Programming 66

printfprintf Function Function

Formats for displaying output values: %s%s for stringstring %c%c for charactercharacter %d%d for integerinteger %f%f for floatfloat/doubledouble %e%e for floatfloat/double double (scientific notation)

TK1913-C ProgrammingTK1913-C Programming 77

printfprintf Function - String Function - StringFormat for string: %s%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 format Value (a string constant)

Output format

Output format

Value (an array of characters)

TK1913-C ProgrammingTK1913-C Programming 88

printfprintf Function - String 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 ProgrammingTK1913-C Programming 99

printfprintf Function - String 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 ProgrammingTK1913-C Programming 1010

printfprintf Function - Character Function - CharacterFormat for character: %c%c

Example:printf(“%c %c %c”, ‘U’, ‘K’, ‘M’);

Example:printf(“%c %c %c”, ‘U’, ‘K’, ‘M’);

U K M_

TK1913-C ProgrammingTK1913-C Programming 1111

printfprintf Function - Character Function - Character

Example:printf(“%c%c%c”, ‘U’, ‘K’, ‘M’);

Example:printf(“%c%c%c”, ‘U’, ‘K’, ‘M’);

UKM_

TK1913-C ProgrammingTK1913-C Programming 1212

printfprintf Function - Character Function - CharacterExample: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 ProgrammingTK1913-C Programming 1313

printfprintf Function - Integer Function - Integer Format for integer: %d%d General format:

%[<min_field_width>.<min_digit>]d%[<min_field_width>.<min_digit>]d

Example:

printf(“Value is:%10.6d”, 56342);

Example:

printf(“Value is:%10.6d”, 56342);

Value is: 056342

10 characters

6 digits

TK1913-C ProgrammingTK1913-C Programming 1414

printfprintf Function - Integer Function - Integer

Example:

printf(“Value is:%10.3d”, 56342);

Example:

printf(“Value is:%10.3d”, 56342);

Value is: 56342

10 characters

5 digits

Min 3 digits

TK1913-C ProgrammingTK1913-C Programming 1515

printfprintf Function - Integer Function - Integer

Example:

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

Example:

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

Value is:56342

5 charactersMin 4 characters

TK1913-C ProgrammingTK1913-C Programming 1616

printfprintf Function - Float Function - Float Format for float: %f%f General format:

%[<min_field_width>.<decimal_places>]f%[<min_field_width>.<decimal_places>]f

Example:

printf(“Value is:%10.4f”, 32.6784728);

Example:

printf(“Value is:%10.4f”, 32.6784728);

Value is: 32.6784

10 characters

4 digits

TK1913-C ProgrammingTK1913-C Programming 1717

printfprintf Function - Float Function - Float

Example:

printf(“Value is:%10f”, 32.6784728);

Example:

printf(“Value is:%10f”, 32.6784728);

Value is: 32.678473

10 characters

6 digits (default)

TK1913-C ProgrammingTK1913-C Programming 1818

printfprintf Function - Float Function - Float

Example:

printf(“Value is:%10.5f”, 32.6784);

Example:

printf(“Value is:%10.5f”, 32.6784);

Value is: 32.67840

10 characters

5 digits

TK1913-C ProgrammingTK1913-C Programming 1919

Example:

printf(“Value is:%5f”, 32.6784728);

Example:

printf(“Value is:%5f”, 32.6784728);

Value is:32.678473

6 digits (default)

printfprintf Function - Float Function - Float

9 characters Min 5 characters

TK1913-C ProgrammingTK1913-C Programming 2020

Example:

printf(“Value is:%.3f”, 32.6784728);

Example:

printf(“Value is:%.3f”, 32.6784728);

Value is:32.678

3 digits

printfprintf Function - Float Function - Float

TK1913-C ProgrammingTK1913-C Programming 2121

printfprintf Function - Float Function - FloatExample:#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);}

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);}

Ali is 21 years old and his height is 1.73000 meters

_

age ?

height ?

21

1.73

TK1913-C ProgrammingTK1913-C Programming 2222

scanfscanf Function Function General format for scanfscanf function:

scanf( input_format , list_of_variables );scanf( input_format , list_of_variables ); input_format input_format tells function about the format that should be

followed when capturing data list_of_variableslist_of_variables are locations in memory, in which the

captured data is kept input_formatinput_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 ProgrammingTK1913-C Programming 2323

scanfscanf Function FunctionExample:

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: m103

103

Key-in a character and a number: m103

Character: m

_

Key-in a character and a number: m103

Character: m

Number: 103

_

TK1913-C ProgrammingTK1913-C Programming 2424

scanfscanf Function FunctionExample:#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);}

Example:#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);}

16 12 2005

month ?

year ?

day ?16

12

2005

16 12 2005

Day: 16, Month: 12, Year: 2005_

TK1913-C ProgrammingTK1913-C Programming 2525

Conclusion & DiscussionConclusion & Discussion Your Project Any problem during lab and tutorial

sessions??

TK1913-C ProgrammingTK1913-C Programming 2626

End of Lecture 5End of Lecture 5

Yes !! That’s all? Yes !! That’s all? What’s next???What’s next???

OPERATORS & OPERATORS & EXPRESSIONSEXPRESSIONS on the way on the way … …

top related