It really is that easy!

Anchor: 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.

Anchor

Anchor C 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, Anchor C is intended to save programming time and effort, but of course our software comes with absolutely no warrantee.

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

Tml

Tml is the souped-up time machine that manages our web site. It uses wiki syntax to build websites. After that it goes into temporal overdrive. If we talk about high voltage here and later write a page about it, Tml will go back in time and create a link to it. Sweet!

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. Here's one that converts a .pbm file into twitter art like this:

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

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

Here is the original Anchor C version to play with. It requires my Anchor pseudocode compiler to run.

#if 0 /* -*- coding: utf-8 -*- */
# This source is also a self-compiling bash script...
# requires http://anch.org/anchor.html (anchor)
anch -lm -run "$0" "[email protected]"
exit
#endif
/* Twit
 *
 * Copyright (C) 2012 by Henry Kroll III, www.thenerdshow.com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

//{ global vars
char *bl[]= "─","▖","▗","▄","▘","▌","▚","▙",
"▝","▞","▐","▟","▀","▛","▜","█",NULL
char ***grid
int rows=20
int cols=40
//}

//{ functions
int blockindex  char *p
    int i
    for  i=16;i--;
        if  !strcmp(bl[i],p)
            
            return i

    return 0
        
void plot  int r, int c
    int b,i=blockindex  grid[r/2][c/2]
    b=((int)pow(2,(c&1))*(3*(r&1)+1)) | i
    strcpy  grid[r/2][c/2],bl[b]
        
void printgrid  void
    int r,c
    for  r=rows/2;r--;
        for  c=0;c<cols/2;c++
            printf  "%s",grid[r][c]
            free  grid[r][c]
        
        printf  "\n"
        free  grid[r]
        
    free  grid

void gridinit  void
    int r,c
    grid = malloc  rows/2*sizeof grid+1
    for  r=0;r<=rows/2;r++
        grid[r] = malloc  cols/2*sizeof grid+1
        for  c=0;c<=cols/2;c++
            grid[r][c]=malloc  5
            strcpy  grid[r][c],bl[0]
            
//}

int main  int argc, char **argv
    FILE *f
    int r,c
    float a
    char buf[99]
    buf[0]=0
    if  argc > 1
        printf  "%s\n",argv[argc-1]
        if  !(f=fopen(argv[argc-1],"r"))
            exit  1
        while  fgets(buf,99,f)!=NULL
            if  sscanf(buf,"%i %i\n",&cols,&rows)==2
                break

        printf  "%i %i\n",rows,cols
        gridinit()
        for  r=rows;r--;
            for  c=0;c<cols;c++
                fgets  buf,2,f
                if  buf[0]=='\n'
                    fgets  buf,2,f
                //printf  "%s",buf
                if  buf[0]=='1'
                    plot  r,c
            
            //printf  "\n"
        
        fclose  f
    else
        printf  "Usage: ./twit.anch <image>.pbm\n\n"
        gridinit()
        for  a=0;a<6.28;a+=.01
            r=sin(a)*rows/2+rows/2
            c=cos(a)*cols/2+cols/2
            plot  r,c
    

    printgrid()
    return 0

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;
}

Selsort

A naive selection sort. This one also requires my Anchor pseudocode compiler to run. I am releasing this program as public domain.

#!/usr/local/bin/anch -run
#include <stdio.h>
#define MAX_STR 10

void swap  char *a, char *b
    char t[MAX_STR]
    strcpy  t, a
    strcpy  a, b
    strcpy  b, t

/* naive selection sort */
void selsort  char a[][MAX_STR]
    int i=0,j,least
    char t[MAX_STR]
    while  *a[i]
        j=i+1
        least=i
        while  *a[j]
            if  (strcmp(a[j],a[least])) < 0
                least=j
            j++
        if  least > i
            swap  a[i], a[least]
        i++

void prin  char a[][MAX_STR]
    int i=0
    while  *a[i]
        puts  a[i++]

int main  void
    char a[6][MAX_STR]= "rain gear", "boots", "nets",
        "skiff", "outboard", 0
    selsort  a
    prin  a
    return 0

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