Skip to content

Instantly share code, notes, and snippets.

@matthewpalmer
Created March 23, 2014 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewpalmer/9717866 to your computer and use it in GitHub Desktop.
Save matthewpalmer/9717866 to your computer and use it in GitHub Desktop.
//
// main.c
// memory
//
// Created by Richard Buckland on 20/11/12.
//
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <assert.h>
#define TRUE 1
#define FALSE 0
// Max values
#define MAX_CHAR 127
#define MAX_SL 2147483647
#define MAX_SI 32767
#define MAX_SLL 9223372036854775807
#define MAX_SS 32767
#define MAX_L 2147483647
#define MAX_I 32767
#define MAX_LL 9223372036854775807
#define MAX_S 32767
#define MAX_US 65535
#define MAX_UI 65535
#define MAX_UL 4294967295
#define MAX_ULL 18446744073709551614
long add (int x, int y);
void printSizes (char c, unsigned long ul, unsigned int ui,
unsigned long long ull, unsigned short us,
signed long sl, signed int si,
signed long long sll, signed short ss,
long l, int i, long long ll, short s, float f,
double d);
void printLocations (char c, unsigned long ul, unsigned int ui,
unsigned long long ull, unsigned short us,
signed long sl, signed int si,
signed long long sll, signed short ss,
long l, int i, long long ll, short s, float f,
double d);
void printMaxValues (char c, unsigned long ul, unsigned int ui,
unsigned long long ull, unsigned short us,
signed long sl, signed int si,
signed long long sll, signed short ss,
long l, int i, long long ll, short s, float f,
double d);
unsigned long long calculateMax(int isSigned, int size);
void unitTests(void);
unsigned long long power(int base, int exponent);
int main(int argc, const char * argv[]) {
printf("%f\n", FLT_MAX);
int x;
int y;
long total;
x = 40;
y = 2;
total = add (x, y);
printf("the sum of %d and %d is %ld\n", x, y, total);
char c = 'a';
unsigned long ul = 0;
unsigned int ui = 1;
unsigned long long ull = 2;
unsigned short us = 3;
signed long sl = 4;
signed int si = 5;
signed long long sll = 6;
signed short ss = 7;
long l = 8;
int i = 9;
long long ll = 10;
short s = 11;
float f = 3.1;
double d = 3.14;
// unitTests();
printSizes (c, ul, ui, ull, us, sl, si,
sll, ss, l, i, ll, s, f, d);
printLocations (c, ul, ui, ull, us, sl, si, sll, ss,
l, i, ll, s, f, d);
printMaxValues (c, ul, ui, ull, us, sl, si, sll, ss,
l, i, ll, s, f, d);
// add them all together just to make use of them so the compiler
// doesn't grumble that they are unused in the program
double grandTotal;
grandTotal = c +
ul + ui + ull + us +
sl + si + sll + ss +
l + i + ll + s +
f + d;
printf ("all these things added together make %f\n", grandTotal);
return EXIT_SUCCESS;
}
void printLocations (char c, unsigned long ul, unsigned int ui,
unsigned long long ull, unsigned short us,
signed long sl, signed int si,
signed long long sll, signed short ss,
long l, int i, long long ll, short s, float f,
double d) {
// Location of variables
printf ("location of char c %p\n", &c);
printf ("location of double d %p\n", &d);
printf ("location of float f %p\n", &f);
printf ("location of short s %p\n", &s);
printf ("location of long long ll %p\n", &ll);
printf ("location of int li %p\n", &i);
printf ("location of long l %p\n", &l);
printf ("location of signed long long sll %p\n", &sll);
printf ("location of signed short ss %p\n", &ss);
printf ("location of signed long sl %p\n", &sl);
printf ("location of unsigned long ul %p\n", &ul);
printf ("location of unsigned short us %p\n", &us);
}
void printSizes (char c, unsigned long ul, unsigned int ui,
unsigned long long ull, unsigned short us,
signed long sl, signed int si,
signed long long sll, signed short ss,
long l, int i, long long ll, short s, float f,
double d) {
// Size of types
printf ("size of char %lu \n", sizeof (c));
printf ("size of unsigned long %lu \n", sizeof (ul));
printf ("size of unsigned int %lu \n", sizeof (ui));
printf ("size of unsigned long long %lu \n", sizeof (ull));
printf ("size of unsigned short %lu \n", sizeof (us));
printf ("size of signed long %lu \n", sizeof (sl));
printf ("size of signed int %lu \n", sizeof (si));
printf ("size of signed long long %lu \n", sizeof (sll));
printf ("size of signed short %lu \n", sizeof (ss));
printf ("size of long %lu \n", sizeof (l));
printf ("size of int %lu \n", sizeof (i));
printf ("size of long long %lu \n", sizeof (ll));
printf ("size of short %lu \n", sizeof (s));
printf ("size of float %lu \n", sizeof (f));
printf ("size of double %lu \n", sizeof (d));
}
void printMaxValues (char c, unsigned long ul, unsigned int ui,
unsigned long long ull, unsigned short us,
signed long sl, signed int si,
signed long long sll, signed short ss,
long l, int i, long long ll, short s, float f,
double d) {
c = MAX_CHAR;
sl = MAX_SL;
si = MAX_SI;
sll = MAX_SLL;
ss = MAX_SS;
l = MAX_L;
i = MAX_I;
ll = MAX_LL;
s = MAX_S;
ul = MAX_UL;
ui = MAX_UI;
// Print the maximums
printf("MAX char %d\n", c);
printf("MAX ul %lu\n", ul);
printf("MAX ui %d\n", ui);
// printf("MAX ull %llu \n", ull);
printf("MAX us %d\n", us);
printf("MAX sl %ld\n", sl);
printf("MAX si %d\n", si);
printf("MAX sll %lld\n", sll);
printf("MAX ss %d\n", ss);
printf("MAX l %ld\n", l);
printf("MAX i %d\n", i);
printf("MAX ll %lld\n", ll);
printf("MAX s %d\n", s);
// Increase everything by one
c = MAX_CHAR + 1;
sl = MAX_SL + 1;
si = MAX_SI + 1;
sll = MAX_SLL + 1;
ss = MAX_SS + 1;
l = MAX_L + 1;
i = MAX_I + 1;
ll = MAX_LL + 1;
s = MAX_S + 1;
ul = MAX_UL + 1;
ui = MAX_UI + 1;
// Print the increased values
printf("MAX plus one char %d\n", c);
printf("MAX plus one ul %lu\n", ul);
printf("MAX plus one ui %d\n", ui);
printf("MAX plus one us %d\n", us);
printf("MAX plus one sl %ld\n", sl);
printf("MAX plus one si %d\n", si);
printf("MAX plus one sll %lld\n", sll);
printf("MAX plus one ss %d\n", ss);
printf("MAX plus one l %ld\n", l);
printf("MAX plus one i %d\n", i);
printf("MAX plus one ll %lld\n", ll);
printf("MAX plus one s %d\n", s);
}
// Note: this function doesn't actually work, because I
// couldn't work out what the relationship between
// size and max value was.
unsigned long long calculateMax(int isSigned, int size) {
unsigned long long max;
int numberOfBits;
int sizeCopy;
sizeCopy = size;
printf("SIZE %d\n", size);
numberOfBits = 4 * size;
if (isSigned == TRUE) {
max = power(2, numberOfBits - 1) - 1;
} else {
max = power(2, 4 * size) - 1;
}
printf("MAX %llu\n", max);
return max;
}
// Does calculations for the power of a number,
// e.g. 2^3 = 8
unsigned long long power(int base, int exponent) {
unsigned long long result = base;
while (exponent > 1) {
result = base * result;
printf("result %llu\n", result);
exponent--;
}
return result;
}
long add (int x, int y) {
long answer;
answer = x + y;
return answer;
}
void unitTests(void) {
assert(power(2, 4) == 16);
assert(power(2, 16) == 65536);
char c = 'a';
unsigned long ul = 0;
unsigned int ui = 1;
unsigned long long ull = 2;
unsigned short us = 3;
signed long sl = 4;
signed int si = 5;
signed long long sll = 6;
signed short ss = 7;
long l = 8;
int i = 9;
long long ll = 10;
short s = 11;
float f = 3.1;
double d = 3.14;
int charSize = sizeof(c);
int ulSize = sizeof(ul);
int uiSize = sizeof(ui);
int ullSize = sizeof(ull);
int usSize = sizeof(us);
int slSize = sizeof(sl);
int siSize = sizeof(si);
int sllSize = sizeof(sll);
int ssSize = sizeof(ss);
int lSize = sizeof(l);
int iSize = sizeof(i);
int llSize = sizeof(ll);
int sSize = sizeof(s);
int fSize = sizeof(f);
int dSize = sizeof(d);
assert(calculateMax(TRUE, charSize) == MAX_CHAR);
assert(calculateMax(TRUE, slSize) == MAX_SL);
assert(calculateMax(TRUE, siSize) == MAX_SI);
assert(calculateMax(TRUE, sllSize) == MAX_SLL);
assert(calculateMax(TRUE, ssSize) == MAX_SS);
assert(calculateMax(TRUE, lSize) == MAX_L);
assert(calculateMax(TRUE, iSize) == MAX_I);
assert(calculateMax(TRUE, llSize) == MAX_LL);
assert(calculateMax(TRUE, sSize) == MAX_S);
assert(calculateMax(FALSE, usSize) == MAX_US);
assert(calculateMax(FALSE, uiSize) == MAX_UI);
assert(calculateMax(FALSE, ulSize) == MAX_UL);
// assert(calculateMax(FALSE, ullSize) == MAX_ULL);
// assert(calculateMax(TRUE, fSize) == ;
// assert(calculateMax(TRUE, dSize) == ;
printf("All tests passed.\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment