Chapter 2 :INPUT AND OUTPUTPROCESSDTI 2143: Computer Programming
OBJECTIVES-Part 1To understand input and output streams. To be able to use all print formatting capabilities. To be able to use all input formatting capabilities. To be able to print with field widths and precisions. To be able to use formatting flags in the printf format control string. To be able to output literals and escape sequences.
printf() function3printf()To send data to output devicefunctionTo display output/dataprintf(“output_format”,print_list) ;formatexampleprintf(“First example“) ;printf(“The bread price is RM %lf“,price) ;
printf() function (continued..)4printf(“output_format”,print_list) ;formatmayconsiststextOutput_formatplaceholderInside“ ”combinationmayconsistvariableconstantPrint_listexpressionfunction_name
printf() function (cont..)5Example 1:printf(“UniversitiTun Hussein Onn Malaysia”);Output format -TextOutput:UniversitiTun Hussein Onn Malaysia
printf() function (cont..)6Example 2:printf(“%lf”, salary); variableFormat output-placeholderOutput:
printf() function (continued..)7Example 3:printf(“Monthly salary is RM %lf”, salary); variableOutput format -TextplaceholderOutput:Monthly salary is RM Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
printf() (function continued..)8Example 4:const float porosity = 16.78;printf(“The porosity of sand = %lf ”, porosity); Output format -TextconstantplaceholderOutput:The porosity of sand = 16.780001 Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
printf() function (cont..)9escape CharacterAdditional formatting to be performed with the printf() functionfunction
printf() function (cont..)10PlaceholderfunctionA symbol beginning with % in a format string that indicates where to display the output valueformatUsing percent symbol (%) followed by characterwhich represent particular data type
printf() function (cont..)11Example 5 (Printing character value ) :char huruf1, huruf2;huruf1 = ‘B’;huruf2 = ‘P’;printf(“%c%c%c”, ‘A’, ‘L’, ‘I’);printf(“\nI live in %c%c”,huruf1,huruf2); Output:ALII live in BP
printf() function (cont..)12Example 6 (Printing integer value) :int number1, number2;printf(“My house number is %d”, 26);number2=10;printf(“\n\tNumber %d and %d”,number1,number2);number2=2;number1= 15;printf(“\nNumber %d and %d”,number2,number1);Output:My house number is 26	Number 906 and 10Number 2 and 15
printf() function (continued..)13Example 6 (Printing floating point number) :double sahih1, sahih2;printf(“The value of pi is %lf”, 3.142);sahih1= 23.46;printf(“\nThe value of sahih1 is %lf”,sahih1);sahih2= 15.78;printf(“\tThe value of sahih2 is %lf’,sahih2);Output:The value of pi ialah 3.142The value of sahih1 is 23.46	The value of sahih2 is 15.78Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
printf() function (cont..)141. Given a = 2, b = 4. What are the output produced by the following statements?a) printf(“The additionresult of two number >> %d”,a + b);b) printf(“\nThe addition of %d and %d “, a,b);2. Write a valid C programming statement to display the following output (use variables): (Notes: Given the value of variable x = 2.5 and y = 3.1The value of x is 2.5cm and y is 3.1cmThe multiplication result of x and y is 7.75cm
scanf() functionscanf()To accept inputfunctionTo read input from keyboardscanf(“input_format”,&variable_list) ;formatscanf(“%d”,&nom) ;examplescanf(“%s %d %lf”,&name,&age,&weight) ;15
scanf() function (cont..)16consistsexampleinput_formatplaceholder%d @ %c @%lfKnown as ampersand ‘&’ symbolVariable address/location referenceconsistoutput_listvariableconstantexpressionfunctionNot allowed!!!
scanf() function (cont..)17Example1:scanf(“%lf %d %c”,&float_num, &num,&letter); VariablePlaceholderThe above statement is valid!Warning!!!!!Every variables to be used in the program MUST be declared!!!
scanf() function (cont..)18Example 2:scanf(“ %d ”, 15); Constant valuePlaceholderThe above statement is INVALID!scanf(“ %lf ”, &price + 0.05); Conversion characterExpressionThe above statement is INVALID!
scanf() function (cont..)19Example 3(program code):#include <stdio.h>void main(){ int semester;float CPA;printf(“Enter the semester : “);scanf(“%d”, &semester);printf(“\nEnter your CPA : “);scanf(“%lf”,&CPA);printf(“\nYour CPA for semester %d is %f”,semester,CPA);}???Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
scanf() function (cont..)20Output:Enter the semester : 3Enter your CPA : 3.45Your CPA for semester 3 is 3.45
scanf() function (continue..)211).Write the scanf() statement to accept the following type of variables:	a. char jantina;	b. float berat;	c. inttempoh;2).State whether the following scanf() statements is VALID or INVALID. Given the following declaration:int a, b;	char c;	a) scanf(“%d”, &a + b); b) scanf(“%c”, &c); c) scanf(“%d”,&a,&b);
Other I/O functions22Similar functions used in the input and output operations in C programming such as:getchar() and putchar() function
getch() danputch() function
gets() dan puts() functionOther I/O functions23To read any input from input device as character data typegetchar()functionvariable= getchar();formatchar letter;printf(“Enter one letter>>”);letter= getchar();example usage
Other I/O functions24To display output in a single character valueputchar()functionputchar(variable);formatprintf(“The input letter is ”);putchar(letter);example usageHazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
Other I/O functions25Example 1: getchar() and putchar()#include <stdio.h>void main(){ char letter;printf(“Enter one letter : “); letter = getchar();printf(“The input letter is\t“);putchar(letter);}Enter one letter :eThe input letter is e
Other I/O functions26Example 2: getchar() and putchar()However, it can also be program like this :#include <stdio.h>void main(){ int number;printf(“Enter one number : “); number = getchar();printf(“The input number is %d\n“,number);putchar(number);}Enter one number :123The input number is 491
Other I/O functionsTo read an input and terminate the program automaticallygetch()functionvariable= getch();formatchar letter;printf(“Enter one letter >>”);letter= getch();examplegetch() have same function like getchar(), but different action27Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
Other I/O functionsExample 3: getch()We can use getch() to hold user screen, and terminate onceany input is read#include <stdio.h>void main(){ int number;printf(“Enter one number : “); number = getchar();printf(“The input number is %d\n“,number);getch();}28Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
Other I/O functionsTo display output , have similar function like putcharputch()functionputch (variable);formatprintf(“The input letter is ”);putch(letter);example29
Other I/O functions30To read string input from input devicegets()functiongets(variable);formatchar name[20];printf(“Enter your name >>”);gets(name);exampleHazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
Other I/O functionsExample 4: gets()If we use scanf() function to read string input, it will only takes the first data between two words (if any)#include <stdio.h>void main(){ char name[20];printf(“Enter your name : “);scanf(“%s”,&name);printf(“Your name is %s “,name);getch();}Enter your name: SitiAminahYour name is Siti31Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
Other I/O functions32Example 5: gets()If we use gets() function to read string input, it will takes all thedata including space between two words (if any)#include <stdio.h>void main(){ char name[20];printf(“Enter your name : “); gets(name);printf(“Your name is %s\n“,name);getch();}Enter your name: SitiAminahYour name is SitiAminahHazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
Other I/O functions33To display string output to output deviceputs()functionsputs (variable);formatprintf(“Your name is ”);puts(name);exampleHazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
Output formatting34Integer formattingTo format the display of an integervaluefunctions%spacedformatUse the integer formatting in theoutput statementWhere to use?Provide spaces according to formatting
Put the value to display starting from right to left sideIf there is – sign in the formatting, values will be printed starting from left to right sideHow it works?
35Output FormattingAssume _ isan empty spaceInteger formattingexampleoutputprintf(“%3d”,123);123outputexampleprintf(“%5d”,123);_ _123outputexampleprintf(“%10d”,123);_ _ _ _ _ _ _123outputexampleprintf(“%-6d”,123);123_ _ _outputexampleprintf(“%-4d%4d”,123,456);123_ _456Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
Output Formatting36Example 1: Integer formatting#include <stdio.h>void main(){ printf(“%d “,123);printf(“\n%3d “,123);printf(“\n%5d “,123); print(“\n%10d”,123); print(“\n%-6d”,123); print(“\n%-4d%d4d”,123,456);}Output?
Output formatting37FloatingnumberformattingTo format the display of the floatingnumber valuefunctions%spacefformatUse in the output statementWhere to use?Provide spaces according to formatting
Put the value to display starting from right to left sideIf there is – sign in the formatting, values will be printed starting from left to right sideHow it works?
Output formatting38formatFloating number formatting%spacefoutputexampleprintf(“%10f”,57.78);_57.780000exampleoutputprintf(“%15f”,57.78);_ _ _ _ _ _57.780000exampleoutputprintf(“%-10f”,57.78);57.780000_outputexampleprintf(“%15f”,57.78);57.780000_ _ _ _ _ _
39Output formattingformatFloating point example%fexampleoutputprintf(“%.3f”,57.78);57.780exampleoutputprintf(“%.2f”,57.78);57.78exampleoutputprintf(“%.1f”,57.78);57.8exampleoutputprintf(“%6.2f”,57.78);57.78_ exampleoutputprintf(“%-6.2f”,57.78);_57.78Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
40Output formattingformatString format%<space>cchar huruf = ‘A’;printf(“%c”,huruf);printf(“%2c”,huruf);printf(“%-5c”,huruf);A_AA_ _ _ _exampleoutputHazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
41Format Output (cont..)formatString formatting%<spaces> schar ayat[] =“KUITTHO PARIT RAJA”printf(“%s”,ayat);printf(“%4s”,ayat);printf(“%22s”,ayat);printf(“%.7s”,ayat);printf(“%-20.13s”,ayat);outputKUITTHO PARIT RAJAKUITTHO PARIT RAJA_ _ _ _KUITTHO PARIT RAJAKUITTHOKUITTHO PARIT_ _ _ _ _ _ _
Quick RecapTable 1- Conversion specifiers for scanf

Chap 2 input output dti2143

  • 1.
    Chapter 2 :INPUTAND OUTPUTPROCESSDTI 2143: Computer Programming
  • 2.
    OBJECTIVES-Part 1To understandinput and output streams. To be able to use all print formatting capabilities. To be able to use all input formatting capabilities. To be able to print with field widths and precisions. To be able to use formatting flags in the printf format control string. To be able to output literals and escape sequences.
  • 3.
    printf() function3printf()To senddata to output devicefunctionTo display output/dataprintf(“output_format”,print_list) ;formatexampleprintf(“First example“) ;printf(“The bread price is RM %lf“,price) ;
  • 4.
    printf() function(continued..)4printf(“output_format”,print_list) ;formatmayconsiststextOutput_formatplaceholderInside“ ”combinationmayconsistvariableconstantPrint_listexpressionfunction_name
  • 5.
    printf() function(cont..)5Example 1:printf(“UniversitiTun Hussein Onn Malaysia”);Output format -TextOutput:UniversitiTun Hussein Onn Malaysia
  • 6.
    printf() function(cont..)6Example 2:printf(“%lf”, salary); variableFormat output-placeholderOutput:
  • 7.
    printf() function(continued..)7Example 3:printf(“Monthly salary is RM %lf”, salary); variableOutput format -TextplaceholderOutput:Monthly salary is RM Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
  • 8.
    printf() (function continued..)8Example 4:const float porosity = 16.78;printf(“The porosity of sand = %lf ”, porosity); Output format -TextconstantplaceholderOutput:The porosity of sand = 16.780001 Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
  • 9.
    printf() function (cont..)9escapeCharacterAdditional formatting to be performed with the printf() functionfunction
  • 10.
    printf() function (cont..)10PlaceholderfunctionAsymbol beginning with % in a format string that indicates where to display the output valueformatUsing percent symbol (%) followed by characterwhich represent particular data type
  • 11.
    printf() function (cont..)11Example5 (Printing character value ) :char huruf1, huruf2;huruf1 = ‘B’;huruf2 = ‘P’;printf(“%c%c%c”, ‘A’, ‘L’, ‘I’);printf(“\nI live in %c%c”,huruf1,huruf2); Output:ALII live in BP
  • 12.
    printf() function (cont..)12Example6 (Printing integer value) :int number1, number2;printf(“My house number is %d”, 26);number2=10;printf(“\n\tNumber %d and %d”,number1,number2);number2=2;number1= 15;printf(“\nNumber %d and %d”,number2,number1);Output:My house number is 26 Number 906 and 10Number 2 and 15
  • 13.
    printf() function (continued..)13Example6 (Printing floating point number) :double sahih1, sahih2;printf(“The value of pi is %lf”, 3.142);sahih1= 23.46;printf(“\nThe value of sahih1 is %lf”,sahih1);sahih2= 15.78;printf(“\tThe value of sahih2 is %lf’,sahih2);Output:The value of pi ialah 3.142The value of sahih1 is 23.46 The value of sahih2 is 15.78Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
  • 14.
    printf() function (cont..)141.Given a = 2, b = 4. What are the output produced by the following statements?a) printf(“The additionresult of two number >> %d”,a + b);b) printf(“\nThe addition of %d and %d “, a,b);2. Write a valid C programming statement to display the following output (use variables): (Notes: Given the value of variable x = 2.5 and y = 3.1The value of x is 2.5cm and y is 3.1cmThe multiplication result of x and y is 7.75cm
  • 15.
    scanf() functionscanf()To acceptinputfunctionTo read input from keyboardscanf(“input_format”,&variable_list) ;formatscanf(“%d”,&nom) ;examplescanf(“%s %d %lf”,&name,&age,&weight) ;15
  • 16.
    scanf() function(cont..)16consistsexampleinput_formatplaceholder%d @ %c @%lfKnown as ampersand ‘&’ symbolVariable address/location referenceconsistoutput_listvariableconstantexpressionfunctionNot allowed!!!
  • 17.
    scanf() function(cont..)17Example1:scanf(“%lf %d %c”,&float_num, &num,&letter); VariablePlaceholderThe above statement is valid!Warning!!!!!Every variables to be used in the program MUST be declared!!!
  • 18.
    scanf() function(cont..)18Example 2:scanf(“ %d ”, 15); Constant valuePlaceholderThe above statement is INVALID!scanf(“ %lf ”, &price + 0.05); Conversion characterExpressionThe above statement is INVALID!
  • 19.
    scanf() function(cont..)19Example 3(program code):#include <stdio.h>void main(){ int semester;float CPA;printf(“Enter the semester : “);scanf(“%d”, &semester);printf(“\nEnter your CPA : “);scanf(“%lf”,&CPA);printf(“\nYour CPA for semester %d is %f”,semester,CPA);}???Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
  • 20.
    scanf() function(cont..)20Output:Enter the semester : 3Enter your CPA : 3.45Your CPA for semester 3 is 3.45
  • 21.
    scanf() function(continue..)211).Write the scanf() statement to accept the following type of variables: a. char jantina; b. float berat; c. inttempoh;2).State whether the following scanf() statements is VALID or INVALID. Given the following declaration:int a, b; char c; a) scanf(“%d”, &a + b); b) scanf(“%c”, &c); c) scanf(“%d”,&a,&b);
  • 22.
    Other I/O functions22Similarfunctions used in the input and output operations in C programming such as:getchar() and putchar() function
  • 23.
  • 24.
    gets() dan puts()functionOther I/O functions23To read any input from input device as character data typegetchar()functionvariable= getchar();formatchar letter;printf(“Enter one letter>>”);letter= getchar();example usage
  • 25.
    Other I/O functions24Todisplay output in a single character valueputchar()functionputchar(variable);formatprintf(“The input letter is ”);putchar(letter);example usageHazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
  • 26.
    Other I/O functions25Example1: getchar() and putchar()#include <stdio.h>void main(){ char letter;printf(“Enter one letter : “); letter = getchar();printf(“The input letter is\t“);putchar(letter);}Enter one letter :eThe input letter is e
  • 27.
    Other I/O functions26Example2: getchar() and putchar()However, it can also be program like this :#include <stdio.h>void main(){ int number;printf(“Enter one number : “); number = getchar();printf(“The input number is %d\n“,number);putchar(number);}Enter one number :123The input number is 491
  • 28.
    Other I/O functionsToread an input and terminate the program automaticallygetch()functionvariable= getch();formatchar letter;printf(“Enter one letter >>”);letter= getch();examplegetch() have same function like getchar(), but different action27Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
  • 29.
    Other I/O functionsExample3: getch()We can use getch() to hold user screen, and terminate onceany input is read#include <stdio.h>void main(){ int number;printf(“Enter one number : “); number = getchar();printf(“The input number is %d\n“,number);getch();}28Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
  • 30.
    Other I/O functionsTodisplay output , have similar function like putcharputch()functionputch (variable);formatprintf(“The input letter is ”);putch(letter);example29
  • 31.
    Other I/O functions30Toread string input from input devicegets()functiongets(variable);formatchar name[20];printf(“Enter your name >>”);gets(name);exampleHazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
  • 32.
    Other I/O functionsExample4: gets()If we use scanf() function to read string input, it will only takes the first data between two words (if any)#include <stdio.h>void main(){ char name[20];printf(“Enter your name : “);scanf(“%s”,&name);printf(“Your name is %s “,name);getch();}Enter your name: SitiAminahYour name is Siti31Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
  • 33.
    Other I/O functions32Example5: gets()If we use gets() function to read string input, it will takes all thedata including space between two words (if any)#include <stdio.h>void main(){ char name[20];printf(“Enter your name : “); gets(name);printf(“Your name is %s\n“,name);getch();}Enter your name: SitiAminahYour name is SitiAminahHazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
  • 34.
    Other I/O functions33Todisplay string output to output deviceputs()functionsputs (variable);formatprintf(“Your name is ”);puts(name);exampleHazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
  • 35.
    Output formatting34Integer formattingToformat the display of an integervaluefunctions%spacedformatUse the integer formatting in theoutput statementWhere to use?Provide spaces according to formatting
  • 36.
    Put the valueto display starting from right to left sideIf there is – sign in the formatting, values will be printed starting from left to right sideHow it works?
  • 37.
    35Output FormattingAssume _isan empty spaceInteger formattingexampleoutputprintf(“%3d”,123);123outputexampleprintf(“%5d”,123);_ _123outputexampleprintf(“%10d”,123);_ _ _ _ _ _ _123outputexampleprintf(“%-6d”,123);123_ _ _outputexampleprintf(“%-4d%4d”,123,456);123_ _456Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
  • 38.
    Output Formatting36Example 1:Integer formatting#include <stdio.h>void main(){ printf(“%d “,123);printf(“\n%3d “,123);printf(“\n%5d “,123); print(“\n%10d”,123); print(“\n%-6d”,123); print(“\n%-4d%d4d”,123,456);}Output?
  • 39.
    Output formatting37FloatingnumberformattingTo formatthe display of the floatingnumber valuefunctions%spacefformatUse in the output statementWhere to use?Provide spaces according to formatting
  • 40.
    Put the valueto display starting from right to left sideIf there is – sign in the formatting, values will be printed starting from left to right sideHow it works?
  • 41.
    Output formatting38formatFloating numberformatting%spacefoutputexampleprintf(“%10f”,57.78);_57.780000exampleoutputprintf(“%15f”,57.78);_ _ _ _ _ _57.780000exampleoutputprintf(“%-10f”,57.78);57.780000_outputexampleprintf(“%15f”,57.78);57.780000_ _ _ _ _ _
  • 42.
    39Output formattingformatFloating point example%fexampleoutputprintf(“%.3f”,57.78);57.780exampleoutputprintf(“%.2f”,57.78);57.78exampleoutputprintf(“%.1f”,57.78);57.8exampleoutputprintf(“%6.2f”,57.78);57.78_ exampleoutputprintf(“%-6.2f”,57.78);_57.78Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
  • 43.
    40Output formattingformatString format%<space>ccharhuruf = ‘A’;printf(“%c”,huruf);printf(“%2c”,huruf);printf(“%-5c”,huruf);A_AA_ _ _ _exampleoutputHazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
  • 44.
    41Format Output (cont..)formatStringformatting%<spaces> schar ayat[] =“KUITTHO PARIT RAJA”printf(“%s”,ayat);printf(“%4s”,ayat);printf(“%22s”,ayat);printf(“%.7s”,ayat);printf(“%-20.13s”,ayat);outputKUITTHO PARIT RAJAKUITTHO PARIT RAJA_ _ _ _KUITTHO PARIT RAJAKUITTHOKUITTHO PARIT_ _ _ _ _ _ _
  • 45.
    Quick RecapTable 1-Conversion specifiers for scanf
  • 46.
    Printing IntegersAn integeris a whole number, such as 776, 0, or -52 that contains no decimal pointTable 1- Integer conversion specifiers
  • 47.
    Printing Floating-Point NumbersFloating-pointvalue contains a decimal point as in 33.5, 0.0 or -657.983Table 3- floating-point conversion specifies
  • 48.