Table of Contents

Part 1

Entries

September 20, 2011

September 21, 2011

September 26, 2011

Today was a good day for many reasons. It seems like my C is coming together, though I admit I know there is still a long ways to go. The best part was with my structure pointer experiment which actually proved Joe O. wrong. A very difficult task to say the least. Today I am going to do my best to finish up my keywords and work on some objectives.

September 26, 2011(later)

Ok so the sysprog book is a bit rough(though not as rough as the discrete book) but I finally finished enough keywords for the month. Some of them I didnt really grasp, such as bit sets and bit masks. Others were a little easier for me(assuming I am right): inodes and data blocks. Hopefully some of this stuff will become easier to understand as we go on. Most things seem to becoming easier but a few still need work.

CPROG Topics

Keyword 1: Standard I/O

Standard I/O(standard input/output) Just as its name implies it deals with inputing information to the terminal, and inputing input from the keyboard

#include <stdio.h>
 
int main()
 
{
 
 
 
		int number;
 
 
 
		printf("Please enter a number: "); //this is standard output
 
		scanf("%d", number); //this is standard input
 
		fprintf(stderr, "this goes to STDERR\n"); //this is standard error(output)
 
 
 
	return 0;
 
}

Keyword 2: Header files

Libraries for the C/C++ language are actually made up of two parts. The header files that declare, and the actual library or “archive” that contains the definition. The library(“archive”) will be discussed later.

The Header files is “included” in a programmers code and allows for easy access to predefined functions. System header files are part of the C/C++ standard library. Local ones are ones defined by the programmers. Most beginning programmers use header files from the C/C++ standard library before they even know exactly what they are.

#include <stdio.h> 
// This is how you include a standard library header file(system) and is almost always used in some way
#include "myheader.h" 
// This is how you include a local header file defined by a user

Keyword 3: Arithmetic(operators and equations)

Arithmetic is defined as dealing with the manipulation of numbers. The basic necessity of arithmetic is operators. The basic operators are (+), (-), (*), (/), and the less known(%).

Equations are simply a combination of operators and numbers

#include <stdio.h>
 
int main()
{
    int a = 5, b = 6; 
	float c = 0;
 
 
    printf("a = 5, b = 6 \n");
    c = a + b; // equation using the (+) addition operator
    printf("a + b = %.2f\n", c);
 
	c = a - b; // equation using the (i) subraction operator
	printf("a - b = %.2f\n", c);
 
	c = a * b; // equation using the (*) multiplication operator
	printf("a * b = %.2f\n", c);
 
	c = (float) a / (float) b; // equation using the (/) division operator
	printf("a / b = %.2f\n", c);
 
	c = a % b; // equation using the (/) modular division operator.  
	printf("a %% b = %.2f\n", c);
 
 
    return(0);
}

Keyword 4: Bitwise Operators(and, or, not ,xor)

As defined by Practical C programming, 3rd edition, an Operator: is a symbol that represents an action to be performed.

Bit operators allow us to work on the individual bits of a data type. Thus far I have not seen them used too much but it is important to know what they are. It will also make it clear why the equivalent logical operators are symbolized the way they are.

resources

Keyword 5: Variables(types,ranges, sizes)

Variables are simply memory locations that are given names and can be assigned values. Variables are used to store different types of data in memory for future usage.

The two basic variable types are Numeric and character.

Here is a list of data types and their range. (this list assumes signed integers, you can add unsigned to the data type shifting the range, size stays the same)

short Numeric - Integer signed: -32768 to 32767 unsigned: 0 to 4294967295 int Numeric - Integer signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 long Numeric - Integer signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 float Numeric - Real signed: 1.2 X 10-38 to 3.4 X 1038 no unsigned double Numeric - Real signed: 2.2 X 10-308 to 1.8 X 10308 no unsigned char Character signed: -128 to 127 unsigned: 0 to 255

These values are all relative to the operating system, as is the same for the size. A char is typically one byte but that might not always be the case. Programmers should always use the sizeof() function to determine the size of the variable type they wish to use.

example:

#include <stdio.h>
 
int main()
{
     int a = sizeof(int),b = sizeof(short),c=sizeof(char):
 
     printf("int: %d\n", a);
     printf("int: %d\n", b);
     printf("int: %d\n", c);
 
 
 
     return 0
}

Keyword 6: Strings

From wikipedia: a string is a finite sequence of symbols that are chosen from a set or alphabet.

For example: this entire sentence could be considered a string or several strings being printed in a certain order. In the C++ language their is an actual string variable but the C language does not have one. C handles strings by simply using an array of characters(which is really what a string is: an array of characters). In the example code I will declare an array and set it equal to a word, this can be one with pointers as well and will be discussed in the future.

example:

#include <stdio.h>
 
int main()
{
	char string[6] = "Hello";  /* array of characters with six elements is declared and set equal to "Hello"
		quotes are not stored but needed to store multiple characters.  The six element is needed as the string
		terminator.  arrays will be gone into greater depth in the future. */
 
	printf("%s\n", string);  //this is printing the string(array of characters) to the screen
	printf("The string is: %s\n", string); //This is printing a literal string and then the stored string(array of characters)
 
        return 0;
}

Keyword 7: Pointers(address of, assignment, dereferencing)

Keyword 8: Version control

Keyword 9: Structures

Keyword 10:Scope( local, block, Global, File)

Scope of a variable is the portion of a program in which a variable is known

Keyword 11 Type Casting

Type casting is when you make a variable of one data type act like another type for a single operation. To cast a variable name simply use brackets() in front of the variable and include inside them the temporary variable type you wish to use. Here is some example code:

include <stdio.h>
 
int main()
{
        int variable = 97;// decarling a variable of int type and giving it value
        printf("%d\n", variable);// simply printing the value of variable as it is
        printf("%c\n", (char)variable);// casting variable as a character should give the ascii char value of 97
 
        return 0;
}

Here is the output:

lab46:~$ ./a.out 
97
a

I would just like to add that with printf() you actually do not need to cast in this particular case but it gets the point across.

Keyword 12: Arrays

Arrays were covered in Data structs.

DATA Topics

Keyword 1: Version control(checkout, commit, update, add, log)

Version control is, simply put, a repository of files. Any change made to the source file is tracked and can be reversed if necessary. Wikipedia is an example of version control, if some one makes an improper change to a page, the original information is not lost it is stored as the previous version therefore making it easy to be restored.

Very helpful resource.

Keyword 2: Pointers(address of, assignment, dereferencing)

Pointers are data types whose value refers to another value stored elsewhere. When you declare a variable that is a pointer you are simply storing an address in that variable that has the data you want. The address that is pointed to is often called the pointee.

Here is an experiment I did that deals with pointers.

Keyword 3: Structures

struct birthDate {
     int year, day;
     char month[10]; 
};  // the structure is now declared
struct birthDate Karls; //Now we have declared a variable called Karl witch is a structure of the birthDate type.
Karls.year = 1988; //we've set Karls birthyear to 1988(lies) 

resources

Keyword 4: Structure pointers

struct birthDate *Brad;  // we have declared a variable named Brad which POINTS to a structure  of the birthDate type
(*Brad).year = 1999;// this SHOULD work(in theory)
Brad->year =1999;// this WILL work and is known as the structure pointer

resources

Keyword 5: Arrays

Much like structures, arrays are used to manage a large quantity of data. The difference is: an array cannot mix data types. If you have multiple data types you will need to use an array for each. Arrays are declared the same way you declare many other data types except they have an index added to end signified by the []. For example:

void main()
{
 
     char array[10];  //I just declared a ten element array of the data type char
     array = "september";// now I have stored 9 characters within the array.
}

To access individual elements of the array you simply use the index, which typically starts at 0. array[0] = s, array[1] = e, array[2] = p. This is quite handy if you need to call one particular element of an array.

Keyword 6: Null pointer

A null pointer simply pointers “nowhere”. And uninitialized pointer can pointer virtually anywhere(but unknown), but a NULL pointer points nowhere meaning there is no object associated with the pointer. Setting a pointer equal to null is quite easy. Example:

{
 
     char *variable = NULL; // all done, yes it's THAT easy for me, it can be this easy for you too!
 
}

Keyword 7: Void pointer

A Void pointer is a way to deal with raw memory. Void basically means the absence of a data type. Void pointers also allow for passing a pointer to an unspecified type.

Keyword 8: Function pointers

Function pointers are an interesting concept and, at least for me, a difficult one to understand. I only recently learned of them so I am not sure how strong this entry is going to be. In simplest terms(Joe O. is going to hate this) a Function pointer is a pointer to a function; AKA: a pointer that points to the address where the function is at. They can be used when you are not sure which function you wish to call. As my understanding is limited I will simply link to a better written explanation of function pointers.

Function pointers

Keyword 9: Static allocation vs. dynamic alocation

static allocation is when memory is allocated before a program is executed(compile time)

Dynamic allocation is when memory is allocated as needed during run time.

Keyword 10: Memory allocation, malloc(),new

Malloc() is a function in C that allocates memory and returns the beginning address. new is a keyword for c++ and basically does the same thing malloc() does for c.

{
    int *variable;//declaring a pointer to an int
 
    variable = malloc(sizeof(int)); //allocating memory for the variable
 
}
 
{
    int *variable;//declaring pointer
 
    variable = new int;// and again allocating memory for the variable
 
}

Keyword 11: memory de-allocation free(),delete

free() and delete are the opposite of malloc() and new free() is a function in c that will de-allocate memory that was previously allocated, so that it can be used if needed. delete is a keyword in c++ that will do the same.

Keyword 12: Linked lists

Linked lists lists is a sequence of nodes and each node points to the next one i the sequence. Linked lists are nice when you do not know how big the sequence(list) is going to be and allows for virtually infinite growth.

SYSPROG Topics

Keyword 1: UNIX systems programming

Unix systems programming, in part, is the understanding the role of an OS as well as what it means to write programs that work directly with the operating system.

sources

first page of the sysprog book that I took off of Haas' desk.

Keyword 2: Kernel space

Kernel space is where the kernel, its extensions and many drivers run. The kernel is the core of the operating system.

Keyword 3: User space

User space is where everything outside of the kernel space is run(everything not related directly the operating system).

Keyword 4: File access(open, read, write, lseek, close

File access is how you access and manipulate files. To do so you need certain tools:

Keyword 5: file descriptors

A File descriptor is a connection created between a process and a file when the open system call is used.

Keyword 6:Buffering

Buffering is reading data in large sizes into memory from user space and then processing it in smaller pieces as the user needs it.

Keyword 7:System call

A System call is a request from a program for a service of the operating system(kernel).

Keyword 8:File types and file properties

Everything in unix is a file. There are several file types: Directories, regular files, and links. Each file has certain properties which can be seen in unix using ls -l(this will tell you the file type as well). The properties are: permissions, owner, group, size of the file, date of last modification and name of the file.

Keyword 9:Bitset and bit masks

A bitset is when a bit or group of bits are “set” to 1(true) via a bitmask. A bit mask is used for setting bits to 1 or 0, or to keep a certain section of bits.

Keyword 10:User ID's, Group ID's

The owner and group of a file are stored as numbers which are the User ID and Group ID. The superuser always has a value of 0, everyone else gets a non-negative integer.

Keyword 11:Filesystem structure, inodes and data blocks

The Unix Filesystem from a users viewpoint is a tree structure. It branches out with each directory and new file. An inode is a struct that stores the properties of each file. Data blocks are where the actual contents of files are kept. Blocks are the same size and one file can use as many as is needed.

Keyword 12:Directories

A directories is simply a file that contains a list of names of files along with a table of inode numbers for those file properties.

links in Unix are very similar to shortcuts in windows. It is like having multiple phonebook entries in several places. A link is simply a special file that points to another file.

HPC1 Topics

Keyword 1

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    return(0);
}

Keyword 2

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

If you want to demonstrate something on the command-line, you can do so as follows:

lab46:~$ cd src
lab46:~/src$ gcc -o hello hello.c
lab46:~/src$ ./hello
Hello, World!
lab46:~/src$ 

Keyword 3

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    return(0);
}

Keyword 4

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

If you want to demonstrate something on the command-line, you can do so as follows:

lab46:~$ cd src
lab46:~/src$ gcc -o hello hello.c
lab46:~/src$ ./hello
Hello, World!
lab46:~/src$ 

Keyword 5

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    return(0);
}

Keyword 6

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

If you want to demonstrate something on the command-line, you can do so as follows:

lab46:~$ cd src
lab46:~/src$ gcc -o hello hello.c
lab46:~/src$ ./hello
Hello, World!
lab46:~/src$ 

CPROG Objectives

Objective

Q&A

Arrays

What is an array? *An array is a set of consecutive memory locations used to store the same type of data.

What are arrays used for?

How do you declare an array?

Pointers

What is a pointer?

What are pointers used for?

How do you declare a pointer?

DATA Objectives

Objective 1

Some common applications for linked lists:

SYSPROG Objectives

Objective 1

Concurrency:

HPC1 Objectives

Objective 1

State the course objective; define what that objective entails.

Method

State the method you will use for measuring successful academic/intellectual achievement of this objective.

Measurement

Follow your method and obtain a measurement. Document the results here.

Analysis

Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.

Objective 2

State the course objective; define what that objective entails.

Method

State the method you will use for measuring successful academic/intellectual achievement of this objective.

Measurement

Follow your method and obtain a measurement. Document the results here.

Analysis

Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.

Objective 3

State the course objective; define what that objective entails.

Method

State the method you will use for measuring successful academic/intellectual achievement of this objective.

Measurement

Follow your method and obtain a measurement. Document the results here.

Analysis

Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.

Semester Experiments

Experiment 1

Question

Will C allow for unsigned floats?

When looking up information on the ranges of data types I noticed there was never an unsigned range for floats, so I thought I would look into it. I am going to copy simple code trying to define unsigned floats and then copy the outcome of my attempt to compile

#include <stdio.h>
 
int main()
{
	float a = 5.0;  // To show the compiler will have no issue declaring a float and setting it equal to a value
	unsigned float b = 10.0; // to show the compiler will treat 'unsigned' and 'float' as two separate keywords that do not belong together
							// this will be seen in the messages after attempting to compile
	printf("float = %f\n unsigned float = %u\n", a, b);  // this is to remove other warnings when compiling 
 
	return 0;
}
lairstation3:~/Desktop/experiments$ gcc -Wall canfloatsbeunsigned.c 
canfloatsbeunsigned.c: In function ‘main’:
canfloatsbeunsigned.c:6: error: both ‘unsigned’ and ‘float’ in declaration specifiers

As you can see it simply won't work, it does not compile. My research leads to a very simple answer. There is no equivalent machine code operations for the cpu to execute(the cpu is not designed to deal with unsigned floats). The url below has discussions on the subject.

Resources

http://stackoverflow.com/questions/512022/why-doesnt-c-have-unsigned-floats

Pointers, address of, value at)

Question

I have noticed that pointers are often times a hurdle for beginning programmers. A common question I hear is: what is the difference between * and &. Early on in the learning process many people do not know what “varName” is when “char * varName” is declared. I am going to attempt to remedy this.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
	char *varName;// I declared a variable called varName that is a pointer aka (*) to a char
	varName = malloc(sizeof(char));  //Malloc returns an address and sets varName = to it(sizes it as well)
	*varName = 'a';// I set *varName(the value AT varName) = 'a'
 
	printf("varName = 0x%x\n",(int) varName);  // "varName" is a pointer to a char, meaning whatever address pointer is at, has ANOTHER address stored there
	printf ("*varName(the value at pointer) = %c\n", *varName);// the address that is stored in varName has a value stored and this is how you get it
	printf("&varName(the address of) = 0x%x\n", (int) &varName);// "varName" is at an address and this is how you get it
 
 
	return 0;
}
lairstation3:~/Desktop/experiments$ ./a.out 
varName = 0x98c5008
*varName(the value at pointer) = a
&varName(the address of) = 0xbfa0b90c
lairstation3:~/Desktop/experiments$ 

Conclusions

Structure pointers

Recently I got into a debate involving structure pointers. This is to show exactly what a struct pointer is:

#include <stdio.h>
#include <stdlib.h>
 
struct bin {
                char *string; 
        };
int main()
{
        struct bin *joesWrong;// declaring a pointer to a struct of the type bin
 
        joesWrong = malloc(sizeof(struct bin)); //mallocing up the struct
 
        joesWrong->string = malloc(sizeof(char)*20);  //these next two lines mean the same
        (*joesWrong).string = malloc(sizeof(char)*20);// and will be proven  in the next block of code
 
 
 
        joesWrong->string = "karl's right\n"; // we will see that the following code works as it should
        printf("%s\n", joesWrong->string);  
        (*joesWrong).string = "Joe's wrong\n"; // this means that x->y and (*x).y mean the same thing
        printf("%s\n", (*joesWrong).string);
 
 
        return 0;
}

This is the output of the code, and as you will see it works as expected, thus showing exactly what a function pointer is. x.(*Y) != x→y

karl's right

Joe's wrong

lab46:~$ ^C
lab46:~$ 

This is a link for Joe :)