SlideShare a Scribd company logo
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

More Related Content

PDF
Deep C
DOCX
Important C program of Balagurusamy Book
PDF
C programming Workshop
PDF
Programming with c language practical manual
PDF
CP Handout#6
DOCX
C Programming
PPTX
What is c
Deep C
Important C program of Balagurusamy Book
C programming Workshop
Programming with c language practical manual
CP Handout#6
C Programming
What is c

What's hot (20)

DOCX
Lab. Programs in C
PPT
Input And Output
PPTX
C Programming Unit-1
PPTX
C Programming Unit-2
PPTX
Programming Fundamentals lecture 5
PPTX
Introduction to Basic C programming 02
DOCX
C Programming
DOCX
Programming in c
DOCX
UNIT-II CP DOC.docx
PDF
CP Handout#2
PPTX
Moving Average Filter in C
PPT
Mesics lecture 5 input – output in ‘c’
PDF
Unit 2
PDF
CP Handout#4
PPTX
Input Output Management In C Programming
PDF
CP Handout#8
PPT
Lecture 8- Data Input and Output
PPTX
C programming(Part 1)
PDF
VTU PCD Model Question Paper - Programming in C
PDF
[ITP - Lecture 12] Functions in C/C++
Lab. Programs in C
Input And Output
C Programming Unit-1
C Programming Unit-2
Programming Fundamentals lecture 5
Introduction to Basic C programming 02
C Programming
Programming in c
UNIT-II CP DOC.docx
CP Handout#2
Moving Average Filter in C
Mesics lecture 5 input – output in ‘c’
Unit 2
CP Handout#4
Input Output Management In C Programming
CP Handout#8
Lecture 8- Data Input and Output
C programming(Part 1)
VTU PCD Model Question Paper - Programming in C
[ITP - Lecture 12] Functions in C/C++
Ad

Viewers also liked (20)

PPT
ΠΕΡΙΒΑΛΛΟΝΤΙΚΗ 9ου ΓΥΜΝΑΣΙΟΥ ΝΙΚΑΙΑΣ
PPTX
7101 project
PPTX
Facebook by cenkyase
PPS
Philosophy for old_age
PPTX
Ways of Looking 1
KEY
第4回淑女会 ~facebook講座~
PDF
Alma media cmd 2013 kai telanne
PDF
تفعيل المشاركات
PDF
Ance_ProgrammaJESSICA_24052011
PPT
ΠΑΡΟΥΣΙΑΣΗ ECOMOBILITY 2014
PPT
Dia de la Madre 2013
PDF
29 fotosintese
PPS
A. nucleicos 2010-2011 new
PPS
Meiose 2010 2011 new
PDF
الأسبوع التاسع
DOCX
Global economy in reverse gear
PDF
14 o codigo_xenetico
PDF
24 celula citoplasma
PPT
หมั่นโถวเสริมโปรตีนจากใบมะรุม
PPT
ΠΕΡΙΒΑΛΛΟΝΤΙΚΗ 9ου ΓΥΜΝΑΣΙΟΥ ΝΙΚΑΙΑΣ
7101 project
Facebook by cenkyase
Philosophy for old_age
Ways of Looking 1
第4回淑女会 ~facebook講座~
Alma media cmd 2013 kai telanne
تفعيل المشاركات
Ance_ProgrammaJESSICA_24052011
ΠΑΡΟΥΣΙΑΣΗ ECOMOBILITY 2014
Dia de la Madre 2013
29 fotosintese
A. nucleicos 2010-2011 new
Meiose 2010 2011 new
الأسبوع التاسع
Global economy in reverse gear
14 o codigo_xenetico
24 celula citoplasma
หมั่นโถวเสริมโปรตีนจากใบมะรุม
Ad

Similar to Chap 2 input output dti2143 (20)

PPTX
CHAPTER 4
PPT
CPU INPUT OUTPUT
PPTX
Data Input and Output
PPTX
Input and Output In C Language
PPTX
20220823094225_PPT02-Formatted Input and Output.pptx
PPTX
COM1407: Input/ Output Functions
PPTX
Input Output function in c programing language.pptx
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
PPT
Fundamental of C Programming Language and Basic Input/Output Function
PPTX
C language
PPTX
C programming(part 3)
PDF
Introduction to Input/Output Functions in C
PPTX
c_pro_introduction.pptx
PDF
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
PPTX
1_Introduction of programming language C.pptx
PPTX
Managing input and output operations in c
PPTX
Managing input and output operation in c
PPTX
PPTX
presentation_c_basics_1589366177_381682.pptx
PPTX
I o functions
CHAPTER 4
CPU INPUT OUTPUT
Data Input and Output
Input and Output In C Language
20220823094225_PPT02-Formatted Input and Output.pptx
COM1407: Input/ Output Functions
Input Output function in c programing language.pptx
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
Fundamental of C Programming Language and Basic Input/Output Function
C language
C programming(part 3)
Introduction to Input/Output Functions in C
c_pro_introduction.pptx
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
1_Introduction of programming language C.pptx
Managing input and output operations in c
Managing input and output operation in c
presentation_c_basics_1589366177_381682.pptx
I o functions

More from alish sha (20)

PPTX
T22016 – how to answer with ubs 9
PPTX
July 2014 theory exam (theory)
PPTX
Accounting basic equation
PPTX
It 302 computerized accounting (week 2) - sharifah
PPTX
It 302 computerized accounting (week 1) - sharifah
PPTX
What are the causes of conflicts (Bahasa Malaysia)
DOC
Lab 9 sem ii_12_13
DOCX
Lab 10 sem ii_12_13
DOC
Lab 6
DOC
Lab 5 2012/2012
DOCX
Purpose elaborate
DOC
Lab sheet 1
DOC
Test 1 alish schema 1
DOC
Lab 6 sem ii_11_12
PDF
Test 1 skema q&a
PDF
Test 1 skema q&a
DOC
Final project
DOCX
Final project
XLS
Attn list test
PDF
Carry markdam31303
T22016 – how to answer with ubs 9
July 2014 theory exam (theory)
Accounting basic equation
It 302 computerized accounting (week 2) - sharifah
It 302 computerized accounting (week 1) - sharifah
What are the causes of conflicts (Bahasa Malaysia)
Lab 9 sem ii_12_13
Lab 10 sem ii_12_13
Lab 6
Lab 5 2012/2012
Purpose elaborate
Lab sheet 1
Test 1 alish schema 1
Lab 6 sem ii_11_12
Test 1 skema q&a
Test 1 skema q&a
Final project
Final project
Attn list test
Carry markdam31303

Chap 2 input output dti2143

  • 1. Chapter 2 :INPUT AND OUTPUTPROCESSDTI 2143: Computer Programming
  • 2. 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.
  • 3. 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) ;
  • 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..)9escape CharacterAdditional formatting to be performed with the printf() functionfunction
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 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 accept inputfunctionTo 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 functions22Similar functions used in the input and output operations in C programming such as:getchar() and putchar() function
  • 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 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
  • 26. 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
  • 27. 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
  • 28. 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
  • 29. 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
  • 30. Other I/O functionsTo display output , have similar function like putcharputch()functionputch (variable);formatprintf(“The input letter is ”);putch(letter);example29
  • 31. 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
  • 32. 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
  • 33. 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
  • 34. 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
  • 35. Output formatting34Integer formattingTo format the display of an integervaluefunctions%spacedformatUse the integer formatting in theoutput statementWhere to use?Provide spaces according to formatting
  • 36. 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?
  • 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 format the display of the floatingnumber valuefunctions%spacefformatUse in the output statementWhere to use?Provide spaces according to formatting
  • 40. 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?
  • 41. 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_ _ _ _ _ _
  • 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>cchar huruf = ‘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..)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_ _ _ _ _ _ _
  • 45. Quick RecapTable 1- Conversion specifiers for scanf
  • 46. Printing IntegersAn integer is a whole number, such as 776, 0, or -52 that contains no decimal pointTable 1- Integer conversion specifiers
  • 47. Printing Floating-Point NumbersFloating-point value contains a decimal point as in 33.5, 0.0 or -657.983Table 3- floating-point conversion specifies