Variables, types, and arithmetic · ตัวแปร, ประเภท, และการคำนวณทางคณิตศาสตร์
Every value has a type
- In C, every variable has a type that says what kind of value it holds.
- The three you use most:
int(a whole number),double(a number with a decimal point), andchar(a single character). - You must give the type when you create the variable:
int n = 5;ordouble pi = 3.14;.
ค่าทุกค่ามีชนิด
- ใน C ตัวแปรแต่ละตัวมี type ที่บอกประเภทของค่าที่มันเก็บไว้
- สามอย่างที่คุณใช้บ่อยที่สุด:
int(จำนวนเต็ม),double(ทศนิยม), และchar(ตัวอักษรเดียว) - คุณต้องระบุ type เมื่อสร้างตัวแปร:
int n = 5;หรือdouble pi = 3.14;
Doing arithmetic
- C does math with
+,-,*, and/, just like a calculator. *and/happen before+and-. Use brackets( )to change the order.- The
%operator gives the remainder of a division:17 % 5is2.
การคำนวณทางคณิตศาสตร์
- C ทำการคำนวณด้วย
+,-,*, และ/เหมือนกับเครื่องคิดเลข *และ/ทำงาน ก่อน+และ-ใช้วงเล็บ( )เพื่อเปลี่ยนลำดับ- ตัวดำเนินการ
%ให้ ผลหารเหลือ ของการหาร:17 % 5คือ2
Integers divide differently
- When you divide two
ints, C throws away the fraction:7 / 2is3, not3.5. - To keep the fraction, at least one side must be a
double.7 / 2.0is3.5. - You can cast an
intto adoublewith(double):(double)sum / count.
จำนวนเต็มหารกันต่างออกไป
- เมื่อหารจำนวน
intสองตัว C จะตัดทอนเศษออก:7 / 2คือ3ไม่ใช่3.5 - หากต้องการรักษาเศษ อย่างน้อยหนึ่งฝั่งต้องเป็น
double7 / 2.0คือ3.5 - คุณสามารถ cast
intเป็นdoubleได้โดยใช้(double):(double)sum / count
Reading input
scanfreads typed input into a variable.scanf("%d", &n);reads oneintinton.- Note the
&beforen:scanfneeds the address of the variable so it can fill it in. - In this playground, Run first asks you for the input, one value per line; Check answer feeds the example input for you.
การรับข้อมูลเข้า
scanfอ่านข้อมูลที่พิมพ์เข้ามาลงในตัวแปรscanf("%d", &n);อ่านintเดียวเข้าไปในn- ระวัง
&ก่อนn:scanfต้องการ ที่อยู่ ของตัวแปรเพื่อให้สามารถใส่ค่าลงไปได้ - ใน playground นี้ Run จะขอข้อมูลเข้าก่อน โดยใส่ค่าละบรรทัด; Check answer จะป้อนตัวอย่างข้อมูลให้คุณอัตโนมัติ
#include <stdio.h>
int main(void) {
int a = 7, b = 2;
printf("%d\n", a / b); // 3 (integer division)
printf("%.1f\n", a / 2.0); // 3.5 (one side is a double)
printf("%d\n", a % b); // 1 (remainder)
return 0;
}
Common mistakes
- Declare a variable with its type:
int n;. - Integer division truncates:
7 / 2is3, not3.5.
ข้อผิดพลาดที่พบบ่อย
- ประกาศตัวแปรพร้อมระบุชนิด:
int n; - การหารจำนวนเต็มจะตัดทอนเศษออก:
7 / 2ได้ผลลัพธ์เป็น3ไม่ใช่3.5
Now you try
- Use
/and%for whole-number math, and adouble(or a cast) when you need a fraction. - For the function tasks, do not write a
main— the checker provides one. - Press Run to compile and run, then Check answer.
ลองดูเลย
- ใช้
/และ%สำหรับเลขคณิตจำนวนเต็ม และใช้double(หรือการแปลงชนิด) เมื่อต้องการค่าทศนิยม - สำหรับโจทย์เกี่ยวกับฟังก์ชัน อย่าเขียน
main— ตัวตรวจสอบจะจัดเตรียมให้เอง - กด Run เพื่อ compile และ run, แล้วกด Check answer
Variables hold a value · ตัวแปรเก็บค่าหนึ่งไว้
A variable is a named box of a fixed type; assigning replaces it. · ตัวแปรคือ กล่องที่มีชื่อ ที่มีประเภทคงที่; การกำหนดค่าใหม่จะแทนที่ค่าเดิม
Read one integer n with scanf. A box holds 6 items. Print two lines exactly: Full boxes: then the value of n / 6, and Left over: then the value of n % 6. · อ่านจำนวนเต็ม n ด้วย scanf. กล่องหนึ่งเก็บข้อมูลได้ 6 รายการ. พิมพ์สองบรรทัดพอดี: บรรทัดแรกคือ Full boxes: ตามด้วยค่าของ n / 6, และบรรทัดที่สองคือ Left over: ตามด้วยค่าของ n % 6.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete double average3(int a, int b, int c) so it returns the average of the three as a double. Watch out for integer division — divide by 3.0, not 3. Do not write a main. · เติม double average3(int a, int b, int c) เพื่อให้ฟังก์ชันนี้คืนค่าเฉลี่ยของตัวเลขทั้งสามในรูปแบบ double ระวังเรื่องการหารแบบจำนวนเต็ม — ให้หารด้วย 3.0 ไม่ใช่ 3 อย่าเขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write double hyp(double a, double b) that returns the hypotenuse length: the square root of a*a + b*b. Use sqrt from <math.h>. Do not write a main. · เขียน double hyp(double a, double b) ที่ส่งกลับความยาวด้านตรงข้ามมุมฉาก: รากที่สองของ a*a + b*b. ใช้ sqrt จาก <math.h>. ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่