Skip to content

Instantly share code, notes, and snippets.

@matthewpalmer
Last active August 29, 2015 13:57
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/9734724 to your computer and use it in GitHub Desktop.
Save matthewpalmer/9734724 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>
#include <limits.h>
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);
void unitTests(void);
int maxInt(int startingNumber);
long maxLong(long startingNumber);
long maxULong(unsigned long startingNumber);
int main(int argc, const char * argv[]) {
unitTests();
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;
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 = SCHAR_MAX;
sl = LONG_MAX;
si = INT_MAX;
sll = LLONG_MAX;
ss = SHRT_MAX;
l = LONG_MAX;
i = INT_MAX;
ll = LLONG_MAX;
s = SHRT_MAX;
ul = ULONG_MAX;
ui = UINT_MAX;
f = FLT_MAX;
d = DBL_MAX;
// Print the maximums
printf("MAX char %d\n", c);
printf("MAX ul %lu\n", ul);
printf("MAX ui %d\n", ui);
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);
printf("MAX f %f\n", f);
printf("MAX d %f\n", d);
// Increase everything by one
c++;
sl++;
si++;
sll++;
ss++;
l++;
i++;
ll++;
s++;
ul++;
ui++;
f++;
d++;
// Print the increased values
printf("The maximum plus one of char is %d\n", c);
printf("The maximum plus one of ul is %lu\n", ul);
printf("The maximum plus one of ui is %d\n", ui);
printf("The maximum plus one of us is %d\n", us);
printf("The maximum plus one of sl is %ld\n", sl);
printf("The maximum plus one of si is %d\n", si);
printf("The maximum plus one of sll is %lld\n", sll);
printf("The maximum plus one of ss is %d\n", ss);
printf("The maximum plus one of l is %ld\n", l);
printf("The maximum plus one of i is %d\n", i);
printf("The maximum plus one of ll is %lld\n", ll);
printf("The maximum plus one of s is %d\n", s);
printf("The maximum plus one of f is %f\n", f);
printf("The maximum plus one of d is %f\n", d);
}
// These functions actually calculate the maximum, not just read
// it from limits.h
// startingNumber can be any value less than the actual integer max, but we use
// the actual max - 5 for testing because it makes it much faster to compute.
int maxInt(int startingNumber) {
// This breaks if the inputNumber is negative or zero.
assert(startingNumber > 0);
while (startingNumber > 0) {
startingNumber++;
}
startingNumber--;
return startingNumber;
}
// startingNumber can be any value less than the actual integer max, but we use
// the actual max - 5 for testing because it makes it much faster to compute.
long maxLong(long startingNumber) {
// This breaks if the inputNumber is negative or zero.
assert(startingNumber > 0);
while (startingNumber > 0) {
startingNumber++;
}
startingNumber--;
return startingNumber;
}
// startingNumber can be any value less than the actual integer max, but we use
// the actual max - 5 for testing because it makes it much faster to compute.
long maxULong(unsigned long startingNumber) {
// This breaks if the inputNumber is negative or zero.
assert(startingNumber > 0);
while (startingNumber > 0) {
startingNumber++;
}
startingNumber--;
return startingNumber;
}
void unitTests(void) {
// MAX - 5 used because it makes the tests run faster
assert(maxInt(INT_MAX - 5) == INT_MAX);
assert(maxLong(LONG_MAX - 5) == LONG_MAX);
assert(maxULong(ULONG_MAX - 5) == ULONG_MAX);
printf("All tests passed.\n");
}
long add (int x, int y) {
long answer;
answer = x + y;
return answer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment