Skip to content

Instantly share code, notes, and snippets.

// Example compilation:
// gcc index_out_of_bounds.c -std=c99 -Wall -Wextra
//
// Compiles with ZERO warnings/errors on gcc 10.2.0
//
// Adding -O2 or above causes 2 warnings
// for the printf line (this is by design of gcc, not a bug):
// array subscript -80 is below array bounds of ‘const int[5]’ [-Warray-bounds]
// array subscript 80 is above array bounds of ‘const int[5]’ [-Warray-bounds]
//
// Tested with gcc 10.2.0 and -std=c99
// Author: github.com/Costava
int main(void) {
// gcc will NOT warn about this line from -Wall or -Wextra
// -Wsign-conversion is needed:
/// warning: unsigned conversion from ‘int’ to ‘unsigned int’ changes
// value from ‘-1’ to ‘4294967295’ [-Wsign-conversion]
const unsigned int foo0 = -1;
// Example compilation:
// gcc fizzbuzz.c -std=c99 -Wall -Wextra
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 100; i += 1) {
if (i % 3 == 0) {
if (i % 5 == 0) {
puts("FizzBuzz");
// Example compilation:
// gcc print_double.c -std=c99 -Wall -Wextra
#include <stdio.h>
int main(void) {
double num;
num = 0.25; printf("%f %a\n", num, num);// 0.250000 0x1p-2
num = 0.50; printf("%f %a\n", num, num);// 0.500000 0x1p-1
@Costava
Costava / typesizes.c
Created February 19, 2021 06:29
Print the size of many types.
// Example compilation:
// gcc typesizes.c -o typesizes -std=c99 -Wall
// Info on integer types: https://en.cppreference.com/w/c/types/integer
#include <stdint.h>
#include <stdio.h>
int main(void) {
printf("sizeof(int): %ld\n", sizeof(int));
// Sort of a benchmark --- if touching every pixel every frame,
// a real program should not hope to get a higher frames per second
// than this simple demo.
//
// Single process and thread.
// Every frame:
// - Set every pixel in array to fully opaque rand color.
// - SDL_UpdateTexture
// - SDL_RenderCopy
// - SDL_RenderPresent
// Sort of a benchmark --- if touching every pixel every frame,
// a real program should not hope to get a higher frames per second
// than this simple demo.
//
// Single process and thread.
// Using SDL_Renderer:
// - SDL_SetRenderDrawColor then SDL_RenderDrawPoint for each pixel each frame.
#include <stdbool.h>
#include <stdio.h>
@Costava
Costava / windows_locks_ntfs.md
Created October 4, 2020 17:53
Stop Windows 10 from locking NTFS partition(s) on multiple-boot computer.

Windows 10 locks shared NTFS partition(s)

Problem: Windows 10 locks shared NTFS partition(s). From Linux (multiple boot), files on the NTFS partition(s) cannot be deleted and programs depending on the partition's data cannot function properly.

Try 0: Shut down Windows

  1. Boot into Windows
  2. Shut down Windows (specifically not restart)
@Costava
Costava / real_mode_boot_to_A.asm
Created September 28, 2020 23:29
Continuously print 'A' at boot
; Continuously print 'A' at boot
;
; Tested with NASM version 2.15.04
; and QEMU emulator version 5.1.0
;
; To compile:
; nasm -f bin real_mode_boot_to_A.asm -o real_mode_boot_to_A.bin
;
; To run:
; qemu-system-x86_64 real_mode_boot_to_A.bin
@Costava
Costava / sdl2_tex_component_order.c
Created September 28, 2020 23:20
SDL_Texture color component order in pixels array (SDL2)
// If a texture has format SDL_PIXELFORMAT_ABGR8888,
// then the red component is the first byte in the pixels array of the texture
// Tested on a little-endian (x86) system
// Would the red component also come first in the array on a big-endian system?
//
// Tested with SDL2 version 2.0.12
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>