Table of Contents

cprog Keywords

LIST FOR C/C++ KEYWORDS

The following is a list of the major topics being covered in this course:

DONE: Standard I/O (STDIO, STDOUT, STDERR)
DONE: Header Files (Local and System), C Standard Library (Libc), Libraries
DONE: arithmetic (equations, operators)
DONE: logic and operators (and, or, not, xor)
DONE: Variables (types, ranges, sizes)
DONE: Scope (Block, Local, Global, File)
DONE: Pointers (address of, assignment, dereferencing)
DONE: Type Casting
DONE: Selection Structures (if, case/switch)
DONE: Repetition/Iteration Structures (for, while, do while)
DONE: Arrays (standard notation, pointer arithmetic, single-dimensional, multi-dimensional)
DONE: File Access (Read, Write, Append)
Structures (Declaration, Accessing Elements, Pointers to)
typedef, enum, union
DONE: Functions, Parameters (Pass by: Value, Address, Reference), Return Types, Recursion, Command-line arguments
DONE: Compiler, Preprocessor, Flags, Assembler, Linker, Multi-file programs (how to structure, how to compile)
DONE: I/O Streams (cin, cout, cerr, stream operators) [C++]
DONE: Namespaces [C++]
DONE: Type Casting Operators, Const-Volatility Specifiers (const, volatile) [C++]
DONE: Classes (Objects, Constructor, Destructor, Access Control, Public, Protected, Private, Friend, “this” pointer) [C++]
DONE: Inheritance (single, multiple Inheritance), Polymorphism/Virtual Functions, Abstract Base Class [C++]
DONE: Overloading (Functions, Operators) [C++]
Exception Handing (throw, try, catch) [C++]
DONE: Templates, STL (Standard Template Library) [C++]

1: Repetition/Iteration Structures

Definition

Repetition/Iteration Structures are basically the coding that is necessary if you want to repeat a process many times, or to count. If you wanted to input 500 values into an array, you'd have to use a repetitive structure (the alternative being typing 500 lines of code). Or let's say you want to run a process multiple times, but each time something is slightly different, you'd use a repetitive structure.

Demonstration

Demonstration of the chosen 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:

Using a for loop

 
//THIS PROGRAM WILL COUNT FROM 0-20
 
#include <stdio.h>
#include <stdlib.h>
 
 
int main()
{//START MAIN
 
        int count = 0;          //DECLARING AND INITIALIZING A VARIABLE FOR COUNTING
 
 
        for(count; count<=20; ++count)          //FOR LOOP TO COUNT FROM 0-20
        {
                printf("%u sheep.\n", count);   //FSTREAM # AND SHEEP
        }       //END FOR LOOP
 
        return (0);     //RETURN 0
}//END MAIN

Using a do while loop

#include <stdio.h>
 
int main()
{
 
        int count=0;
        do
        {
                printf("%u sheep.\n", count);
                ++count;
        }
        while(count<=20);
 
        return (0);
}

Using a while loop

#include <stdio.h>
 
int main()
{
        int count = 0;
 
        while(count<=20)
        {
                printf("%u sheep.\n", count);
                ++count;
        }
 
        return 0;
}

The outputs of all of these programs lead to the SAME THING, so I'm only putting it once.

lab46:~/src/cprog/MessingAround$ nano RepetitiveDoWhile.c
lab46:~/src/cprog/MessingAround$ gcc -o RepetitiveDoWhile RepetitiveDoWhile.c
lab46:~/src/cprog/MessingAround$ ./RepetitiveDoWhile
0 sheep.
1 sheep.
2 sheep.
3 sheep.
4 sheep.
5 sheep.
6 sheep.
7 sheep.
8 sheep.
9 sheep.
10 sheep.
11 sheep.
12 sheep.
13 sheep.
14 sheep.
15 sheep.
16 sheep.
17 sheep.
18 sheep.
19 sheep.
20 sheep.

2: Variables (types, ranges, sizes)

Definition

Variables are structures that can hold types of data that can be manipulated.

Demonstration

To define a variable you have to make a statement in the code.

[type][identifier] = [value];

The type is the type of variable - Char short int int long int long long int float double long double

All of these variables can also be signed or unsigned (whether or not half of the memory allocated to each variable will be used for negative values, or further positive values.)

The sizes and ranges of these variables can differ, as shown by the output of a program (Project 0) that prints it all.

An unsigned char is 1 bytes
The range of an unsigned char is 0 to 255
An unsigned char can store 255 unique values

An unsigned short int is 2 bytes
The range of an unsigned short int is 0 to 65535
An unsigned short int can store 65535 unique values

An unsigned int is 4 bytes
The range of an unsigned int is 0 to 4294967295
An unsigned int can store 4294967295 unique values

An unsigned long int is 8 bytes
The range of an unsigned long int is 0 to 18446744073709551615
An unsigned long int can store 18446744073709551615 unique values

An unsigned long long int is 8 bytes
The range of an unsigned long long int is 0 to 18446744073709551615
An unsigned long long int can store 18446744073709551615 unique values

A signed char is 1 bytes
The range of a signed char is -128 to 127
A signed char can store 256 unique values

A signed short int is 2 bytes
The range of a signed short int is -32768 to 32767
A signed short int can store 65536 unique values

A signed int is 4 bytes
The range of a signed int is -2147483648 to 2147483647
A signed int can store 4294967296 unique values

A signed long int is 8 bytes
The range of a signed long int is -9223372036854775807 to 9223372036854775806
A signed long can store 18446744073709551615 unique values

A signed long long int is 8 bytes
The range of a signed long long int is -9223372036854775807 to 9223372036854775806
A signed long long can store 18446744073709551615 unique values

3: Pointers (address of, assignment, dereferencing)

Definition

A pointer is basically a variable, that contains the address/location of another variable.

Demonstration

To assign a pointer, you denote it with;

*[variable declaration];

The asterisk denotes pointer. And you can have two, or even three to designate a double or triple pointer.

To dereference a pointer, you use an asterisk again.

I think of a pointer as 'going higher' and then dereferencing as 'going back down'.

So if you have a variable called pointer, which is a single pointer, that variable is one level up. To put something in it, you have to bring it down a level.

The plain variable is just the address of where the actual value is being stored.

So, given the following code and output, you can see that the normal variable you can just printf it normally. But when it comes to storing and printing to the pointer, you need to dereference it to take the pointer back down to the storage unit that it specifies.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        int *pointer, variable;
 
        *pointer = 9001;        //need  to dereference to store it
 
        variable = 1111;        //not a pointer so it's fine
 
        printf("\nVariable is %d\n", variable);
 
        printf("\nPointer is %d\n", pointer);   
//Since it is not dereferenced, it will print an address (garbage).
 
 
        printf("\n*Pointer (dereferenced) is %d\n", *pointer);
//Since it has been dereferenced, it will properly display the value.
 
        return (0);
 
}
lab46:~/src/cprog/MessingAround$ ./pointers

Variable is 1111

Pointer is -1584842064

*Pointer (dereferenced) is 9001

4: File Access (Read, Write, Append)

Definition

File Access is basically the term for when you read from, write to, or append a file. Basically the program can open a .txt file or a .data file or whatever, and then either read information from it (such as a list of numbers or something) or write information to it (such as results of some calculation), or append (just add onto what is already in the file).

Demonstration

The following program will take an input file, and then add 1 to every number in the input file, and write those results into a different file.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
 
        int number;
 
        FILE *in, *out;
//Defines two file variable pointers called in and out.
 
        in = fopen("numbers.txt", "r");
//Opening the input file (known as numbers.txt). The r denotes reading.
 
        out = fopen("output.txt", "w");
//Opening the output file (naming it output.txt). The w denotes writing.
 
        if(in == NULL)
        {
                printf("Error, need an input file numbers.txt");
                exit(1);
        }
//This block checks to make sure that a file gets thrown into the in variable
 
        fscanf(in, "%hhd", &number);
//That in at the beginning of the statement indicates that input file
 
        while(number != 17)
        {
                int newnumber=0;
 
                newnumber = number+1;
 
                fprintf(out, "%hhd\n", newnumber);
 
                fscanf(in, "%hhd", &number);
        }
 
        return (0);
}

5: Standard I/O (STDIO, STDOUT, STDERR)

Definition

Standard I/O is the Default Input, Output, and Error Output. Normally these are the keyboard, and the monitor.

Demonstration

When we use scanf(“%d”, &number); it inputs an integer value from the standard input, which is the keyboard.

If we use fscanf(in, “%d”, &number); it inputs an integer value from whatever is defined as 'in' (like a text file), so we changed the input.

When we use printf(“Hello”); it prints Hello to the standard output, which is the monitor.

If we use fprintf(out, “Hello”); it prints Hello to whatever output is defined by 'out' (for instance, a text file). So we changed the output.

The errors are outputted to the STDOUT typically, and we haven't covered redirecting them in C yet.

6. Arrays (standard notation, pointer arithmetic, single-dimensional, multi-dimensional)

Definition

An array is basically a bunch of different data values (of the same type) that fall under the same identifier. The identifier is basically a gigantic data block, that has been sliced into smaller blocks, each of which holds the different data values.

Demonstration

This program will use two 1-d arrays, and 1 2-d array. It will store 0-4 in the 1-d arrays, and in the 2-d array it shows examples of counting by increasing numbers in sets of 5. So, in the first one it counts forward by zeros. In the second set it counts forward by 1s. Third by 2s. Etc.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        int Array[5];
 //This defines an array that can hold 5 values
 
 
        int D_Array[5][5];
//This defines a 2-d array, so it holds 5 values, each of which have five values underneath (like a table of sorts).
 
        int *P_Array;
        P_Array = (int*)malloc(sizeof(int)*5);
//This defines a 5 slot array, but using pointer style arithmetic.
 
        int count = 0;
        int count2 = 0;
        int number = 0;
 
        for(count = 0; count < 5; ++count)
        {
                int number2 = 0;
 
                Array[count] = number;
 
                *(P_Array + count) = number;
 
                for(count2 = 0; count2 < 5; ++count2)
                {
 
                        D_Array[count][count2] = number*number2;
                        ++number2;
                }
                ++number;
        }
 
 
        printf("\nThe values in Array\n");
 
        for(count = 0; count < 5; ++count)
        {
                printf("%d ", Array[count]);
        }
 
        printf("\nThe values in P_Array\n");
 
        for(count = 0; count < 5; ++count)
        {
                printf("%d ", *(P_Array + count));
        }
 
        printf("\nThe values in D_Array\n");
 
        for(count = 0; count <5; ++count)
        {
                for(count2 = 0; count2 < 5; ++count2)
                {
                        printf("%d ", D_Array[count][count2]);
                }
 
                printf("\n");
        }
 
        printf("\n");
 
        return (0);
}
lab46:~/src/cprog/MessingAround$ vim arrays.c
lab46:~/src/cprog/MessingAround$ gcc -o arrays arrays.c
lab46:~/src/cprog/MessingAround$ ./arrays

The values in Array
0 1 2 3 4
The values in P_Array
0 1 2 3 4
The values in D_Array
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16

lab46:~/src/cprog/MessingAround$

7: Selection Structures (if, case/switch)

Definition

Selection structures are basically ways of checking. They allow you to check things within the program, and decide what to do based on the check. With more complex logic operating behind the checks, the more interactive and 'intelligent' the program becomes.

Demonstration

IF statements

The following program will take a number, and using if blocks determine IF the number is even or odd.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        int number = 0;
        int x;
 
        printf("Enter a number: ");
 
        scanf("%d", &number);
 
        x = number%2;
 
        if(x == 0)
        {
                printf("\n%d is even\n", number);
        }
        else
        {
                printf("\n%d is odd\n", number);
        }
        return (0);
}
lab46:~/src/cprog/MessingAround$ vim Selection.c
lab46:~/src/cprog/MessingAround$ gcc -o Selection Selection.c
lab46:~/src/cprog/MessingAround$ ./Selection
Enter a number: 2

2 is even
lab46:~/src/cprog/MessingAround$ ./Selection
Enter a number: 3

3 is odd
Switch Case
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        int number;
 
        printf("Enter a number: ");
        scanf("%d", &number);
 
        int x;
        x = number%2;
 
        switch (x)
        {
        case 0:
                printf("The number is even");
                break;
        case 1:
                printf("The number is odd");
                break;
        default:
                printf("The number is neither even nor odd");
                break;
        }
 
        printf("\n");
 
        return (0);
}
lab46:~/src/cprog/MessingAround$ gcc -o SwitchCase SwitchCase.c
lab46:~/src/cprog/MessingAround$ ./SwitchCase
Enter a number: 6
The number is even
lab46:~/src/cprog/MessingAround$ ./SwitchCase
Enter a number: 9
The number is odd
lab46:~/src/cprog/MessingAround$ 

8: Logic and Operators

Definition

Logic and operators allow you to make a program much more interactive and capable.

Demonstration

And can be used to ensure two values are both true.

Or can be used to ensure at least one value is true.

Not returns the opposite of a value , from true to false and vice versa.

XOR will return true if JUST ONE value is true.

cprog Objective

cprog Objective

Use pointers and variables to discover the indirect properties of data storage

Definition

Basically this means that we will figure out, and be able to utilize, pointers and variables for the sake of different methods and 'tricks' behind data storage.

Method

Probably paying attention when Matt Haas is talking about those things would be a great start. Defining these as keywords would help as well.

Measurement

I defined them both as keywords, and it did help me understand a lot more. I already knew variables for the most part, but all the stuff to do with pointers I was sketchy on and now I feel much more confident about it.

Analysis

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

I think I did great. Of course there is room for improvement, but I am no longer clueless. Not really, being forced to define them and give working examples of them (for me) works very well. Perhaps the course objective would make a little more sense if it went into a little bit more detail in what it means by 'indirect properties data storage'.