Page cover

C

Quick guide to the language C. Honestly I find C quite easy to pickup once I had some training in JavaScript, the syntaxes were hugely similar here and there.

As a budding ethical hacker, my goal would be to at least be able to read C for exploits (buffer overflow, memory allocation) in addition to scripting in python, bash and JavaScript.

C in exploit development

Most operating systems today including the Linux Kernel are implemented with C code. By learning C, you will be able to understand and visualize the inner workings of computer systems (like allocation and memory management), their architecture and the overall concepts that drive programming.

Exploit Writing and Development: C, the mother of all programming languages, is used massively in the security field; it helps with exploiting writing and development. C also helps penetration testers write programming scripts. Learning C will also help hackers get an overview of the structure of operating systems.

Basic Syntaxes

  • #include <stdio.h>: This line is needed to run the line of code that starts with printf.

  • int main(){ }: This is the starting point of the code. All the code inside the curly braces {} runs first.

  • // output a line: This is a comment. It is not a line of code but a message we can add to code to tell ourselves or others what the code does. When the code is run this line will be ignored.

  • printf("Hello World!");: This line of code prints, or outputs, the text “Hello World!” to the console. Printing text to the console is one way for a program to communicate with the user.

\n adds newline, \t adds a tab space within the string, /* <insert string> */ allows for a block comment over several lines

; semicolon is a MUST

#include <stdio.h>

int main() {
  // output a line
  printf("Hello World!\n");
}

A widely used C Compiler is gcc, which stands for GNU Compiler Collection.

Compiling

Need to run the below in the command line to compile code

  • gcc is how we run the compiler application.

  • helloWorld.c is the filename of our code to be compiled.

  • -o helloWorld is an optional but common addition to the command. It tells gcc to output the program executable under the name helloWorld. If this is left out, the executable file will be called a.out

Variables

The basic structure of variable creation (known as declaration) is type variable_name

The main data types in C: int, float, double, and char.

  • int : whole number

  • float : decimal (6 decimal places)

  • double : number with possible decimals (15 decimal places)

  • char : letter or number, only single char

you can create more than one variable of the same type at a time by listing them with commas between their names, like type variable1, variable_2, variable_3, variable4.

Replacing Variables

Setting Permanent Variables (Const)

These special types, also known as literals, prevent any changes during execution once the value is set at declaration.

Any basic data type in C, like those we have gone over, can be declared as a constant using the keyword const before the type. So instead of our template of type variable_name, it would be const type variable_name.

It is also a best practice to use all upper case letters when declaring a constant:

Conversion of float to int

Char Conversion

In the back-end, a char doesn’t store 'a', it stores the value representing that: 97 for lowercase and 65 for uppercase.

  1. Converting char to integer

  2. int back to char

Codecademy Grocery Store Project 1 - Variables

Grocery Store Part 2 - Operators

Operators

To be continued

Last updated