Funny and strange UNIX happenings and C programming tips.

Funny and cool Linux tips 1337 or Leet in the UNIX time. -01:44:57– gordon@deusexmachina [~]$ date +%s 1337096699-01:44:57– gordon@deusexmachina [~]$ date +%s 1337096699 The missing days in 1752. I have mentioned this before, but it is worth mentioning again. -01:45:31– gordon@deusexmachina [~]$ cal 9 1752 September 1752 Su Mo Tu We Th Fr Sa 1 … Read more

C code that will select a random word from an array and include it in a string.

This code will pick a random word from the x[] array and include it in the printed string. This program will also get your current Linux username to customize the output. I am sure this code would be very useful to someone who wants to know how to handle arrays in C. #include <stdio.h> #include … Read more

C code that will open a file and print the contents to the terminal.

This code will print the contents of a file to the terminal. Feel free to use this in your own projects if you wish. #include <stdio.h>   #define MEM "/proc/diskstats"   int main (void) {   FILE *g; char Meminfo[40]; g = fopen(MEM, "r"); if(!g) { printf ("Sorry, I cannot open: %s.\n", MEM); } else … Read more

The simplest hello world program you can write in C.

This is a very simple Hello World program that you can write in C. int main (void) {   write (1, "Hello World\n", 14); return 0; }int main (void) { write (1, "Hello World\n", 14); return 0; } Just compile the program like this: localhost% cc minimal.c -o small minimal.c: In function ‘main’: minimal.c:2:5: warning: … Read more

A nice sample of code that will render a spiral on your terminal screen. This could be useful indeed.

This code sample will render a spiral on your terminal when you compile and execute it. Very interesting indeed. #include <sstream> #include <iostream> #include <string>   std::string rle = "32 2@14 1n16 11@9 2@10 1n13 3@13 3@8 1@7 1n10 2@21 2@7 1@5 1n7 2@27 1@6 1@4 " "1n6 1@10 10@10 2@5 2@2 1n4 2@7 4@9 … Read more

Very cool code that prints a tick sign in asterisks. This is awesome C++ code.

This code prints out a tick sign using asterisks. I tested this code on Fedora 19 and gcc 4.8 and it compiles without any problems. #include <iostream> #include <iomanip>   using std::cout;   int main() {   cout<<std::setw(8)<<"*"<<"n"; cout<<std::setw(7)<<"*"<<"n"; cout<<std::setw(6)<<"*"<<"n";   cout<<"* *"<<"n"<<"* *"<<"n"<<"*"; cout<< "n";   return 0; }#include <iostream> #include <iomanip> using std::cout; … Read more

Some very good Linux resources for learning programming and BASH scripting.

http://gentoomen.org/tutorials/resources.html. The gentoomen.org resources page. Very good list of links to various tutorials and help pages. http://tldp.org/LDP/abs/html/. The Advanced BASH scripting guide. ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf. The VIM guideboook. http://stommel.tamu.edu/~baum/programming.html. 3000 useful guides to programming and Linux documentation and help pages. http://samizdat.mines.edu/howto/HowToBeAProgrammer.html. How to be a programmer. A short but comprehensive summary. http://www.psychocats.net/ubuntu/index. Ubuntu resources page. Everything you … Read more

Wrapping the printf() statement onto multiple lines in C and some other useful samples.

This code sample shows how we are wrapping a printf() statement onto multiple lines using backslashes. #include "stdio.h"   #define hello "Hello World."   int main(int argc, char* argv[]) { printf("This is a very long sentence we are handing down\n"\ "Mr smith, do you have anything to say for yourself"\ "?");   printf("%s\n", hello);   … Read more

Cool C programming trick, run /bin/sh with the C execve() function.

This programming trick is pretty cool, you can run the /bin/sh shell with a C program. The execve() function is useful for running a command within a C program and passing arguments to it, but I have replaced the arguments with NULL instead, that is what you put when you do not need arguments. #include … Read more

Cool C programs and some very useful code snippets to make programming even more fun.

A very obfuscated C program. typedef unsigned char t;t*F="%c",l[]="|\\/=_ \n](.\0(),*(.(=(}*.)[[*.",N=’\n’,* r;typedef(*H)();extern H Ar;Q(a){return(a|-a)>>31;}H S(c,a){return(H)(a&~c|(int )Ar&c);}extern t*ist;V(t*u){*u^=*u&2^(*u>>7)*185;}Z(t*u,t n){*u-=n;}e(t c,H h){ R(h,Q(* r^c));} I(){r=l +7-4*Q( getchar ()^*l); }R(H h, int c){Ar=S (c,h);- main() ;}P(){r ++;}z() { O(&N);} O(t*c){ printf( F,+*c); }T(){r= "This is not a function\n" ;}w(U){ U=Z(r,8 ); r-=~Q(* r/8-4); return 0; }M(){r= ist-68; } h(){t … Read more

Running a command within another program. How to do this with C.

This code snippet will run the date command. The execl() function is very useful for executing a command within your C program.You replace the NULL identifiers with any extra arguments to pass to the program. I prefer this over the system() function. #include <unistd.h>   int main(void) {   execl("/bin/date", "%c", NULL, NULL, NULL); return … Read more