SlideShare a Scribd company logo
1 of 8
Mohd Ashari FSKSM 1
BAB 3 OPERASI INPUT OUTPUT
3.0 Pengenalan
- input & output boleh dilakukan samada secara :
a. interaktif (papan kekunci + skrin/pencetak) atau
b. kelompok (fail+skrin/pencetak
3.1 Fungsi printf( )
cth: printf(“Apa khabar pelajar semua?”);
#include<stdio.h>
#include<math.h>
void main( )
{ int jejari; const float pi=3.14; jejari=4;
printf(“Jejari ialah = %d n”,jejari);printf(“Nilai pi ialah %fn ”,pi);
printf(“Luas bulatan ialah %f”,pi*pow(jejari,2));
printf(“nTerimakasih”); }
Mohd Ashari FSKSM 2
BAB 3 OPERASI INPUT OUTPUT
3.1.1 Conversion character
%c, %d, %f, %lf, %g, %o, %s, %u, %x, %%
3.1.2 Constant/Escape character
a, b, f, n, r, t, v, , ’, ”, ooo, xhh, o
3.2 scanf( )
- membaca data daripada papan kekunci.
Cth1 : printf(“Berikan harga ayam : RM”);
scanf(“%f”,&harga);
Cth2 : char nama[30]; printf(“Masukkan nama anda: “);
scanf(“%s”,a); printf(“Nama ialah : %s”,a);
3.3 getc( ) dan putc( )
membaca satu aksara menulis satu aksara
- lihat contoh berikut:
Mohd Ashari FSKSM 3
BAB 3 OPERASI INPUT OUTPUT
int intvar;
intvar = getc(stdin ) - papan kekunci
putc(intvar,stdout/stdprn) - skrin/pencetak
3.4 getchar( ) dan putchar( ) - perlu #include <conio.h>
aks_masuk = getc(stdin); < => aks_masuk = getchar( );
putc(aks_masuk,stdout) <=> putchar(aks_masuk);
3.5 getch( ) dan putch( ) <=> getchar( ) dan putchar( )
3.6 gets( ) dan puts( )
- baca rentetean aksara dan cetak rentetean di skrin
char Nama[30]; printf(:Masukkan Nama :”); gets(Nama);
printf(“Nama ialah :”); puts(Nama);
Mohd Ashari FSKSM 4
3.7 Penyata OUTPUT berformat
printf(“ %1d”,123); > 123
printf(“%5d”,123); > --123
printf(“%-5d,123); > 123--
printf(“%6.2f”,123.4567);> 123.46
printf(“%7.1f”,123.4567);> --123.5
printf(“%7.3f”,123.4567);> 123.457
printf(“%.2f”,123.4567); > 123.46
printf(“%.3f”,123.4567); > 123.457
ch = ‘A’; st[] = “UTM SKUDAI”;
printf(“%c:,ch); > A printf(“%s”,st); > UTM SKUDAI
printf(“%2c”,ch); > -A printf(“%.6s”,st); > UTM SK
BAB 3 OPERASI INPUT OUTPUT
Mohd Ashari FSKSM 5
3.8 Pemproses Fail
Fail : -berjujukan
-rawak
Fail berjujukan: - bina fail, “w”,
- tambah, “a”, dan
- baca, “r”
Cara isytihar: FILE *fptr;
huruf besar pembolehubah penuding
BAB 3 OPERASI INPUT OUTPUT
Mohd Ashari FSKSM 6
Buka Fail
fptr = fopen(“nama.txt”,”w”);
Tutup Fail
fclose(fptr);
Jenis Capaian: mod
“r” - read (baca)
“w” - write (tulis)
“a” - append (tambah)
“r+” - baca + tulis
“w+” - bina, baca + tulis
“a+” - tambah, baca + tulis
BAB 3 OPERASI INPUT OUTPUT
Mohd Ashari FSKSM 7
fptr = fopen(“a:pekerja.txt”,”r”);
disket A
Pengesahan
#include<stdio.h>
void main()
{ FILE *f1;
f1=fopen(“a:pekerja.txt”,”r”);
if (f1==NULL) {puts(“Fail tidak boleh dibuka”); exit(1); }
atau
if ((f1=fopen(“a:pekerja.txt”,”r”))==NULL) {puts(“Fail tidak
boleh dibuka”); exit(1); }
BAB 3 OPERASI INPUT OUTPUT
Mohd Ashari FSKSM 8
Menulis dan Menambah Data
fprintf(), fputc(), fputs(), fputch(), fputchar()..
Cth:
FILE *f2;
if ((f2=fopen(“nama.txt”,”w”))==NULL) {printf(“fail salah”; exit(1);}
fputs(“Mohamad Ashari b Aliasn”,f2); fputs(“Ali b Abdullahn”,f2);
Membaca Data dari Fail
fscanf(), fgets(), fgetc(), fgetch(), fgetchar(),….
fscanf(fptr,“%s”,name);
Memeriksa akhir fail(eof)
cth: while (!eof(fptr)) { aksara = fgetc(fptr); …… }
BAB 3 OPERASI INPUT OUTPUT

More Related Content

Recently uploaded

Group Project (1).pdf pendidikan moral tingkatan 1
Group Project (1).pdf pendidikan moral tingkatan 1Group Project (1).pdf pendidikan moral tingkatan 1
Group Project (1).pdf pendidikan moral tingkatan 1nurulaidahashim1
 
Bahaya vape dan rokok (Seminar Remaja Kebangsaan)-1.pptx
Bahaya vape dan rokok (Seminar Remaja Kebangsaan)-1.pptxBahaya vape dan rokok (Seminar Remaja Kebangsaan)-1.pptx
Bahaya vape dan rokok (Seminar Remaja Kebangsaan)-1.pptxkedekut1
 
Mesyuarat kokurikulum sjk t bukit darah.
Mesyuarat kokurikulum sjk t bukit darah.Mesyuarat kokurikulum sjk t bukit darah.
Mesyuarat kokurikulum sjk t bukit darah.gs69955
 
Slaid Taklimat Program NILAM 2021 malaysia
Slaid Taklimat Program NILAM 2021 malaysiaSlaid Taklimat Program NILAM 2021 malaysia
Slaid Taklimat Program NILAM 2021 malaysiaLUKTZECHINMoe
 
Bab 1 : Tujuan Perniagaan dan pemilikan perniagaan
Bab 1 : Tujuan Perniagaan dan pemilikan perniagaanBab 1 : Tujuan Perniagaan dan pemilikan perniagaan
Bab 1 : Tujuan Perniagaan dan pemilikan perniagaanErnaKasran1
 
Bab 2 Sebatian Karbon new copy kimi.pptx
Bab 2 Sebatian Karbon new copy kimi.pptxBab 2 Sebatian Karbon new copy kimi.pptx
Bab 2 Sebatian Karbon new copy kimi.pptxFarid Salleh
 

Recently uploaded (6)

Group Project (1).pdf pendidikan moral tingkatan 1
Group Project (1).pdf pendidikan moral tingkatan 1Group Project (1).pdf pendidikan moral tingkatan 1
Group Project (1).pdf pendidikan moral tingkatan 1
 
Bahaya vape dan rokok (Seminar Remaja Kebangsaan)-1.pptx
Bahaya vape dan rokok (Seminar Remaja Kebangsaan)-1.pptxBahaya vape dan rokok (Seminar Remaja Kebangsaan)-1.pptx
Bahaya vape dan rokok (Seminar Remaja Kebangsaan)-1.pptx
 
Mesyuarat kokurikulum sjk t bukit darah.
Mesyuarat kokurikulum sjk t bukit darah.Mesyuarat kokurikulum sjk t bukit darah.
Mesyuarat kokurikulum sjk t bukit darah.
 
Slaid Taklimat Program NILAM 2021 malaysia
Slaid Taklimat Program NILAM 2021 malaysiaSlaid Taklimat Program NILAM 2021 malaysia
Slaid Taklimat Program NILAM 2021 malaysia
 
Bab 1 : Tujuan Perniagaan dan pemilikan perniagaan
Bab 1 : Tujuan Perniagaan dan pemilikan perniagaanBab 1 : Tujuan Perniagaan dan pemilikan perniagaan
Bab 1 : Tujuan Perniagaan dan pemilikan perniagaan
 
Bab 2 Sebatian Karbon new copy kimi.pptx
Bab 2 Sebatian Karbon new copy kimi.pptxBab 2 Sebatian Karbon new copy kimi.pptx
Bab 2 Sebatian Karbon new copy kimi.pptx
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

bab3c.ppt

  • 1. Mohd Ashari FSKSM 1 BAB 3 OPERASI INPUT OUTPUT 3.0 Pengenalan - input & output boleh dilakukan samada secara : a. interaktif (papan kekunci + skrin/pencetak) atau b. kelompok (fail+skrin/pencetak 3.1 Fungsi printf( ) cth: printf(“Apa khabar pelajar semua?”); #include<stdio.h> #include<math.h> void main( ) { int jejari; const float pi=3.14; jejari=4; printf(“Jejari ialah = %d n”,jejari);printf(“Nilai pi ialah %fn ”,pi); printf(“Luas bulatan ialah %f”,pi*pow(jejari,2)); printf(“nTerimakasih”); }
  • 2. Mohd Ashari FSKSM 2 BAB 3 OPERASI INPUT OUTPUT 3.1.1 Conversion character %c, %d, %f, %lf, %g, %o, %s, %u, %x, %% 3.1.2 Constant/Escape character a, b, f, n, r, t, v, , ’, ”, ooo, xhh, o 3.2 scanf( ) - membaca data daripada papan kekunci. Cth1 : printf(“Berikan harga ayam : RM”); scanf(“%f”,&harga); Cth2 : char nama[30]; printf(“Masukkan nama anda: “); scanf(“%s”,a); printf(“Nama ialah : %s”,a); 3.3 getc( ) dan putc( ) membaca satu aksara menulis satu aksara - lihat contoh berikut:
  • 3. Mohd Ashari FSKSM 3 BAB 3 OPERASI INPUT OUTPUT int intvar; intvar = getc(stdin ) - papan kekunci putc(intvar,stdout/stdprn) - skrin/pencetak 3.4 getchar( ) dan putchar( ) - perlu #include <conio.h> aks_masuk = getc(stdin); < => aks_masuk = getchar( ); putc(aks_masuk,stdout) <=> putchar(aks_masuk); 3.5 getch( ) dan putch( ) <=> getchar( ) dan putchar( ) 3.6 gets( ) dan puts( ) - baca rentetean aksara dan cetak rentetean di skrin char Nama[30]; printf(:Masukkan Nama :”); gets(Nama); printf(“Nama ialah :”); puts(Nama);
  • 4. Mohd Ashari FSKSM 4 3.7 Penyata OUTPUT berformat printf(“ %1d”,123); > 123 printf(“%5d”,123); > --123 printf(“%-5d,123); > 123-- printf(“%6.2f”,123.4567);> 123.46 printf(“%7.1f”,123.4567);> --123.5 printf(“%7.3f”,123.4567);> 123.457 printf(“%.2f”,123.4567); > 123.46 printf(“%.3f”,123.4567); > 123.457 ch = ‘A’; st[] = “UTM SKUDAI”; printf(“%c:,ch); > A printf(“%s”,st); > UTM SKUDAI printf(“%2c”,ch); > -A printf(“%.6s”,st); > UTM SK BAB 3 OPERASI INPUT OUTPUT
  • 5. Mohd Ashari FSKSM 5 3.8 Pemproses Fail Fail : -berjujukan -rawak Fail berjujukan: - bina fail, “w”, - tambah, “a”, dan - baca, “r” Cara isytihar: FILE *fptr; huruf besar pembolehubah penuding BAB 3 OPERASI INPUT OUTPUT
  • 6. Mohd Ashari FSKSM 6 Buka Fail fptr = fopen(“nama.txt”,”w”); Tutup Fail fclose(fptr); Jenis Capaian: mod “r” - read (baca) “w” - write (tulis) “a” - append (tambah) “r+” - baca + tulis “w+” - bina, baca + tulis “a+” - tambah, baca + tulis BAB 3 OPERASI INPUT OUTPUT
  • 7. Mohd Ashari FSKSM 7 fptr = fopen(“a:pekerja.txt”,”r”); disket A Pengesahan #include<stdio.h> void main() { FILE *f1; f1=fopen(“a:pekerja.txt”,”r”); if (f1==NULL) {puts(“Fail tidak boleh dibuka”); exit(1); } atau if ((f1=fopen(“a:pekerja.txt”,”r”))==NULL) {puts(“Fail tidak boleh dibuka”); exit(1); } BAB 3 OPERASI INPUT OUTPUT
  • 8. Mohd Ashari FSKSM 8 Menulis dan Menambah Data fprintf(), fputc(), fputs(), fputch(), fputchar().. Cth: FILE *f2; if ((f2=fopen(“nama.txt”,”w”))==NULL) {printf(“fail salah”; exit(1);} fputs(“Mohamad Ashari b Aliasn”,f2); fputs(“Ali b Abdullahn”,f2); Membaca Data dari Fail fscanf(), fgets(), fgetc(), fgetch(), fgetchar(),…. fscanf(fptr,“%s”,name); Memeriksa akhir fail(eof) cth: while (!eof(fptr)) { aksara = fgetc(fptr); …… } BAB 3 OPERASI INPUT OUTPUT