Skip to content

Instantly share code, notes, and snippets.

@yothinin
yothinin / ex01_client.c
Created October 31, 2025 16:05
socket client with c
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <locale.h>
int main() {
setlocale(LC_ALL, ""); // รองรับพิมพ์ภาษาไทย
int sockfd;
struct sockaddr_in servaddr;
@yothinin
yothinin / ex01_server.c
Created October 31, 2025 16:04
socket server with c
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <locale.h>
int main() {
setlocale(LC_ALL, ""); // ให้ printf แสดง UTF-8 ได้
int sockfd;
struct sockaddr_in servaddr, cliaddr;
@yothinin
yothinin / callback.c
Created February 27, 2024 13:16
Example to create callback function in C language.
#include <stdio.h>
#include <string.h>
// ประกาศฟังก์ชัน iterateArray ที่รับอาร์เรย์และฟังก์ชัน callback เป็นพารามิเตอร์
void iterateArray(void* array, int length, size_t size, void (*callback)(void*, int)) {
for (int i = 0; i < length; i++) {
callback(array + i * size, i);
//callback (array[i], i); //ผิดไม่สามารถแบบนี้ได้ เพราะเป็น void*
}
}
@yothinin
yothinin / ex_bubblesort.c
Created February 14, 2024 15:43
Example for bubble sort integer in an array.
#include <stdio.h>
// ฟังก์ชันสำหรับสลับค่าของสองตัวแปร
void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// ฟังก์ชันสำหรับ Bubble Sort
@yothinin
yothinin / ex_linkedlist.c
Created February 13, 2024 17:10
Linked list example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node {
char name[100]; // สมมติว่าชื่อมีความยาวสูงสุด 100 ตัวอักษร
struct Node* next;
} Node;
@yothinin
yothinin / ex_dyn-garray.c
Created February 11, 2024 04:22
Exaple for use GArray to add data into dynamic array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
int main() {
GArray *name_array = g_array_new(TRUE, FALSE, sizeof(char *)); // สร้าง GArray สำหรับเก็บ pointer ไปยังสตริง
g_print("Enter names (press Enter without input to stop):\n");
@yothinin
yothinin / ex_dynarray.c
Last active February 27, 2024 14:17
Example to create dynamic allocation memory with C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char **name = NULL; // ประกาศ pointer ไปยัง array ของ pointer ที่เก็บสตริง
int size = 0; // จำนวนสมาชิกใน array name
char buffer[100]; // เก็บข้อมูลที่ผู้ใช้ป้อน
printf("Enter names (press Enter without input to stop):\n");
@yothinin
yothinin / ex_treeview.c
Last active February 27, 2024 14:18
Example program demonstrating the functionality of GtkTreeView and the usage of GArray
/*
* Example program demonstrating the functionality of GtkTreeView and the usage of GArray.
* Compile: gcc -g -o ex_treeview ex_treeview.c `pkg-config --cflag --libs gtk+-3.0`
*/
#include <gtk/gtk.h>
typedef struct {
gint index;
gchar *item;
gchar *path;
@yothinin
yothinin / ex_gtklistbox.c
Created January 2, 2024 12:34
Example for GtkListBox.
#include <gtk/gtk.h>
static void activate(GtkApplication *app, gpointer user_data) {
// Create a new toplevel window
GtkWidget *window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "GtkListBox Example");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
// Create a GtkListBox
GtkWidget *listbox = gtk_list_box_new();
@yothinin
yothinin / ex_structwidget.c
Created February 9, 2023 11:55
Example for use struct to pass GtkWidget to function.
/*
* This example very simple to use Gtk for beginers
* in the past I used this style to coding but now,
* I use Glade to generate ui file to display GUI
* it's very easy to manipulate layout of the widgets
*/
#include <gtk/gtk.h>
typedef struct _MyWidgets {
GtkWidget *window;