16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C Programming – Basics<br />

main.c<br />

#include <br />

#include <br />

#include .calc.h.<br />

#define MAXOP 10<br />

main () {<br />

...}<br />

stack.c<br />

#include <br />

#include "calc.h"<br />

#define MAXVAL 100<br />

int sp=0;<br />

double val[MAXVAL];<br />

void push(double)<br />

double pop(void){<br />

...}<br />

getop.c<br />

#include <br />

#include <br />

#include "calc.h"<br />

int getop(char s[]){<br />

...}<br />

Variables, Structure & Initialisation<br />

Static variables remain in existence ra<strong>the</strong>r than coming and going each time <strong>the</strong> function is activated, i.e. permanent<br />

storage <strong>with</strong>in a single function.<br />

C is not a block structured language in <strong>the</strong> sense of Pascal, because functions may not be defined <strong>with</strong>in o<strong>the</strong>r<br />

functions.<br />

In <strong>the</strong> absence of explicit initialisation, external and static variables are guaranteed to be initialised to zero,<br />

automatic and register variables have undefined initial values.<br />

int days [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};<br />

char pattern[] = "auld"; /* this is equivalent to */<br />

char pattern[] = {'o', 'u','1','d','\0'};<br />

Recursion<br />

/* qsort: quick sort into ascending order */<br />

void qsort(int v[], int left, int right)<br />

{<br />

int i, last;<br />

void swap(int v[], int i, int j); /* you must implement it */<br />

if (left >= right) /* do nothing if array contains */<br />

return; /* fewer than two elements */<br />

swap(v, left, (left+right)/2); /* move partition elem */<br />

last = left;<br />

for (i=left+1; i | .-| ---> |echo\0|<br />

| .-| ---> |hello\0|<br />

| .-| ---> |world\0|<br />

| 0 |<br />

32

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!