Post

0x02 - writing a C program

Wrote the first C binary

0x02 - writing a C program

bin 0x02 of LiveOverflow’s binexp series
he asks to write a simple C program and compile it.
i did. it’s literally a main() with argv.

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>

int main(int argc, char *argv[]) {
    if(argc == 2) {
        printf("YO BUD %s :P \n", argv[1]);
    } else {
        fprintf(stderr, "Usage: %s <name>\n", argv[0]);
        return 1;
    }
    return 0;
}

image


breaking it down (my way)

  • argc → how many arguments (arg count)
  • argv[] → actual args (arg values)
  • argv[0] → the name of the binary itself
  • argv[1] → the string we pass

super basic stuff, but you gotta get it into muscle memory.


compile + run

1
2
gcc main.c -o main
./main lav
YO BUD lav :P

image

no input?

1
./main
Usage: ./main <name>

image


tl;dr

  • got back to C (yes i’ve written before in diploma but this hit diff)
  • wrote a basic argc/argv CLI prog
  • compiled with gcc, ran it, aliased it
  • now i’m comfy with the structure again

till the next leak :p
~ lav

This post is licensed under CC BY 4.0 by the author.