C - SDL (Simple Directmedia Layer) - Hello World!

You probably already installed SDL library on your computer. If not, you can follow this SDL tutorial setup on Eclipse . Let’s test it with a Hello World! tutorial. For Windows OS, do not forget to add the SDL.dll in the folder where there is your .exe. To have an easy application than ever beginners can make, we need to follow five steps: Include the right library In our case, we have to include the SDL/SDL.h header. ...

November 17, 2011 · Mi-K

C - Library functions - Using strcmp()

In this tutorial we will see how to use the strcmp() function. It returns: -1 if the first argument is less than the second 0 if the first argument is equal to the second 1 if the first argument is greater than the second We need the string.h include to use this function. Let’s see this example of the strcmp() function: // compare.c #include <stdio.h> #include <string.h> int main(int ac, char *av[]) { char *word; int cmp; word = "badprog"; cmp = strcmp(av[1], word); if (!cmp) printf("Match, indeed, strcmp = %d\n", cmp); else printf("Do not match, indeed, strcmp = %d\n", cmp); return (0); } Compiling and executing: ...

June 7, 2011 · Mi-K

C - Recursion - Displaying a string

In this tutorial we will see how to display a string with the recursive method. Let’s see this example of the recursive and the iterative method with the functions below: // recursion.c #include <string.h> #include <stdio.h> #include <unistd.h> void displayString_iterative(char *str) { int i; i = 0; while (str[i]) write(1, &str[i++], 1); } void displayString_recursive(char *str) { if (strlen(str) <= 0) return; write(1, &str[0], 1); displayString_recursive(str + 1); } int main() { displayString_recursive("Hello BadproG.com!\n"); displayString_iterative("Hello BadproG.com!\n"); return (0); } Let’s compile and execute it: ...

June 7, 2011 · Mi-K

C - Character manipulation - Allocation memory

It is hard to understand conceptually what is happening when we create a string with and without malloc() allocation. That is what we will see in this tutorial. Let’s see some examples of allocation memory, with and without malloc(), and let’s see what is happening in the memory: #include <stdio.h> #include <stdlib.h> int main() { char *str1; char *str2; char *str3; char *str4; char *str5; str1 = "hell"; str2 = "hello"; str3 = "hello"; str4 = "hello"; str5 = malloc(sizeof(*str2) * 5 + 1); str5[0] = 'h'; str5[1] = 'e'; str5[2] = 'l'; str5[3] = 'l'; str5[4] = 'o'; str5[5] = '\0'; printf("str1 | %c - %c - %c - %c\n", str1[0], str1[1], str1[2], str1[3]); printf("str1 | %p - %p - %p - %p\n\n", &str1[0], &str1[1], &str1[2], &str1[3]); printf("str2 | %c - %c - %c - %c - %c\n", str2[0], str2[1], str2[2],str2[3], str2[4]); printf("str2 | %p - %p - %p - %p - %p\n\n", &str2[0], &str2[1], &str2[2], &str2[3], &str2[4]); printf("str3 | %c - %c - %c - %c - %c\n", str3[0], str3[1], str3[2],str3[3], str3[4]); printf("str3 | %p - %p - %p - %p - %p\n\n", &str3[0], &str3[1], &str3[2], &str3[3], &str3[4]); printf("str4 | %c - %c - %c - %c - %c\n", str4[0], str4[1], str4[2],str4[3], str4[4]); printf("str4 | %p - %p - %p - %p - %p\n\n", &str4[0], &str4[1], &str4[2], &str4[3], &str4[4]); printf("str5 | %c - %c - %c - %c - %c\n", str5[0], str5[1], str5[2],str5[3], str5[4]); printf("str5 | %p - %p - %p - %p - %p\n\n", &str5[0], &str5[1], &str5[2], &str5[3], &str5[4]); free(str5); return (0); } Let’s compile it and execute it: ...

May 18, 2011 · Mi-K

C - Linked lists - Head and End

I hope you liked my fantastic simple example of linked list (OK it was not so exciting, but it works fine). SO, let’s try today a tutorial of a linked list where we will able to add and display elements in our linked list, first by adding elements from the head and then from the queue (or the end). Let’s start our example of this famous linked list by adding elements from the head and from the end: ...

April 16, 2011 · Mi-K

C - Library functions - Using daemon()

In this tutorial we will see the daemon() function. This function is designed to launch a program in background. So when you exectute a program it will start and if you want to stop it, just make a Ctrl + C. The program will stop instantly. Now, with the daemon function, the program will not stop even with a Ctrl + C. To stop it, you have to kill the process with the kill command. ...

April 15, 2011 · Mi-K

C - General Programming - Return value of a if statement without any condition

To test a condition in a if statement without any condition, the result has to be other than 0 to test a true condition. Let’s see an example of a if statement without any condition: #include <stdio.h> int checkIt() { int a; a = 0; if (a == 0) return (-1); return (0); } int main() { int number; number = checkIt(); if (number) printf("True - Value was %d.\n", number); else printf("False - Value was %d.\n", number); return (0); } Compile and execute it: ...

April 4, 2011 · Mi-K

C - Bitwise operation - Bit shift in decimal

Let’s try to transform a binary into a decimal with a simple loop. Let’s see this example of a bit shift into a decimal display: #include <stdio.h> int main() { int i; int j; i = 0; j = 1; printf("%d\n", j); while (i < 32) { j = j << 1; printf("%d\n", j); i++; } return (0); } Let’s compile and execute it: $ cc main.c && ./a.out Result: 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 -2147483648 0 I’m seeing some of you saying : “Uh why?” ...

March 26, 2011 · Mi-K

C - Character manipulation - Displaying a char

Today I will show you how to transform a decimal number, an hexadecimal number and a binary number into the same character. And you will see that this transformation is made by the terminal, the shell and Linux (or UNIX ;). Let’s see an example, with 3 files and after with printf(): First of all, you have to create 3 files named file1, file2 and file3. #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { int fd1; int fd2; int fd3; char i; char j; char k; i = 120; j = 0x78; k = 0b01111000; fd1 = open("file1", O_WRONLY); fd2 = open("file2", O_WRONLY); fd3 = open("file3", O_WRONLY); write(fd1, &i, 1); write(fd2, &j, 1); write(fd3, &k, 1); } Compile and execute it: ...

March 22, 2011 · Mi-K

C - Linked lists - Simple example

Below, certainly the most easy example of the linked lists. Let’s see this example as a FILO, meaning First In Last Out: #include <stdlib.h> #include <stdio.h> /* ** STRUCTURE */ typedef struct s_list { int nb; struct s_list *next; } t_list; /* ** ADD */ t_list *add(t_list *element, int i) { t_list *link; link = malloc(sizeof(*link)); link->nb = i; link->next = element; return (link); } /* ** DISPLAY */ void display(t_list *toDisplay) { while (toDisplay != NULL) { printf("toDisplay->nb = %d\n", toDisplay->nb); toDisplay = toDisplay->next; } } /* ** MAIN */ int main() { t_list *new; int i; new = NULL; i = 0; while (i < 18) new = add(new, i++); display(new); return (0); } Result: ...

February 13, 2011 · Mi-K