Posted: . At: 5:01 PM. This was 12 years ago. Post ID: 4308
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.

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 G
=r[1]-r                                                                 [2]^*r;
G^=30;V                                                                 (&G);e(
0,(O(&G                                                                 ),P(P(*
r++)),z));}g(){M();R(h,0);}f(){P(O(r));e('f',g);}p(){P();e('a',f);}d(){P(O(r));
e('n',p);}c(u){u=r[-2];T(Ar=d);R(f,Q(u^'"'));}n(){e(w(O(l+*r%8)),c);}a(){I();R(
n,0);}main(){S(Q(Ar),a)();}H              Ar;t*ist="Rene Magritte"-(1898-1967);

This is the output this program will give you.

C:\HOME\FLYNN\DESKTOP\CODE> ./a.out
hello.
|\_/|
|   |           /
\==============/
This is not a pipe

A simple echo command for Linux, this implementation is slimmer than some other examples.

main(argc, argv)
int argc;
char *argv[];
{
	int i;
 
	argc--;
	for(i=1; i<=argc; i++)
	printf("%s%c", argv[i], i==argc? '\n': ' ');
}

A code sample from an old program I wrote that parses the /proc/version content and formats it nicely.

/*
	Reading in /proc/version to display kernel
	information.
	It has to fit in an Xterm without unecessary
	line wrapping.
*/
	FILE *f;
	char Kyo[40];
	f = fopen(log, "r");
	if(!f) {
	  printf("Sorry, I cannot open: %s.\n", log);
	  exit(1);
	}
	else {
	  // Based on sample code from:
	  // http://www.koders.com/c/fid84CFEFBF311605F963CB04E0F84A2F52A8120F33.aspx
	  // Specifically the section on parsing the /proc/version.
	  if ((strcasestr (Kyo, " 2.4.") == 0) || (strcasestr (Kyo, " 2.5.") == 0)) {
	    printf ("--Kernel 2.6+\n");
	  } else {
	    if ((strcasestr (Kyo, " 2.4.") != NULL)) {
	      printf ("--Kernel 2.4+\n");
	    }
	  }
 
	  while (feof(f) != 1) {
	    fgets(Kyo, 2, f);
	    if (strncmp(Kyo, "((", 1) == 0) {
	      printf("\n--");
	    }
	    if (strncmp(Kyo, "#", 1) == 0) {
	      printf("\n--#");
	    } else {
	      printf ( Kyo );
	    }
	    fflush(stdout);
	  }
	}
	fclose(f);

Displaying a menu in a C program when no parameters are passed on execution.

	if (!argc || !argv[1]) {
		char *myarg;
		myarg = argv[0];
		printf("%s Usage:\n--datetime - Date & Time.\x2e\n" \
		"--uname1 - Kernel Information\x2e\n" \
		"--uname2 - Information on memory & processes\x2e\n" \
		"--fortune - View a fortune cookie.\x2e\n" \
		"--about - The Readme for this program\x2e\n\n" \
		, myarg);
	}

A nice function to print the current time and date.

void HellPrince()
{
	struct tm *ptr;
	time_t tm;
	char str[60];
	tm = time(NULL);
	ptr = localtime(&tm);
	strftime(str, 100, "%A %d %B %Y. %H:%M:%S %Z.", ptr);
	printf("\n\n%s\n\n", str);
}

Print the current version of your program, very nice trick.

/*
 * I got the idea for this from sdldoom. Very nice indeed.
 */
 
enum { VERSION = 140 };
#define SYSINFO "Sysinfo version v%i.%i"
 
const char *p = "System Information Program";
char ver[128];
 
void print_menu()
{
	printf("\tSysinfo. %s\n\n", p);
 
	printf("1 - Kernel Information.\n2 - Memory & processes.\n");
	printf("3 - CDROM Information.\n4 - View /proc information.\n");
	printf("5 - View Real Time Clock Information.\n");
	printf("6 - View Input Devices information.\n");
 
	sprintf (ver, SYSINFO, VERSION / 100, VERSION % 100);
 
	printf("\n%s, by Bejiitas_Wrath 2007.\nsysinfo comes with ", ver);
	printf("ABSOLUTELY NO WARRANTY. This\n is open source software,");
	printf("and you are welcome to \nredistribute it under certain ");
	printf("conditions as \noutlined in the GNU Public License.\n\n");
	exit(0);
}

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.