วิชาการเขียนโปรแกรมด้วยภาษาซี (c)

การแสดงผลค่าตัวแปร ภาษาซี c programming

บทความนี้จะเป็นการนำค่าจากตัวแปรชนิดข้อมูลประเภทต่าง ๆ มาแสดงผล โดยการใช้คำสั่ง printf

ข้อควรรู้เรื่องรหัสรูปแบบ

รหัสรูปแบบใช้สำหรับควบคุมการแสดงผลตัวแปร ดังต่อไปนี้


%d ใช้กับข้อมูลที่เป็นจำนวนเต็ม เวลาเรียกจะใช้จะอยู่ภายใน ” “

การใช้งานเช่น printf (“%d”)


%f ใช้กับข้อมูลที่เป็นจำนวนเลขทศนิยม เวลาเรียกจะใช้จะอยู่ภายใน ” “

การใช้งานเช่น printf (“%f”)


%c ใช้กับข้อมูลที่เป็นอักขระ เวลาเรียกจะใช้จะอยู่ภายใน ” “

การใช้งานเช่น printf (“%c”)


%s ใช้กับข้อมูลที่เป็นข้อความ เวลาเรียกจะใช้จะอยู่ภายใน ” “

การใช้งานเช่น printf (“%s”)


ตัวอย่างการแสดงผลค่าตัวแปร ภาษาซี (c)

#include <stdio.h> 
main()
{
  int a = 2; 
  float b = 2.5;
  char c = 'A';
  char aa[10]="wuttichai";
  printf ("%d\n",a); 
  printf ("%f\n",b);
  printf ("%c\n",c);
  printf ("%s",aa);
}

ผลลัพธ์ที่ได้

2
2.500000
A
wuttichai

slot gacor hari ini slot qris slot gacor maxwin slot online slot gacor gampang menang slot gacor hari ini slot online slot slot slot guci777 guci777 fafatogel fafatogel fafatogel fafatogel agen138 slot agen138 slot data hk bola slot bolaslot99 bola slot agen138 link alternatif koin138 link alternatif agen138 slot ag138 slot slot gacor slot thailand