ฟังก์ชันอินพุตและเอาต์พุตที่ไม่ได้จัดรูปแบบจะอ่านอินพุตเดียวที่ส่งโดยผู้ใช้ และอนุญาตให้แสดงค่าเป็นเอาต์พุตที่คอนโซล
ฟังก์ชันอินพุตที่ไม่ได้ฟอร์แมต
ฟังก์ชันอินพุตที่ไม่ได้จัดรูปแบบในภาษาการเขียนโปรแกรม C มีคำอธิบายด้านล่าง −
getchar()
มันอ่านตัวอักษรจากแป้นพิมพ์
ไวยากรณ์ของฟังก์ชัน getchar() มีดังนี้ -
Variablename=getchar();
ตัวอย่างเช่น
Char a; a = getchar();
ตัวอย่างโปรแกรม
ต่อไปนี้เป็นโปรแกรม C สำหรับฟังก์ชัน getchar() -
#include<stdio.h>
int main(){
char ch;
FILE *fp;
fp=fopen("file.txt","w"); //open the file in write mode
printf("enter the text then press cntrl Z:\n");
while((ch = getchar())!=EOF){
putc(ch,fp);
}
fclose(fp);
fp=fopen("file.txt","r");
printf("text on the file:\n");
while ((ch=getc(fp))!=EOF){
if(fp){
char word[100];
while(fscanf(fp,"%s",word)!=EOF) // read words from file{
printf("%s\n", word); // print each word on separate lines.
}
fclose(fp); // close file.
}else{
printf("file doesnot exist");
// then tells the user that the file does not exist.
}
}
return 0;
} ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
enter the text then press cntrl Z: This is an example program on getchar() ^Z text on the file: This is an example program on getchar()
ได้()
มันอ่านสตริงจากแป้นพิมพ์
ไวยากรณ์สำหรับฟังก์ชัน gets() มีดังนี้ -
gets(variablename);
ตัวอย่าง
#include<stdio.h>
#include<string.h>
main(){
char str[10];
printf("Enter your name: \n");
gets(str);
printf("Hello %s welcome to Tutorialspoint", str);
} ผลลัพธ์
Enter your name: Madhu Hello Madhu welcome to Tutorialspoint
ฟังก์ชันเอาต์พุตที่ไม่ได้จัดรูปแบบ
ฟังก์ชันเอาต์พุตที่ไม่ได้จัดรูปแบบในภาษาการเขียนโปรแกรม C มีดังนี้ -
putchar()
จะแสดงอักขระบนจอภาพ
ไวยากรณ์สำหรับฟังก์ชัน putchar() มีดังนี้ -
Putchar(variablename);
ตัวอย่างเช่น
Putchar(‘a’);
ใส่()
จะแสดงสตริงบนจอภาพ
ไวยากรณ์สำหรับฟังก์ชัน puts() มีดังนี้ −
puts(variablename);
ตัวอย่างเช่น
puts("tutorial"); โปรแกรมตัวอย่าง
ต่อไปนี้เป็นโปรแกรม C สำหรับฟังก์ชัน putc และ getc -
#include <stdio.h>
int main(){
char ch;
FILE *fp;
fp=fopen("std1.txt","w");
printf("enter the text.press cntrl Z:\n");
while((ch = getchar())!=EOF){
putc(ch,fp);
}
fclose(fp);
fp=fopen("std1.txt","r");
printf("text on the file:\n");
while ((ch=getc(fp))!=EOF){
putchar(ch);
}
fclose(fp);
return 0;
} ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
enter the text.press cntrl Z: This is an example program on putchar() ^Z text on the file: This is an example program on putchar()