C

#include<stdio.h>

int main(void){
    printf("Hello, C!");
    return 0;
}

Table of Contents

  1. Why use C?
    1. Resources
  2. Installation
  3. Hello, World
  4. Pointers
  5. Unix System Calls
    1. Fork
    2. Pipe
    3. Exec

Why use C?

C is the backbone of Unix systems. That’s cool. Currently the only project I really have going in C is RML. The Operating Systems course uses C to teach about processes and threading.

Farily certain I just bombed an interview for IBM, so there’ll be a lot more added to this page in the coming weeks as I read Cracking the Coding Interview.

Resources

  1. The C Book
  2. C Elements of Style
  3. GNU C Programming Tutorial and Reference Manual
  4. Build your Own Lisp
  5. Writing Bug-Free C Code
  6. K&R C (Not a libre publication.)

Installation

To run C programs, it is best not to use an IDE. To get a feel for how things fit together, it is best to use the following tools:

Tool GNU/Linux Windows MacOS
Text Editor Gedit, Vim Notepad++ TextWrangler
Compiler gcc MinGW cc xcode cc

A debugger like GDB is also recommended.

Hello, World

Hello World is an excellent first example to demonstrate some key properties of C programs:

#include<stdio.h>

int main(int argc, char** argv){
  puts("Hello, World.");
	return 0;
}

Headers allow the use of functions from libraries.

#include<stdio.h>

The main function must be present in every .c file, and is the point where the program will begin to run.

int main(int argc, char** argv){

Puts is short for put string

puts("Hello, World.");

Return 0 indicates that no errors have occured, and that the program is finished running.

return 0;
}

C uses char*, a pointer to a list of characters with the final character being the null terminator.

puts("string"); is sufficient for most string-output.

printf("string %f", flt); - printf is required for inserting data into strings. Data can be represented in a variety of ways with % notation.

Pointers

C allows the manual storage and manipulation of memory addresses. Using the pointer (*) and address (&) operators, a programmer can create programs that minimize copying, with heavy emphasis on modifying arrays in the memory in-place. This lower, more complex level of control enables faster algorithms and interesting hacks.

// A pointer is the same size as the type declared.
int y = 32;
int * yptr;
yptr = &y;

// Run:
y -> 32
*yptr -> 32
yptr -> A3BC3AF8

Unix System Calls

Fork

The fork() call can be used to split a running program into seperate processes. Calling fork() returns the PID of the child to the parent, and 0 to the child.

Pipe

The pipe() call (which requires a int pipeinfo[2]; to point to,) creates a pipe that can be used to forward date to and from child or executed processes.

Exec

I’ve found the following very useful when I accidentally start a child process that I forget to kill:

ps -A | grep {name}
kill -9 {pid}

CC BY-SA 4.0 - This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. You are free to share, redistribute and adapt the material as long as appropriate credit is given, and your contributions are distributed under the same license.