C, UNIX, & Compiling Basics

For a more detailed working code example click here.


Unix and Terminal

Note: Don't type the ''


List Current Directories

ls


Change Current Directory

cd 'directoryName'


//Back up one directory

cd ..


//Return to your home directory

cd


Create a New Directory

mkdir 'directoryName'



Copy, Move and Rename Files

//Copy file from current location to another

cp 'fileName' 'destination'


//Move a file

mv 'fileName' 'destination'


//Rename a file

mv 'fileName' 'newfileName'


It's highly suggested that you create a new directory for every lab and assignment.

For example a new Lab1 directory that stores lab 1 files.

We may not have multiple files for labs now but very soon we will.



Compiling and Submitting Assignments


Compiling & Running a Program With GCC


Note: The $ just means this is in a command line do not type it.


//Basic argument structure.

$
gcc 'fileName.c' -o 'outputFileName' 'flags'


//Note: Each arguement besides the first is optional.

//Examples to compile and run


$
gcc lab1.c

$
./a.out


//Or to rename the compiled executable use the -o flag


$
gcc lab1.c -o lab1

$
./lab1


//Or to compile multiple files


$
gcc lab1.c runner.c functions.c -o lab1

$
./lab1


I recomend you always compile showing warnings.

By default only errors are shown.

Warnings will compile but should always be delt with.

Clearing them will greatly help with logic or unseen syntax errors.


//To do so use the -Wall flag


$
gcc lab1.c -Wall

$
./a.out



Debugging Your Program


Debugging will help you pin point seg faults, variable logic and much more.


Go here to learn more about the GDB debugger.


Beginner to Intermediate C Programming


Basic Data Types

Note: Please never use x as a real life variable name.

(Unless your variable is actually an x,y coordinate or somthing)


Integers

Whole numbers ranging from -32767,32767

//Creates and assigns a new variable of interger data type.

int x = 734;


//Creates and assigns a new variable of long data type.

//Double the size in memory of standard int.

long x = 734734734;



Floating Point Data Types

Data types that support decimal point precision.

//Creates and assigns a new variable of float data type.

float x = 734.734;


//Creates and assigns a new variable of double data type.

//The exact memory of float and double vary ammong compilers.

//For this class float will almost exclusively be used.

//But double has double the memory limit of float.

double x = 734.734734734734;


Characters

Small whole numbers from 0,127 representing a character on the ASCII table.

//Creates and assigns a new variable of character data type.

char x = 'Z';



Strings (Arrays of Characters)

Strings are not a built in data type in C.

Thus we treat them as character arrays.

Note strings should end with a null terminator '\0'.

This is simply to let most functions know when you hit the end of the string which could be differnt than the end of the array.

//Creates and assigns a new character array assigned to the given string.

char* x = "Hello";


//Same as

char x[] = {'H', 'e', 'l', 'l', 'o', '\0'};



User Defined Data Types (Structures)

Defining new abstract data types from a combination of the basic data types

//Defines a new datatype called "Person"

typedef struct {

int age;

char name[20];

} Person;


//Create a new variable joe of type Person

Person joe;

joe.age = 22;

//Note you cannot directly reassign arrays.

//You will need to use the strcpy function.

//This works like you would think joe.name = "Joe" would.

strcpy(joe.name, "Joe");



Malloc & Malloc

Dynamically allocating new memory and assigning them to pointers

//Say we want a char array of size 5

int* numArray = malloc(sizeof(int) * 5);

//Note malloc takes in one parameter the size of desired bytes

//and it returns a pointer to the memory block allocated

//We can now treat numArray as a int array of 5.


Pointers

Pointers are simply memory locations of data.

You can think of a pointer as a short cut to a file.

You can change the value of the data without changing the pointer.

//Lets init a value and a pointer to it.

int x = 5;

//To "refrence" a values memory loaction use the & sign.

int* xPtr = &x;

//Now we could do things like pass xPtr into a function.

//Then we could change x ouside our current scope.

//To "derefrence" a pointer to get the value use the * sign.

*xPtr = 99;


//Recall arrays are just pointers to the first index.

int array[] = {1,2,3,4,5};

//So if we just derefrence "array" we get 1.

printf("%d", *array);

//Outputs: 1

Refer to the Code Example for more about functions, loops, pointers, ect.