บทที่ 1: แนะนำภาษา C และโปรแกรมแรก
บทที่ 1: แนะนำภาษา C และโปรแกรมแรก
ภาษา C เป็นภาษาโปรแกรมระดับกลางที่พัฒนาโดย Dennis Ritchie ในปี 1972 ที่ Bell Labs ใช้สำหรับระบบปฏิบัติการ UNIX มันมีประสิทธิภาพสูง ควบคุมฮาร์ดแวร์ได้ดี และเป็นพื้นฐานของภาษาอื่นๆ เช่น C++, Java
การติดตั้ง:
สำหรับ Windows: ติดตั้ง MinGW หรือ Visual Studio Code กับ GCC
สำหรับ Linux/Mac: GCC มีติดตั้งมาแล้ว (ตรวจสอบด้วย gcc --version)
โปรแกรมแรก: Hello World
โปรแกรมนี้แสดงข้อความ "Hello, World!" บนหน้าจอ เพื่อทดสอบการทำงานพื้นฐาน
#include <stdio.h> // รวมไลบรารีสำหรับ printf
int main() { // ฟังก์ชันหลักที่โปรแกรมเริ่มทำงาน
printf("Hello, World!\n"); // พิมพ์ข้อความและขึ้นบรรทัดใหม่
return 0; // ส่งคืนค่า 0 แสดงว่าสำเร็จ
}
คำอธิบาย:
main() เป็นจุดเริ่มต้นของโปรแกรม
printf() ใช้แสดงผล output
รันแล้วจะเห็น "Hello, World!" บนคอนโซล
บทที่ 2: ตัวแปร ประเภทข้อมูล และค่าคงที่
ตัวแปรคือพื้นที่หน่วยความจำสำหรับเก็บข้อมูล ประเภทข้อมูลกำหนดขนาดและชนิด เช่น int สำหรับจำนวนเต็ม
ประเภทข้อมูลพื้นฐาน:
int: จำนวนเต็ม (ขนาด 4 bytes, เช่น 5)
float: ทศนิยม (ขนาด 4 bytes, เช่น 3.14)
double: ทศนิยมความละเอียดสูง (ขนาด 8 bytes)
char: ตัวอักษร (ขนาด 1 byte, เช่น 'A')
void: ไม่มีค่า (ใช้ในฟังก์ชัน)
ค่าคงที่: ใช้ const เพื่อป้องกันการเปลี่ยนค่า
ตัวอย่างโค้ด: การประกาศและใช้งานตัวแปร
#include <stdio.h>
int main() {
int age = 25; // ประกาศตัวแปร int และกำหนดค่า
float height = 1.75; // ทศนิยม
char initial = 'A'; // ตัวอักษร
const double PI = 3.14159; // ค่าคงที่
printf("Age: %d\n", age); // %d สำหรับ int
printf("Height: %.2f meters\n", height); // %.2f สำหรับ float แสดง 2 ตำแหน่งทศนิยม
printf("Initial: %c\n", initial); // %c สำหรับ char
printf("PI: %.5f\n", PI); // %.5f สำหรับ double
// PI = 3.14; // Error: ไม่สามารถเปลี่ยนค่าคงที่
return 0;
}
คำอธิบาย:
โปรแกรมแสดงค่าตัวแปรต่างๆ โดยใช้ format specifiers ใน printf() เช่น %d สำหรับ int
บทที่ 3: ตัวดำเนินการ (Operators)
ตัวดำเนินการใช้คำนวณหรือเปรียบเทียบข้อมูล
คณิตศาสตร์: +, -, *, /, % (modulo)
เปรียบเทียบ: ==, !=, >, <, >=, <=
ลอจิคัล: && (AND), || (OR), ! (NOT)
การกำหนดค่า: =, +=, -=
#include <stdio.h>
int main() {
int a = 10, b = 3;
// คณิตศาสตร์
printf("a + b = %d\n", a + b); // 13
printf("a %% b = %d\n", a % b); // 1 (เศษ)
// เปรียบเทียบ
if (a > b) {
printf("a is greater than b\n");
}
// ลอจิคัล
if (a > 0 && b > 0) {
printf("Both positive\n");
}
// การกำหนดค่า
a += 5; // a = a + 5 → 15
printf("New a: %d\n", a);
return 0;
}
คำอธิบาย:
โปรแกรมคำนวณและแสดงผลตัวดำเนินการต่างๆ โดยใช้ if เพื่อตรวจสอบเงื่อนไข
บทที่ 4: โครงสร้างควบคุม (Control Structures)
ใช้ควบคุมการไหลของโปรแกรม เช่น การตัดสินใจและการวนซ้ำ
4.1: การตัดสินใจ (if, else, switch)
4.2: การวนซ้ำ (Loops)
for: รู้จำนวนรอบแน่นอน
while: วนจนกว่าเงื่อนไขเท็จ
do-while: วนอย่างน้อย 1 รอบ
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("Positive\n");
} else if (num < 0) {
printf("Negative\n");
} else {
printf("Zero\n");
}
// switch
char grade = 'B';
switch (grade) {
case 'A': printf("Excellent\n"); break;
case 'B': printf("Good\n"); break;
default: printf("Invalid\n");
}
return 0;
}
คำอธิบาย:
if-else ตรวจสอบเงื่อนไข, switch ใช้สำหรับตัวเลือกหลายค่า (ต้องมี break เพื่อหยุด)
#include <stdio.h>
int main() {
// for loop
for (int i = 1; i <= 5; i++) {
printf("%d ", i); // 1 2 3 4 5
}
printf("\n");
// while
int j = 5;
while (j > 0) {
printf("%d ", j); // 5 4 3 2 1
j--;
}
printf("\n");
// do-while
int k = 0;
do {
printf("Do-while runs at least once\n");
} while (k > 0); // วน 1 รอบแม้เงื่อนไขเท็จ
return 0;
}
คำอธิบาย:
Loops ใช้ทำซ้ำคำสั่ง เช่น พิมพ์ตัวเลข
บทที่ 5: ฟังก์ชัน (Functions)
ฟังก์ชันคือบล็อกโค้ดที่เรียกใช้ซ้ำได้ มีพารามิเตอร์และคืนค่า
#include <stdio.h>
// ประกาศฟังก์ชัน (prototype)
int add(int x, int y);
int main() {
int result = add(5, 3); // เรียกฟังก์ชัน
printf("Sum: %d\n", result); // 8
return 0;
}
// กำหนดฟังก์ชัน
int add(int x, int y) {
return x + y; // คืนค่า
}
คำอธิบาย:
ฟังก์ชัน add รับ 2 ค่า คำนวณผลรวม แล้วคืนค่าให้ main
บทที่ 6: อาร์เรย์ (Arrays)
อาร์เรย์คือกลุ่มข้อมูลเดียวกัน เรียงลำดับ เริ่ม index ที่ 0
#include <stdio.h>
int main() {
// 1D array
int arr[5] = {1, 2, 3, 4, 5}; // ขนาด 5
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]); // 1 2 3 4 5
}
printf("\n");
// 2D array (matrix)
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 3; col++) {
printf("%d ", matrix[row][col]); // 1 2 3 4 5 6
}
}
printf("\n");
return 0;
}
คำอธิบาย:
ใช้ loop เพื่อเข้าถึงแต่ละ element ในอาร์เรย์
บทที่ 7: สตริง (Strings)
สตริงคืออาร์เรย์ของ char จบด้วย '\0' (null terminator) ใช้ไลบรารี <string.h> สำหรับฟังก์ชันอย่าง strlen, strcpy
#include <stdio.h>
#include <string.h> // สำหรับฟังก์ชันสตริง
int main() {
char str[20] = "Hello"; // สตริง
printf("String: %s\n", str); // %s สำหรับสตริง
// ความยาว
printf("Length: %lu\n", strlen(str)); // 5
// คัดลอก
char copy[20];
strcpy(copy, str);
printf("Copy: %s\n", copy);
// เชื่อมต่อ
strcat(str, " World");
printf("Concat: %s\n", str); // Hello World
return 0;
}
คำอธิบาย:
ฟังก์ชันจาก <string.h> ช่วยจัดการสตริง เช่น เชื่อมหรือหาความยาว
บทที่ 8: พอยน์เตอร์ (Pointers)
พอยน์เตอร์คือตัวแปรที่เก็บที่อยู่หน่วยความจำ ใช้ * สำหรับ dereference
#include <stdio.h>
int main() {
int num = 10;
int *ptr = # // ชี้ไปที่ num (& คือ address-of)
printf("Value of num: %d\n", num); // 10
printf("Address of num: %p\n", &num); // ที่อยู่ (เช่น 0x7fff...)
printf("Value via ptr: %d\n", *ptr); // 10 (dereference)
*ptr = 20; // เปลี่ยนค่าผ่านพอยน์เตอร์
printf("New num: %d\n", num); // 20
return 0;
}
คำอธิบาย:
พอยน์เตอร์ช่วยเข้าถึงและแก้ไขค่าผ่านที่อยู่ เหมาะสำหรับ dynamic data
บทที่ 9: โครงสร้าง (Structures) และยูเนียน (Unions)
Structure คือกลุ่มตัวแปรต่างชนิดกัน Union เหมือน structure แต่ใช้หน่วยความจำร่วม
#include <stdio.h>
struct Person { // กำหนด structure
char name[20];
int age;
float height;
};
int main() {
struct Person p1; // สร้าง instance
strcpy(p1.name, "John");
p1.age = 30;
p1.height = 1.80;
printf("Name: %s, Age: %d, Height: %.2f\n", p1.name, p1.age, p1.height);
return 0;
}
คำอธิบาย:
Structure ใช้เก็บข้อมูลที่เกี่ยวข้องกัน เช่น ข้อมูลบุคคล
#include <stdio.h>
union Data {
int i;
float f;
char c;
};
int main() {
union Data d;
d.i = 10;
printf("Int: %d\n", d.i); // 10
d.f = 3.14; // overwrite i
printf("Float: %.2f\n", d.f); // 3.14 (i เปลี่ยนไป)
return 0;
}
คำอธิบาย:
Union ประหยัดหน่วยความจำโดยใช้พื้นที่เดียวกัน
บทที่ 10: การจัดการไฟล์ (File Handling)
ใช้ <stdio.h> สำหรับเปิด/อ่าน/เขียนไฟล์ โหมด: "r" (read), "w" (write), "a" (append)
#include <stdio.h>
int main() {
FILE *file; // พอยน์เตอร์ไฟล์
// เขียนไฟล์
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file\n");
return 1;
}
fprintf(file, "Hello, File!\n");
fclose(file);
// อ่านไฟล์
file = fopen("example.txt", "r");
char buffer[50];
fgets(buffer, 50, file);
printf("Content: %s", buffer);
fclose(file);
return 0;
}
คำอธิบาย:
fopen เปิดไฟล์, fprintf/fgets เขียน/อ่าน, fclose ปิดเพื่อปล่อยทรัพยากร
บทที่ 11: การจัดสรรหน่วยความจำแบบไดนามิก (Dynamic Memory Allocation)
ใช้ <stdlib.h> สำหรับ malloc, calloc, realloc, free เพื่อจัดสรรหน่วยความจำตอนรันไทม์
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
arr = (int*) malloc(n * sizeof(int)); // จัดสรรสำหรับ 5 int
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
printf("%d ", arr[i]); // 1 2 3 4 5
}
printf("\n");
free(arr); // ปล่อยหน่วยความจำ
return 0;
}
คำอธิบาย:
malloc จัดสรรหน่วยความจำ dynamic, free ป้องกัน memory leak
บทที่ 12: คำสั่งพรีโปรเซสเซอร์ (Preprocessor Directives)
ใช้ # สำหรับกำหนดแมโคร, รวมไฟล์, เงื่อนไขคอมไพล์
#include <stdio.h>
#define PI 3.14159 // แมโครค่าคงที่
#define SQUARE(x) (x * x) // แมโครฟังก์ชัน
int main() {
printf("PI: %.5f\n", PI);
printf("Square of 5: %d\n", SQUARE(5)); // 25
#ifdef DEBUG // ตรวจสอบกำหนดหรือไม่
printf("Debug mode\n");
#endif
return 0;
}
คำอธิบาย:
Preprocessor แปลงโค้ดก่อนคอมไพล์ เช่น แทนที่ PI ด้วย 3.14159
ตัวอย่างโค้ดโปรแกรมภาษาซี
#include <stdio.h>
char name[] = "นายคมกฤษ ไพศรี";
float m;
float H;
float BMI;
int main() {
printf("%s เลขที่ 1 \nส่งงานวันจันทร์ที่ 25 สิงหาคม 2568",name);
printf("\nกรุณากรอกน้ำหนักของคุณ : ");
scanf("%f",&m);
printf("\nกรุณากรอกส่วนสูงของคุณ : ");
scanf("%f",&H);
BMI=m/((H/100)*(H/100));
printf("\nค่า BMI ของคุณเท่ากับ : %.2f",BMI);
if (BMI < 18.5)
printf("\nคุณผอมเกินไป");
else if (BMI < 25)
printf("\nคุณมีน้ำหนักปกติ");
else if (BMI < 30)
printf("\nคุณมีน้ำหนักเกิน");
else
printf("\nคุณอ้วน");
return 0;
}
#include <stdio.h>
char name[] = "นายคมกฤษ ไพศรี";
int score;
int main() {
printf("%s เลขที่ 1 \nส่งงานวันจันทร์ที่ 1 กันยายน 2568",name);
printf("\nกรุณากรอกคะแนนของคุณ : ");
scanf("%d",&score);
printf("\nคะแนนของคุณเท่ากับ : %d",score);
if (score >= 80)
printf("\nคุณได้เกรด 4.0");
else if (score >= 75)
printf("\nคุณคุณได้เกรด 3.5");
else if (score >= 70)
printf("\nคุณได้เกรด 3.0");
//ทำ 2.5,2.0,1.5,1.0 เพิ่มเอง
else
printf("\nคุณได้เกรด 0");
return 0;
}