Skip to content

Instantly share code, notes, and snippets.

View joehakimrahme's full-sized avatar

Joe H. Rahme joehakimrahme

  • Red Hat
  • Milano
View GitHub Profile
@joehakimrahme
joehakimrahme / size.c
Created June 4, 2012 13:13
K&R (2.1): Size of types
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char **argv)
{
signed short i, min, max;
i = min = max = 0;
while (++i) {
@joehakimrahme
joehakimrahme / entab.c
Created June 4, 2012 09:16
K&R (1.21): Replace blanks by tabs
#include <stdio.h>
#define TABSTP 4
/* TODO: read this string from argv,
to test different scenarios */
char* input_str = "Something to test it";
int nexttbstp (int position);
int printblank(int start, int end);
@joehakimrahme
joehakimrahme / K&R: 1.21
Created June 4, 2012 09:13
Entab: Replace blanks by tabs
#include <stdio.h>
#define TABSTP 4
/* TODO: read this string from argv,
to test different scenarios */
char* input_str = "Something to test it";
int nexttbstp (int position);
int printblank(int start, int end);
@joehakimrahme
joehakimrahme / tableswitch.lua
Created May 16, 2012 08:28
Switch table in Lua
abs = function(x)
test = {
[true]=-x,
[x>0]=x,
[x==0]=0}
return test[true]
end
print (abs(-3)) -- outputs "3"
@joehakimrahme
joehakimrahme / table-ex2.lua
Created May 15, 2012 22:11
Programming in Lua
t3 = {1, 4, 5, 2, 31}
print (t3[3]) -- will output "5"
t4 = {a = "foo", b = 3}
print (t4.a) -- will output "foo"
print (t4.b) -- will output "3"
@joehakimrahme
joehakimrahme / table-ex1.lua
Created May 15, 2012 22:02
Lua presentation
t1 = { a=1, b="hello" }
print (t1["a"]) -- outputs "1"
t2 = { [1<4] = "foo", [1<10] = "bar" }
print (t2[true]) -- outputs "bar"
@joehakimrahme
joehakimrahme / gist:2702849
Created May 15, 2012 15:55
Lua multiline comments
--[[
print("hello world")
--]]
-- By adding a single hypen at the beginning of the top line, you effectively uncomment the whole block:
---[[
print("hello world")
--]]
function eat(food)
print ("I eat " .. food)
end
function drink(food)
print ("I drink " .. food)
end
function swallow (food)
func = {[true] = eat,
@joehakimrahme
joehakimrahme / example1.c
Created February 2, 2011 22:36
First use of the fork() function
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char** argv){
fork();
fprintf(stdout, "Hello World");
return EXIT_SUCCESS;
}
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char** argv){
fork();
/* The function getpid() returns the PID of the current process */
fprintf(stdout, "I'm a process, my PID is: %d", getpid());
return EXIT_SUCCESS;