Skip to content

Instantly share code, notes, and snippets.

@b-adams
Created July 30, 2012 19:37
Show Gist options
  • Save b-adams/3209471 to your computer and use it in GitHub Desktop.
Save b-adams/3209471 to your computer and use it in GitHub Desktop.
CS225 Day 12 Notes
//Day 12 C routines
#include <stdio.h>
void routineA();
void routineB();
void routineC();
void routineD();
int main()
{
printf("\n---------\nRoutine A\n---------\n");
routineA();
printf("\n---------\nRoutine B\n---------\n");
routineB();
printf("\n---------\nRoutine C\n---------\n");
routineC();
printf("\n---------\nRoutine D\n---------\n");
routineD();
}
#include "routineA.c"
#include "routineB.c"
#include "routineC.c"
#include "routineD.c"
// - - - - - - - - - - - - - - - - -
// Routine A, Sizes of C integers
// - - - - - - - - - - - - - - - - -
// climits (cf. cs131 textbook (Hennefeld, Baker, Burchard) p. 51)
// defines constants LONG_MAX, LONG_MIN,
// INT_MAX, INT_MIN, SHRT_MAX, SHRT_MIN, CHAR_MAX, CHAR_MIN
// and the same for unsigned, ULONG_MAX, etc. (No MIN's for unsigned. Why not?)
// sizeof is a built in function that tells the storage space size of any variable or constant.
#include <limits.h>
void routineA()
{
printf("Min. char: %d\n", CHAR_MIN);
printf("Min. long: %ld\n", LONG_MIN);
printf("Max. long: %ld\n", LONG_MAX);
long a = 66;
printf("a(long) contains %ld\n", a);
printf("%ld byte(s)\n", sizeof a);
printf("long is %ld bytes\n", sizeof(long));
}
// - - - - - - - - - - - - - - - - -
//Routine B, Investigate unsigned integers in C, with some alternative number entries
// - - - - - - - - - - - - - - - - -
void routineB()
{
int i = -1; // int is signed.
//Other integer types are short, long.
printf("%d\n", i);
unsigned int u;
//u will be the biggest unsigned value, 1 less than the # of possibles.
u = i; // type "casting" takes place here, without notice
printf("%u biggest unsigned INT\n", u);
//The u is for unsigned. See http://www.cplusplus.com/reference/clibrary/cstdio/printf/
unsigned int v;
v = 0xFFFF; // zero 0x marks hex input
printf("%u=%x hex\n", v, v);
//The x is for hex output. See http://www.cplusplus.com/reference/clibrary/cstdio/printf/
unsigned int w;
w = 65536;
printf("%u squared = %u\n", w, w*w);
}
// - - - - - - - - - - - - - - - - -
//Routine C, Powers of 129
// - - - - - - - - - - - - - - - - -
void routineC()
{
int i = 1; // int (without modifiers) is signed.
unsigned int u;
u = 1;
printf("%d is signed \t %u is unsigned \n", i, u);
for (int loop =1; loop <=20; loop++)
{
i=i*129;
u=u*129;
printf("%d is signed \t %u is unsigned \n", i, u);
}
}
// - - - - - - - - - - - - - - - - -
//Routine D . Using C bitwise & logical operations
// - - - - - - - - - - - - - - - - -
void routineD()
{
short numA=1, numB=3, numC, numD; // 0000 0001, 0000 0011
printf("%d\t%d\n", numA, numB);
// Example of the bitwise-AND operator &
numC = numA & numB; // numC is now 1
numD = numA && numB; // this shouldn't be legal, Logical op
printf("%d\t%d\n", numC, numD);
// bitwise OR (inclusive) |
numC = numA | numB; // numC is now 3
numD = numA || numB; // this shouldn't be legal, Logical op
printf("%d\t%d\n", numC, numD);
// bitwise OR (exclusive) ^
numC = numA ^ numB; // numC is now 2
printf("%d\n", numC);
// bitwise NOT (1's complement) ~
numC = ~numB; // numC is now -4, 1111 1100
numD = !numB; // numC is Logical Not of 0000 0011. What?
printf("%d\t%d\n", numC, numD);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment