C basics: main, printf, and functions · พื้นฐาน C: main, printf, และฟังก์ชัน
C is a compiled language
- C is one of the oldest and most important languages. Linux, databases, and even Python are built with C.
- Unlike Python, C is compiled: the computer first turns your code into a program, then runs it.
- C is strict: every statement ends with a semicolon
;, and every variable needs a type.
C เป็นภาษาที่ compiled
- C เป็นหนึ่งในภาษาที่เก่าแก่และสำคัญที่สุด Linux, databases, และแม้แต่ Python ก็สร้างด้วย C
- ต่างจาก Python, C คือ compiled: คอมพิวเตอร์จะเปลี่ยนโค้ดของคุณเป็นโปรแกรมก่อน แล้วจึงรัน
- C มีความ เข้มงวด:every statement ต้องลงท้ายด้วย semicolon
;, และ every variable ต้องมี type
The C skeleton
- Every C program starts in a function called main. Your code goes inside main.
#include <stdio.h>gives youprintf, which prints text.return 0;at the end ofmainmeans "the program finished without an error".
โครงสร้างหลักของ C
- Every C program เริ่มต้นในฟังก์ชันที่เรียกว่า main โค้ดของคุณจะอยู่ ภายใน main
#include <stdio.h>ให้คุณprintf, ซึ่งพิมพ์ข้อความreturn 0;ที่ปลายmainหมายความว่า "โปรแกรมจบลงโดยไม่เกิดข้อผิดพลาด"
#include <stdio.h>
int main(void) {
printf("C runs from inside main.\n");
int year = 11;
printf("Grade %d\n", year);
return 0;
}
printf and types
printfprints text. The\ninside the text means "start a new line".- To print a value, use a placeholder:
%dfor anint,%ffor adouble,%cfor one character. printf("Grade %d\n", year);puts the value ofyearwhere the%dis.- Every variable needs a type first:
int year = 11;ordouble pi = 3.14;.
printf และ types
printfพิมพ์ข้อความ The\nภายในข้อความหมายถึง "เริ่มบรรทัดใหม่"- เพื่อพิมพ์ค่า ให้ใช้ placeholder:
%dสำหรับint,%fสำหรับdouble,%cสำหรับหนึ่งอักขระ printf("Grade %d\n", year);จะใส่ค่าของyearลงในตำแหน่งที่%d- ตัวแปรทุกตัวต้องกำหนดชนิดก่อน:
int year = 11;หรือdouble pi = 3.14;
Common mistakes
- Every statement ends with
;, andmainreturnsint(return 0;). printfneeds the right format (%d,%f) and\nfor a new line.
ข้อผิดพลาดที่พบบ่อย
- ทุกคำสั่งลงท้ายด้วย
;และmainจะส่งค่ากลับเป็นint(return 0;) printfต้องใช้รูปแบบที่ถูกต้อง (%d,%f) และมี\nเพื่อขึ้นบรรทัดใหม่
Now you try
- Each task pre-fills the code you need. Write inside
main, or complete the function shown. - Some tasks ask for a function only — do not write your own
main; the checker adds one. - Press Run to compile and run, then Check answer. Your code compiles and runs on the server.
ลองดูเลย
- แต่ละงานจะกรอกโค้ดที่คุณต้องการไว้ล่วงหน้า เขียนภายใน
mainหรือเติมให้สมบูรณ์ตามฟังก์ชันที่กำหนด - บางงานขอเพียง ฟังก์ชัน เท่านั้น — อย่าเขียน
mainของคุณเอง; ตัวตรวจสอบจะเพิ่มให้ - กด Run เพื่อคอมไพล์และรัน แล้วกด Check answer โค้ดของคุณจะคอมไพล์และรัน บนเซิร์ฟเวอร์
Compile then run · คอมไพล์แล้วจึงรัน
C source is compiled to a program, which you then run. · ซอร์สโค้ด C จะถูก คอมไพล์ เป็นโปรแกรม ซึ่งคุณจึงนำไปรันต่อ
Inside main, use printf(...) to print exactly Hello, C! on its own line. · ภายใน main, ใช้ printf(...) เพื่อพิมพ์ข้อความ Hello, C! พอดีบนบรรทัดเดียว
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete the function int square(int n) so it returns n * n. The checker calls it with several values. Do not write a main — the checker provides one. · เติมเต็มฟังก์ชัน int square(int n) ให้ส่งกลับค่า n * n. ตัวตรวจสอบจะเรียกใช้มันด้วยค่าต่างๆ หลายค่า. ห้าม เขียน main — ตัวตรวจสอบจัดให้มาอยู่แล้ว
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Inside main, declare int grade = 11;. Then use printf to print two lines: first Hello!, then Grade 11 — print the number with a %d placeholder, not by typing 11. · ภายใน main, ประกาศตัวแปร int grade = 11;. จากนั้นใช้ printf เพื่อพิมพ์สองบรรทัด: บรรทัดแรกคือ Hello!, บรรทัดถัดไปคือ Grade 11 — พิมพ์ตัวเลขโดยใช้ตัววางตำแหน่ง %d แทนการพิมพ์ 11 โดยตรง
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่