Skip to content

Instantly share code, notes, and snippets.

@gustavorv86
gustavorv86 / macro_print_binary.c
Last active October 3, 2019 06:15
Print data type as binary format
#include <stdio.h>
#include <stdlib.h>
// MACRO PRINTB
#define PRINTB(x) \
for(int i=(sizeof(x)*8-1) ; i>=0 ; i--) { \
(x & (1 << i)) ? printf("1") : printf("0") ; \
if (i % 4 == 0) { printf(" "); } \
} \
@gustavorv86
gustavorv86 / Agenda.c
Last active February 23, 2017 09:36
Adaptación del lenguaje C a la programación orientada a objetos
/*
File: Agenda.c
*/
#include "Agenda.h"
#include <stdio.h>
#include <stdlib.h>
void addLast(Agenda _this, Persona persona) {
AgendaNode node;
@gustavorv86
gustavorv86 / search.c
Last active April 12, 2017 06:35
Search algorythms in C/C++
#include "search.h"
unsigned int linear_search(int* array_items, unsigned int size, int search_item) {
unsigned int i;
for(i=0; i<size; i++) {
if(array_items[i] == search_item) {
/* elemento encontrado, devolvemos el indice 'i' */
return i;
@gustavorv86
gustavorv86 / array.c
Last active April 12, 2017 06:35
Dynamic array implementation in C/C++
#include "array.h"
#include <stdio.h>
#include <stdlib.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
ARRAY array_new(unsigned int size) {
ARRAY s_array;
@gustavorv86
gustavorv86 / LookAndFeel.java
Last active August 29, 2019 13:59
Class to set the default java Look And Feels
import javax.swing.UIManager;
public class LookAndFeel {
public static enum LAF {
CROSS,
SYSTEM,
METAL,
NIMBUS,
MOTIF,
@gustavorv86
gustavorv86 / sort.c
Last active April 12, 2017 06:33
Sort algorythms in C/C++
#include "sort.h"
#include <stdlib.h>
#define SWAP(a, b, tmp) ((tmp)=(a), (a)=(b), (b)=(tmp))
void bubble_sort(float*array, int size) {
int i, j;
float tmp;
@gustavorv86
gustavorv86 / rpi_rotary_encoder.c
Last active October 3, 2019 06:15
Raspberry PI Rotary Encoder Function Example
/*
Wiring
01 3.3V DC POWER --------------- +
06 GROUND ---------------------- GND
11 GPIO17 (GPIO_GEN0) ---------- CLK
12 GPIO18 (GPIO_GEN1) ---------- DT
13 GPIO19 (GPIO_GEN2) ---------- SW
Compile:
@gustavorv86
gustavorv86 / split_string.c
Last active February 6, 2023 08:21
Split a string (char*) by a character in C/C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void string_split(char * string, char sep, char *** r_array_string, int * r_size) {
int i, k, len, size;
char ** array_string;
// Number of substrings
@gustavorv86
gustavorv86 / file_copy.c
Last active August 13, 2019 11:15
POSIX GNU/Linux efficient copy file.
/**
* Copy the 'source' file to 'dest'. If 'dest' exists it will be
* overwritten.
*
* @param source Path to the source file.
* @param dest Path to the destination file.
* @return Number of bytes copied or -1 if cannot copy
* file.
*/
int file_copy(char * source, char * dest)
@gustavorv86
gustavorv86 / millisleep.c
Last active October 3, 2019 06:13
Function that pauses the execution of a program without affecting the signals.
#include <time.h> /* Needed for struct timespec */
/**
* Pauses the execution of a program without affecting the signals.
* Note: Compile with '-std=gnu11'.
*
* @param millis Milliseconds to sleep.
*/
void millisleep(int millis)
{