It really is that easy!

Crap: for  i=5;i--;  C: for (i=5;i--;){

Getting Started with C Programming

C programming is a fun hobby as well as an occupation. Want to know a strange old trick to learn C? Compile, modify, and test short programs like these. We have developed many small, free and open source C programs, utilities, and some rather obfuscated code over the years this way. A fancy, expensive integrated development environment (IDE) is not needed. Something like the free and open source SciTE text editor and the Gnu C Compiler (GCC) will suffice.

Crap

Crap started out as A Nice Code Helper to replace missing brackets {} and semicolons in C, Java, JavaScript, and PHP programs; it now replaces so many missing brackets and semicolons that it just about qualifies as a language of its own. Exactly like C, but with a VB or Python-like feel, Crap is intended to save programming time and effort, but of course our software comes with absolutely no warrantee.

We use Crap to develop everything from simple scripts to full-blown applications that compile and run with the full power and speed of C.

Listlib

The C standard library is not well-equipped for handling linked lists. Listlib is a small, object-oriented linked list library I wrote as part of my TML project, the program that manages this web site. I used Listlib to create a stack to store the nested tag values and file names for backlinks. Listlib handles multiple lists and has methods to split strings into lists, sort, reverse, get, push, pop, etc.

The library is one monolithic, polymorphic function cleverly named, "list". The only thing to remember is that POP pulls memory from an allocated structure, with the consequence that we have to explicitly FREE anything we POP (see example below). To use Listlib, put #include "listlib.h" at the top of the file. Use the enum values, PRINT, DESTROY, etc. to access the various methods. For help, puts (list(NULL,HELP));

Listlib is released under the terms of the GNU Public license.

#include "listlib.h"
int main (void) {
    LinkedList myList;
    char *tmp, s[] = "I like my cheese";
    myList = list (NULL,SPLIT,s," ");
    printf ("Move '%s' to the beginning:\n",
        tmp = (char*)list(myList,POP,2));
    myList = list (myList,PREPEND,tmp);
    FREE(tmp); /* POP must be FREE'd */
    list (myList,PRINT,1);
    list (myList,DESTROY);
}
/* output:
Split: I like my cheese
Move 'my' to the beginning:
0. my
1. I
2. like
3. cheese
*/

Twitter Art

I wrote several Anchor C programs to make blocky text graphics.

▐▀▜─▛▀▘▀▛▘▌▐▖▞▐▀▀▐▀▀▝▜▀─▗▛█▖─▟█▖
▐▀▌─▛▀─▐──▌▌▙▘▐▀▘▐▀▘─▌──██▘─▐▙▙█
▌─▌▐▄▄─▐──█─▛─▙▄▖▙▄▖─▌──▝██▘▐▜▜▜

I wrote a JavaScript application that does pretty much the same thing and a whole lot more via the web. You can steal that one and place the code in your web site to give your visitors something to doodle with.


Public Domain

The following obfuscated code snippets are public domain.

Trig or Treat

Visit the Trig or Treat page for more information on this.

#include <math.h>
int main(i,r,c){
for(r=0;r<24;r++){
c=(int)(sinf(acosf(r/12.0-1))*24);
if(r==23)printf("Happy Halloween");else
for(i=0;i<24-c;i++)printf(" ");
for(i=0;i<2*c;i++)printf("%c",
((r>4&&r<8&&(i>6&&i<12||i>26&&i<32))||
(r>12&&r<21&&i>7&&i<2*c-8))?' ':'@');
printf("\n");}}

Dude

A classic abuse of #defines, this program illustrates the versatility of C. In a sense programming is a form of poetry. May it keep the holidays fizzing and buzzing along.

//file: dude.h
#include <stdio.h>
#define like (!(
#define beers )){
#define bought !=
#define hey  int
#define dude ((
#define allright main (void
#define when for(
#define spilled  =
#define man ;
#define rock ++
#define say printf(
#define drank %
#define ok );}
#define mugs )&&(
#define lol );
#define so ){
#define omg }
#define wtf return 0


//~ Output:
//~ 1
//~ 2
//~ Fizz
//~ 4
//~ Buzz
//~ Fizz
//~ 7
//~ 8

//file: dude.c
//~ #include "dude.h"

hey you man
hey allright so
when you spilled 1 man you bought 101 man you rock so
if like you drank 3 beers say "Fizz" ok
if like you drank 5 beers say "Buzz" ok
if dude you drank 3 mugs you drank 5 beers
say "%i", you lol
omg say "\n" lol
omg wtf man
omg

Algebra

This program solves simple linear equations such that y=2x+4 becomes x=(y-4)/2. This is just simple string substitution. For something more robust use mathomatic. I am releasing this program as public domain.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define F(f) strtod(f,NULL)
int main(int c,char**v){char *f,o,*r,*m;
    c>1&&(m=strrchr(f=v[1],'='))&&(*m++='\0'),
    (r=strchr(m,'x'))?*r++='\0',o=*r++^6:0;
    o=='-'||o=='+'?printf("x=(%s%c%s)/%s\n",f,o,r,m)&&
    printf("I will guess the answer is %g\n",
        (F(f)+((o=='+')?1:-1)*F(r))/F(m))
    &&0:puts("Solve for x in the form: y=2x+4");
    return c^2;
}

FileSize

Sometimes it can be handy to find out the size of a file. This program is public domain.

#include <stdio.h>
int main (int c,char**v){
    FILE *f;
    return --c&&(f=fopen(v[c],"r"))&&!fseek(f,0L,SEEK_END)&&
    printf("%s %li\n",v[c],ftell(f))&&!fclose(f)&&main(c,v);
}

Binspeak

Encode or decode a text message into a string of binary numbers, so you can speak in secret code. "Hi" becomes: 01001000 01101001. I am releasing this program as public domain.

#include <stdio.h>
#include <time.h>
void printbin (char *cStr){
    int x = 0, i = 0;
    if (cStr[i] < '2'){
        for (; cStr[i]; x |= (cStr[i++] - '0')){
            x <<= 1;
        }
        printf ("%c", x);
    }
    else{
        while (cStr[i]){
            for (x = 128; x; x >>= 1){
                printf ("%i", !!(cStr[i] & x));
            }
            i++;
            printf (" ");
        }
        printf ("00001010\n");
}   }
int main (void){
    char cStr[99];
    while (fscanf(stdin, "%98s", cStr) != EOF){
        printbin (cStr);
    }
    return 0;
}

CCBY Copyright © 2024 Henry Kroll III, thenerdshow.com This web page is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.