Notes on the float and double data type in C

Synopsis

Values must contain a floating-point. You can omit digits before the decimal point or digits after the decimal point, but not both. Floats cannot be signed or unsigned. Meaning they are essentially signed by default.

[float|double] = <value>

float

float myFloat = 125.8f;
float myFloat2 = 3.0f;
float myFloat3 = -.001f;
printf("Value of myFloat is: %g \n", myFloat);
printf("Value of myFloat2 is: %g \n", myFloat2);
printf("Value of myFloat3 is: %g \n", myFloat3);

The reason you need to add the character f (or F) to the declaration is to explicitly tell the compiler to treat the variable as a float. As by default floating-point constants are taken as a double by the C compiler.

double

double myDouble = 125.8;
double myDouble2 = 3.;
double myDouble3 = -.001;
printf("Value of myDouble is: %g \n", myDouble);
printf("Value of myDouble2 is: %g \n", myDouble2);
printf("Value of myDouble3 is: %g \n", myDouble3);

Ranges

float and double cannot have range specifiers like long and short.

float is generally 4 bytes in size (32 bits wide).
double is generally 8 bytes in size (64 bits wide).

Notes on the int data type in C

Synopsis

Integers (signed and unsigned) only. No decimal places. No commas. If you want to gaurantee signed or unsigned you must explicitly define it.

[signed|unsigned] [range] = <value>

Signed integers

signed int myInt   = 12000;
signed int myInt2  = -999;
printf("Value of myInt is: %i", myInt);
printf("Value of myInt2 is: %i", myInt2);

For shorthand signed integers you can simply use int keyword.

int myInt   = 12000;
int myInt2  = -999;

Unsigned integers

unsigned int myInt3  = 12000;
printf("Value of myInt3 is: %u", myInt3);

For shorthand unsigned integers you can simply use unsigned keyword.

unsigned myInt3  = 12000;

Ranges

int is gauranteed to be at least 1 byte in size (8 bits wide). But is usually either 2 bytes (16 bits wide) or (as per most modern machines) 4 bytes (32 bits wide) in size

Test it:

printf("On this machine, int is stored in %lu bytes. (%lu bits wide).\n",
                sizeof(int),
                ((sizeof(int)) * 8));

short int is an integer value of reduced precision. For example, if the machine stores an int 32 bits wide, then short int will be 16 bits wide. A short int is guaranteed to never be wider than an int (although it may be the same).

Test it:

printf("On this machine, short is stored in %lu bytes. (%lu bits wide).\n",
                sizeof(short),
                ((sizeof(short)) * 8));

long int is an integer value of extended precision. Gauranteed to be at least 4 bytes in size (contain at least 32 bits of precision), and may or may not be larger than a standard int.

Test it:

printf("On this machine, long is stored in %lu bytes. (%lu bits wide).\n",
                sizeof(long),
                ((sizeof(long)) * 8));

long long int is an integer value of extra extended precision. Gauranteed to be at least 8 bytes in size (contain at least 64 bits of precision).

Test it:

printf("On this machine, long long is stored in %lu bytes. (%lu bits wide).\n",
                sizeof(long long),
                ((sizeof(long long)) * 8));

Ranges in real terms

8-bits wide (1 byte)

– Maximum signed range is from −128 to 127.
– Maximum unsigned range is from 0 to 255.

16-bits wide (2 bytes)

– Maximum signed range is from −32,768 to 32,767.
– Maximum unsigned range is from 0 to 65,535.

32-bits wide (4 bytes)

– Maximum signed range is from −2,147,483,648 to 2,147,483,647.
– Maximum unsigned range is from 0 to 4,294,967,295.

64-bits wide (8 bytes)

– Maximum signed range is from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
– Maximum unsigned range is from 0 to 18,446,744,073,709,551,615

There is more information on integer ranges here: http://en.wikipedia.org/wiki/Integer_(computer_science)

Good to know

Without explicit specification, the type of an integer constant defaults to the smallest possible type that can hold the constant’s value, unless the value is suffixed with an L , l , U , or u . The following list describes the type assignment of integer constants:

* If the constant has no suffix, and is given in decimal radix, it will have the first type from this list capable of storing the value: int , long int , unsigned long int .
* If the constant has no suffix, and is given in octal or hexadecimal radix, it will have the first type from this list capable of storing the value: int , unsigned int , long int , unsigned long int .
* If the constant has the U or u suffix, it will have the first type from this list capable of storing the value: unsigned int , unsigned long int .
* If the constant has the L or l suffix, it will have the first type from this list capable of storing the value: long int , unsigned long int .
* If the constant has both U and L suffixes (or the lowercase combination), it will have type unsigned long int .
* For example, the constant 59 is assigned the int data type by default, but the constant 59L is assigned the long data type. 59UL is typed as unsigned long int .

* from cs.auckland.ac.nz

Octal notation. (Base 8)

Integers starting with a 0 are treated as octal (base 8).

int myDecimal   = 16;
int myOctal     = 020; // 16 in decimal.
printf("Value of myOctal is: %o", myOctal); // Value of myOctal is: 50
printf("Value of myOctal is: %#o", myOctal); // Value of myOctal is: 050

Hexidecimal notation. (Base 16)

Integers starting with a 0x are treated as hexidecimal (base 16). In hexidecimal, symbols 0–9 represent values zero to nine, and A, B, C, D, E, F (or alternatively a–f) represent values ten to fifteen.

int myDecimal   = 15;
int myHex       = 0xF; // 15 in decimal.
printf("Value of myHex is: %x", myHex); // Value of myHex is: f
printf("Value of myHex is: %#x", myHex); // Value of myHex is: 0xf
printf("Value of myHex is: %X", myHex); // Value of myHex is: F
printf("Value of myHex is: %#X", myHex); // Value of myHex is: 0xF
printf("Value of myHex is: %i", myHex); // Value of myHex is: 15

The primary use of hexadecimal notation is a human-friendly representation of binary-coded values. Hexadecimal is also commonly used to represent computer memory addresses.

On some machines each hexidecimal code (e.g. 5 or D) takes up 4 bits (a nibble) as opposed to a decimal which takes up (at least) 8 bits.

Binary notation. (Base 2)

C (and C++) do not have native support for binary notation. You can only work with decimal (base 10), octal (base 8) or hexadecimal (base 16) integers as standard.

int myBinary = 0xF; // binary: 00001111

Some compliers (such as GCC) support binary notation using an 0b prefix.

int x = 0b00001111;

But as support isn’t widespread it’s safer to stick to base 10, 8 or 16.

More Information

http://en.wikipedia.org/wiki/C_data_types

Git and GitHub Notes

Initial Setup

$ git config –global user.name “Jon Matthews”
$ git config –global user.email joncarlmatthews@gmail.com
$ ssh-keygen -t rsa -C “joncarlmatthews@gmail.com”
$ cat ~/.ssh/id_rsa.pub

https://github.com/settings/ssh

How to create a branch

Create it locally:
$ git branch my-new-branch

Switch to the branch
$ git checkout my-new-branch

Push the branch back to the remote host
$ git push origin my-new-branch

How to delete a branch

To delete the local copy:
$ git branch -d my-old-branch

To delete the remote copy:
$ git push origin –delete my-old-branch

How to clone a specific branch

$ git clone -b my-branch git@github.com:user/myproject.git

How to checkout remote branch from within an existing clone

$ git fetch origin

See the available branches:
$ git branch -r

$ git checkout -b my-branch

How to switch to a local branch from within an existing clone

$ git checkout my-branch

How to restore an accidentally deleted file

$ git checkout path/to/my/deleted/file.html

How to create a new Branch from an existing Tag

$ git pull origin master
$ git fetch –all
$ git tag -l
$ git checkout -b myNewBranch myExistingTag
$ git push origin myNewBranch

How to merge a branch

Checkout the branch you wish to merge into
$ git checkout master

Merge with the branch you’re merging form.
$ git merge myOtherBranch

** manually fix any conflicts **

Push back
$ git commit -a -m’Merge with branch myOtherBranch’

$ git push origin master

How to create a tag

List current tags:
$ git tag -l

Create the tag
$ git tag -a v1.4.0 -m ‘my version 1.4.0’

List current tags:
$ git tag -l

Push the tag back to the remote server:
$ git push origin v1.4.0

How to clone a specific tag

Clone the repo:
$ git clone git@github.com:user/myproject.git

List the available tags:
$ git tag -l

Checkout the desired tag:
$ git checkout tags/<tagname>

Deploy from a tag

$ cd “${DEPLOY_TREE}”
$ git fetch –all
$ git checkout –force “${TARGET}”

from: http://grimoire.ca/git/stop-using-git-pull-to-deploy

How to ignore all local changes for a certain amount of time

$ git update-index –skip-worktree

To undo:
$ git update-index –no-skip-worktree