printf()
ฟังก์ชัน printf() ใช้สำหรับพิมพ์ข้อความพร้อมกับค่าของตัวแปร
นี่คือไวยากรณ์ของ printf() ในภาษา C
printf(const char *str, ...);
นี่คือตัวอย่าง printf() ในภาษา C
ตัวอย่าง
#include<stdio.h>
int main() {
int a = 24;
printf("Welcome! \n");
printf("The value of a : %d",a);
getchar();
return 0;
} ผลลัพธ์
Welcome! The value of a : 24
sprintf()
ฟังก์ชัน sprintf() เรียกอีกอย่างว่าฟังก์ชันการพิมพ์สตริง มันไม่พิมพ์สตริง มันเก็บสตรีมอักขระบนบัฟเฟอร์ถ่าน จัดรูปแบบและจัดเก็บชุดอักขระและค่าต่างๆ ในอาร์เรย์
นี่คือไวยากรณ์ของ sprintf() ในภาษา C
int sprintf(char *str, const char *string,...);
นี่คือตัวอย่าง sprintf() ในภาษา C
ตัวอย่าง
#include<stdio.h>
int main() {
char buf[20];
int x = 15, y = 25, z;
z = x + y;
sprintf(buf, "Sum of values : %d", z);
printf("%s", buf);
return 0;
} ผลลัพธ์
Sum of values : 40
fprintf ()
ฟังก์ชัน fprintf() เรียกว่าฟังก์ชันการพิมพ์รูปแบบ มันเขียนและจัดรูปแบบเอาต์พุตไปยังสตรีม ใช้สำหรับพิมพ์ข้อความแต่ไม่อยู่ในคอนโซล stdout
นี่คือไวยากรณ์ของ fprintf() ในภาษา C
int fprintf(FILE *fptr, const char *str, ...);
นี่คือตัวอย่าง fprintf() ในภาษา C
ตัวอย่าง
#include<stdio.h>
int main() {
int i, x = 4;
char s[20];
FILE *f = fopen("new.txt", "w");
if (f == NULL) {
printf("Could not open file");
return 0;
}
for (i=0; i<x; i++) {
puts("Enter text");
gets(s);
fprintf(f,"%d.%s\n", i, s);
}
fclose(f);
return 0;
} ผลลัพธ์
Enter text Hello world! Enter text Demo
ผลลัพธ์จะแก้ไขไฟล์ "new.txt" ข้อความต่อไปนี้เป็นข้อความที่แก้ไขของไฟล์
0,Hello world! 1,Demo