Category Archives: C

Pointers

Pointers are a very powerful, dangerous and common constant. Here is a simple understanding of how they work with variables.

int x;

x=1;

int y=x;

This means y = 1 because it is the same value of x.

Another way of using pointers is by using a pointer deceleration which is like:

int ip; the ip stands for pointer to it

ip = &x, this assigns the address of x to the int value of ip rather then the value of x itself.

int y = ip; would look at the value of y which again is the same as x .

Increment i

Increment I is a common operator used in C and C++ and is used normally within the loop command.

It uses single elements or components, for example in our line of code:

for( int i=1; i<5; ++i) {

}

such as the number of i which is 1.

The ++ as with other operators returns a value, it does it by;

1) if ++ is before the variable, e.g ++i (i=1)… the value returned is the value in the I after it has been incremented.

2) if ++ is after the variable, e.g i++ is the value of i before it has been incremented.

Increment is an increase of some amount either fixed or variable

Loops

Loops are vital part of C++ as it can help in so many ways and quicken up certain actions.

A way to write a loop for C and C++ is as follows:

for (int i =1; i<5; ++i) {

}
This code is a simple loop that will make the command console output a loop for the int i.

So first of all the ‘for’ initiates the loop command and then everything for the loop is put between brackets ().

So we define are int within the loop so int i= 1;

Then we use the int i and say that the loop command is equal to or less then 5….. i<5;

This will make the loop work 5 times and no more

++i is a common usage is C and C++ and stands for increment i (I shall go over this next post)

Depending on your storage allocation the result would be for

Static: 1,2,3,4,5

Auto: 1,1,1,1,1

Registered: 1,1,1,1,1

Storage Classes 2

As I said there are four different storage classes, the next two are:

Registered storage space does the exact same as auto storage but it is just located within a registered location within the program to make it run faster.

External storage is the last of the four and one of the most flexible as it allows the user to bring in code externally form another project or source file. The way it works is say you have a file with this code in it:

int var = 47;

void func() {

printf(“this is a function”);

}

To call the void function to another file you would put:

extern void func(); – this calls the function into the new file and (this would go above the main block, just so it can be used throughout the project).

func(); – This would use the function, which in this case has the printf keyword within it.

REMEMBER TO ALWAYS CALL IN THE CODE BEFORE YOU USE IT.

As you can see there is a variable also in the code above which is not being externally called in. To do this you would:

extern int var; – To call in the variable function

printf(“the extern var is %d\n”, var);- This calls in the variable

The %d is writing the number tothe command console of the variable which is set at 47.

Storage Classes

The storage class in C is split into four different types of storage. The first two are static storage space and an auto storage space, these are the most common used for all simple code.

A storage class is a storage allocation for when, where, how the storage for a variable is allocated.

The auto storage is the default storage allocation and the allocated space it stores information in is temporarily space or (temp space).

Static storage is a space in which the information is stored within the same space, therefore the information is overwritten each time. An easy way to remember or understand this is:

Auto storage in a 5 times loop will produce 7,7,7,7,7

While Static would produce 7,8,9,10,11

Identifier scope

The identifier scope is the name given to the line of code that declares the variable, so:

char * s=(“hello world”\n); this is the identifier scope

It has to sit within the block that the variable will be used and before it is used.

Another way of using variables

So as you know as long as you identify a variable first it can be used in your script.

Another of of using a variable is:

char * s =(“hello world\n”);

printf (s);

This indicates that the character sting s is hello world and printf will read that and output it to the command console.

Stdin

#include <stdio.h>

enum { max_string = 127};

static char string [max_string + 1] = “”;

int main() {

printf(“Type a string: “);

fgets(string, max_string, stdin);

printf(“The string is %s\n”, string);

return 0;

}

As you can see in this code some lines are new and some already known. What the code does is allows the user to write a string onto the command program and then the program will use the input to write into another string.

enum or An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration tag and defines the set of named integer identifiers (called the “enumeration set,” “enumerator constants,” “enumerators,” or “members”). A variable with enumeration type stores one of the values of the enumeration set defined by that type.

This is easier to see as:

enum identifier { enumerator-list } which for our code allows the string to be 127 characters long
Static char allows the string to add in +1 string, as you can see at the end of that code =”” is saying that the static char has an empty string.

printf will will dsiplay its string onto the comman program

fgets, is a coomon way to get input from the input stream. Written like

fgets(string, max_string, stdin);

the first string is where the input will go

the max_string constant followed by the input string stdin (standard in), stdin can be changed to wherever it is you want to input the string.

The %s is a token used to tell the program to print the next argument \n is new line and the last bit of code ,string tells %s what to print.

What I mean by this is that whatever is written into the string %s will print it and create a new line.

http://msdn.microsoft.com/en-us/library/whbyts4t(v=vs.80).aspx

Stdout and Stderr

In C and C++ (Which is in the C++ Category) you will see a few different identifiers that allow the project to function correctly.

The first two I learned about were the Std (Standard) out and err streams, they both output a string to the command console but with one slight difference.

fprintf(stdout, “hello world\n”); // stdout strem comes out in black

fprintf(stderr, “hello world”); // err stream comes out in red on eclipse*

So as you can see here the code is a bit different, fprintf again send stings to the output stream the same as printf. The difference with it is fprintf however is that it allows you to output to any stream you want and tell it too.

The first line is telling fprintf to send the string to the stdout stream which is the standard output stream, while the second line is telling fprintf to send it to the stderr stream which is standard err stream. The differences inthe background are:

The stdout stream is buffered at the end of the statement or to the ;

The stderr stream is not buffered until of the program.

When you do this don’t worry what order the streams come out, you can sort that out if needed.

* May be a different colour for your IDE/Complier

Strings

Strings are arrays of chars. String literals are words surrounded by double quotation marks.

So in the language of C the string is outputted onto the command line and run on screen.

So to start with, if I want to write the string “hello world” it would look like this:

printf (“hello world\n”);

This Identifier followed by statement is a simple line of code that will operate in a C compiler. As you can see the is a \n and the end of the text, this indicates a new line character or to simply understand it, a new line.

The code altogether at this point should look like this:

#include

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

printf (“hello world\n”);

Return 0;

}

This would complie and run and bring up the string hello world to the Command console.