C
READ FILE
/* read file line by line demo */
#include<stdio.h>
#include<stdio.h>
int main(void){
FILE *fp = fopen("lipsum.txt", "r");
if(!fp){
fprintf(stderr, "%s\n", "Couldn't open file");
}
char *line = NULL;
size_t linecap = 0;
ssize_t linelen;
while((linelen = getline(&line, &linecap, fp)) != -1){
printf("%s\n", line);
}
return 0;
}
POINT
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct Point {
int x;
int y;
} Point;
Point* createPoint(int x, int y){
Point *p = malloc(sizeof(Point));
if(p == NULL){ //malloc can fail, but unlikely
fprintf(stdout, "%s", "Error allocating memory...");
exit(1);
}
p->x = x;
p->y = y;
return p;
}
void swap(Point* p) {
int tmp = p->x;
p->x = p->y;
p->y = tmp;
}
int main(void){
Point *p = createPoint(75, 12);
assert(p->x == 75 && p->y == 12);
swap(p);
assert(p->x == 12 && p->y == 75);
free(p);
return 0;
}
write text to file
#include <stdio.h>
int main(void){
FILE *file = fopen("example.txt", "w");
if (file == NULL){
perror("Error opening file");
return 1;
}
fprintf(file, "Hello text file !\n");
fclose(file);
return 0;
}
WRITE 1 BYTE TO FILE
/*
* Write a single byte to a file.
* Overwrite if already exists.
* Creates if doesn't exist.
*/
#include<stdio.h>
#include<stdlib.h>
int main(void) {
FILE *f;
f = fopen("foo.txt", "wb");
if(f == NULL){
fprintf(stderr, "%s", "Error opening file...");
exit(1);
}
char byte = 255;
fwrite(&byte, sizeof(char) ,1 , f);
fclose(f);
return 0;
}
simple C char stack
#include<stdio.h>
#include<assert.h>
typedef struct Stack {
char array[256];
int pos;
} Stack;
void Stack_init(Stack* stack) {
stack->pos = -1;
}
void Stack_push(Stack* stack, char c){
if (stack->pos<255) {
stack->array[++stack->pos] = c;
} else {
printf("err, stack overflow\n");
char array[256]; }
}
char Stack_pop(Stack* stack){
if (stack->pos == -1) {
printf("err : stack underflow\n");
return '\0';
}
return stack->array[stack->pos--];
}
int main(void) {
Stack stack;
Stack_init(&stack);
Stack_push(&stack, 'a');
Stack_push(&stack, 'b');
assert(Stack_pop(&stack) == 'b');
assert(Stack_pop(&stack) == 'a');
/*
while(stack.pos != -1) {
printf("%c\n", Stack_pop(&stack));
}
*/
return 0;
}
C dereferencing to NULL
int* p1x = NULL;
*p1x = 72 ; // SEGFAULT !! DEREFERENCING NULL
OK :
int* px = NULL;
int x = malloc(sizeof(int));
px = &x;
NODES
#include<stdio.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
void printNode(Node *node){
if(node==NULL) return;
printf("%d\n", node->data);
printNode(node->next);
}
void addNode(Node *node, Node *newNode){
if(node->next==NULL){
node->next = newNode;
newNode->next=NULL;
return;
}
addNode(node->next, newNode);
}
int main(void){
Node n;
n.data = 15;
n.next=NULL;
printNode(&n);
printf("-------\n");
Node n2;
n2.data = 30;
addNode(&n, &n2);
printNode(&n);
printf("-------\n");
Node n3;
n3.data = 60;
addNode(&n, &n3);
printNode(&n);
return 0;
}
DEQUE
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct Node {
struct Node* next;
void* value;
} Node;
typedef struct Deque {
Node* first;
Node* last;
int pos;
} Deque;
void Deque_init(Deque* dq) {
dq->first = NULL;
dq->last = NULL;
dq->pos = -1;
}
void* Deque_popfirst(Deque* dq) {
if (dq->pos == -1) {
return NULL; // Deque is empty
}
Node* temp = dq->first;
void* value = temp->value;
dq->first = temp->next;
free(temp);
if (dq->first == NULL) {
dq->last = NULL;
}
dq->pos--;
return value;
}
void* Deque_poplast(Deque* dq) {
if (dq->pos == -1) {
return NULL; // Deque is empty
}
Node* temp = dq->last;
void* value = temp->value;
if (dq->first == dq->last) {
dq->first = NULL;
dq->last = NULL;
} else {
Node* current = dq->first;
while (current->next != dq->last) {
current = current->next;
}
current->next = NULL;
dq->last = current;
}
free(temp);
dq->pos--;
return value;
}
void Deque_push_front(Deque* dq, void* item) {
Node* new_node = (Node*)malloc(sizeof(Node));
assert(new_node != NULL);
new_node->value = item;
new_node->next = dq->first;
if (dq->pos == -1) {
dq->last = new_node;
}
dq->first = new_node;
dq->pos++;
}
void Deque_push_back(Deque* dq, void* item) {
Node* new_node = (Node*)malloc(sizeof(Node));
assert(new_node != NULL);
new_node->value = item;
new_node->next = NULL;
if (dq->pos == -1) {
dq->first = new_node;
dq->last = new_node;
} else {
dq->last->next = new_node;
dq->last = new_node;
}
dq->pos++;
}
int main(void) {
int* x1 = malloc(sizeof(int));
*x1=1;
int* x2 = malloc(sizeof(int));
*x2=2;
int* x3 = malloc(sizeof(int));
*x3=3;
Deque dq;
Deque_init(&dq);
assert(!Deque_popfirst(&dq));
assert(!Deque_poplast(&dq));
Deque_push_front(&dq, x1);
Deque_push_front(&dq, x2);
Deque_push_back(&dq, x3);
// // 2 1 3
assert(*((int*)Deque_popfirst(&dq))== 2);
assert(*((int*)Deque_poplast(&dq))== 3);
assert(*((int*)Deque_popfirst(&dq))== 1);
assert(!Deque_popfirst(&dq));
assert(!Deque_poplast(&dq));
/*
int* x1 = malloc(sizeof(int));
*x1=0;
printf("%d\n", *x1);
free(x1);
*/
/*
int* x1 = malloc(sizeof(int));
*x1 = 72;
Deque_push_front(&dq, x1);
printf("%d\n", *dq.first);
*/
return 0;
}
HELLO WORLD SDL2
// SDL2 Hello, World!
// compile with : gcc main.c -lSDL2 -o out
// run with ./out
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.h>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
int main(int argc, char* args[]) {
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* bg = NULL;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError());
return 1;
}
window = SDL_CreateWindow("hello_sdl2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
bg = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
SDL_SetTextureBlendMode(bg, SDL_BLENDMODE_BLEND);
if (window == NULL) {
fprintf(stderr, "could not create window: %s\n", SDL_GetError());
return 1;
}
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = 100;
rect.h = 100;
uint32_t canvas[SCREEN_WIDTH*SCREEN_HEIGHT];
for (int i=0; i<SCREEN_WIDTH*SCREEN_HEIGHT; i++) {
if(i<SCREEN_WIDTH*SCREEN_HEIGHT/2){
canvas[i] = 0xFFFF4444;
}
else {
canvas[i] = 0xFFFFFFFF;
}
}
uint32_t frame_start = 0;
uint32_t frame_end= 0;
uint32_t frame_count= 0;
bool quit = false;
while (!quit) {
frame_count = SDL_GetTicks();
frame_start = frame_count;
SDL_Event event;
while (SDL_PollEvent(&event) != 0) {
switch (event.type) {
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_SPACE :
break;
case SDLK_q :
quit = true;
break;
case SDLK_h:
break;
case SDLK_l:break;
case SDLK_LEFT:
rect.x-=10;
break;
case SDLK_RIGHT:
rect.x+=10;
break;
case SDLK_UP:
rect.y-=10;
break;
case SDLK_DOWN:
rect.y+=10;
break;
default :
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_SPACE :
break;
case SDLK_q :
quit = true;
break;
case SDLK_h:break;
case SDLK_l:break;
case SDLK_LEFT: break;
case SDLK_RIGHT: break;
case SDLK_UP: break;
case SDLK_DOWN: break;
default :
}
break;
case SDL_MOUSEWHEEL:
if (event.wheel.y > 0) {} else if (event.wheel.y < 0) {}
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) {} else if (event.button.button == SDL_BUTTON_RIGHT) {}
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT) {} else if (event.button.button == SDL_BUTTON_RIGHT) {}
break;
case SDL_MOUSEMOTION:
break;
default:
}
}
SDL_UpdateTexture(bg, NULL, &canvas, SCREEN_WIDTH * sizeof(uint32_t));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, bg, NULL, NULL);
// draw rectangle
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // < set drawing color to white
SDL_RenderFillRect(renderer, &rect);
// set drawing color to white
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
// render everything ?
SDL_RenderPresent(renderer);
// ~~~~ regulate fps ~~~~
frame_end = SDL_GetTicks();
const int elapsed_time = frame_end - frame_start;
const int time_wanted = 1000 / 60;
if (elapsed_time < time_wanted) {
SDL_Delay(time_wanted - elapsed_time);
}
}
SDL_DestroyTexture(bg);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Function pointers
#include <stdio.h>
#include <assert.h>
int add(int a, int b) { return a+b;}
int sub(int a, int b) { return a-b;}
// typedef int (*funPtr)(int, int); // could make more readable with this
int do_op(int a, int b, int (*op)(int, int)) {
return op(a, b);
}
int main(void) {
assert(do_op(5, 5, add) == 10);
assert(do_op(5, 5, sub) == 0);
return 0;
}