25 scanf , printf, การรับข้อมูลจากผู้ใช้งาน


  • scanf อ่านค่าจากหน่วยนำเข้าข้อมูล เช่น คีย์บอร์ด
  • จะทำการเก็บค่าตาม format-string ที่ระบุไว้ เก็บไว้ตามที่อยู่ของตัวแปรที่กำหนดไว้
  • ตัวอย่าง
    scanf( “ %d , %f “ , &a , & b);
    เมื่อ a คือตัวแปรชนิด integer และ b คือชนิด float หรือ double
SequenceMeaning
\aBell (alert)
\bBackspace
\fForm feed
\nNew line
\rCarriage return
\tHorizontal tab
\vVertical tab
\\Backslash
\?Question mark
\’Single quotation
\”Double quotation
Specifierความหมายชนิดข้อมูล
%cSingle characterchar
%dSigned decimal integerint, short
%ldSigned long decimal integerlong
%fDecimal floating-point numberfloat, double
%sCharacter stringchar arrays
%uUnsigned decimal integerunsigned int, unsigned short
%luUnsigned long decimal integerunsigned long

#include <stdio.h>
int main(void) // scanf
{
setbuf(stdout,NULL);
int number;
float b[2];
//printf("%d\n",&number);
printf("Enter\"number : ");
scanf("%d %f%f",&number,&b[0],&b[1]);
printf("%d , %f , %f",number,b[0],b[1]);
return 0;
}