Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

วิธีพิมพ์เครื่องหมาย % ด้วย printf()

โดยทั่วไป printf() ใช้พิมพ์ข้อความพร้อมค่าตัวแปร หากต้องการพิมพ์เครื่องหมาย % เป็นข้อความ ต้องใช้ %% (คู่น้อย) % เดียวจะไม่พิมพ์อะไรและไม่แสดงข้อผิดพลาด

ตัวอย่าง

#include <stdio.h>
int main() {
    printf("%");
    printf("%%");
    getchar();
    return 0;
}

ผลลัพธ์

%

วิธีอื่นๆ

#include <stdio.h>
#include <string.h>
int main() {
    printf("welcome%\n");
    printf("%%\n");
    printf("%c\n", '%');
    printf("%s\n", "%");
    char a[5];
    strcpy(a, "%%");
    printf("This is a's value: %s\n", a);
    return 0;
}

ผลลัพธ์

welcome%
%
%
%
This is a's value: %%