Contents [0/8] |
Example: Show Bytes [1/8] |
Example: Two's Complement [2/8] |
Example: Floating Point [3/8] |
Example: Functions [4/8] |
Example: Pass by Value [5/8] |
Example: Addresses and pointers [6/8] |
Example: The dereferencing operator [7/8] |
Example: Passing pointers as parameters [8/8] |
Example: Show Bytes [1/8] |
file:book-show-bytes.c [source]
00001: #include <stdio.h> 00002: typedef unsigned char *pointer; 00003: 00004: void show_bytes(pointer start, int len) 00005: { 00006: int i; 00007: for (i = 0; i < len; i++) { 00008: printf("0x%p\t0x%.2x\n", start+i, start[i]); 00009: } 00010: printf("\n"); 00011: } 00012: 00013: int main() 00014: { 00015: int a = 15213; 00016: printf("int a = 15213;\n"); 00017: show_bytes((pointer) &a, sizeof(int)); 00018: return 0; 00019: } 00020:
file:show-bytes.c [source]
00001: #include <stdio.h> 00002: typedef unsigned char *byte_pointer; 00003: 00004: void show_bytes(byte_pointer start, int len) { 00005: int i; 00006: for (i=0; i<len; i++) { 00007: printf("%.2x", start[i]); 00008: printf(" "); 00009: } 00010: } 00011: void show_int(int x) { show_bytes((byte_pointer) &x, sizeof(int)); } 00012: void show_float(float x) { show_bytes((byte_pointer) &x, sizeof(float)); } 00013: void show_pointer(void *x) { show_bytes((byte_pointer) &x, sizeof(void *)); } 00014: 00015: void print_binary(int x) { 00016: int size = (sizeof x) * 8; // makes this machine-independent 00017: // 2's complement 00018: if (x<0) printf("1"); 00019: else printf("0"); 00020: int i; 00021: int mask = 1; 00022: for (i=1; i<size-1; i++) 00023: mask = mask << 1; 00024: for (i=1; i<size; i++) { 00025: if (x&mask) printf("1"); 00026: else printf("0"); 00027: mask = mask >> 1; 00028: } 00029: } 00030:
file:show-bytes-eg.c [source]
00001: #include <stdio.h> 00002: #include <string.h> 00003: 00004: #include "show-bytes.c" 00005: 00006: void test_show_bytes(int val) 00007: { 00008: int ival = val; 00009: show_int(ival); 00010: print_binary(ival); 00011: printf ("\n"); 00012: 00013: float fval = (float) ival; 00014: show_float(fval); 00015: printf ("\n"); 00016: 00017: int *pval = &ival; 00018: show_pointer(pval); 00019: printf ("\n"); 00020: } 00021: 00022: void simple_show() 00023: { 00024: int val = 0x12345678; 00025: byte_pointer valp = (byte_pointer) &val; 00026: show_bytes(valp, 1); /* A. */ 00027: printf ("\n"); 00028: show_bytes(valp, 2); /* B. */ 00029: printf ("\n"); 00030: show_bytes(valp, 3); /* C. */ 00031: printf ("\n"); 00032: } 00033: 00034: void float_eg() 00035: { 00036: int x = 3490593; 00037: float f = (float) x; 00038: show_int(x); 00039: printf ("\n"); 00040: show_float(f); 00041: printf ("\n"); 00042: } 00043: 00044: void string_eg() 00045: { 00046: char *s = "ABCDEF"; 00047: show_bytes((unsigned char *)s, (int) strlen(s)+1); 00048: printf ("\n"); 00049: } 00050: 00051: void show_twocomp() 00052: { 00053: short int x = 12345; 00054: short int mx = -x; 00055: 00056: show_bytes((byte_pointer) &x, sizeof(short int)); 00057: printf ("\n"); 00058: show_bytes((byte_pointer) &mx, sizeof(short int)); 00059: printf ("\n"); 00060: } 00061: 00062: int main(int argc, char *argv[]) 00063: { 00064: int val = 12345; 00065: 00066: if (argc > 1) { 00067: if (argv[1][0] == '0' && argv[1][1] == 'x') 00068: sscanf(argv[1]+2, "%x", &val); 00069: else 00070: sscanf(argv[1], "%d", &val); 00071: printf("Calling test_show_bytes\n"); 00072: test_show_bytes(val); 00073: } else { 00074: printf("Calling show_twocomp\n"); 00075: show_twocomp(); 00076: printf("Calling simple_show\n"); 00077: simple_show(); 00078: printf("Calling float_eg\n"); 00079: float_eg(); 00080: printf("Calling string_eg\n"); 00081: string_eg(); 00082: } 00083: return 0; 00084: } 00085:
Example: Two's Complement [2/8] |
file:unsigned1.c [source]
00001: #include <stdio.h> 00002: 00003: int main() { 00004: int i; 00005: int x; 00006: unsigned int y; 00007: 00008: printf("-1 signed and unsigned\n"); 00009: x = -1; 00010: printf("%d %u\n", x, x); 00011: 00012: 00013: printf("\ngoing up\n"); 00014: x = 1; 00015: y = 1; 00016: for (i=0; i<=32; i++) { 00017: printf("%12d %12u\n", x, y); 00018: x <<= 1; 00019: y <<= 1; 00020: } 00021: 00022: printf("\ngoing down\n"); 00023: x = 1<<31; 00024: y = 1<<31; 00025: for (i=0; i<=32; i++) { 00026: printf("%12d %12u\n", x, y); 00027: x >>= 1; 00028: y >>= 1; 00029: } 00030: return 0; 00031: } 00032:
file:unsigned2.c [source]
00001: #include <stdio.h> 00002: 00003: int main() { 00004: unsigned int x=1; 00005: int i; 00006: for (i=0; i<28; i++) { 00007: x *= 3; 00008: printf("%u\n", x); 00009: } 00010: return 0; 00011: } 00012:
file:twoscomplement1.c [source]
00001: #include <stdio.h> 00002: #include <math.h> 00003: #include "show-bytes.c" 00004: 00005: int main() { 00006: int x; 00007: printf("Enter an integer\n"); 00008: scanf("%d", &x); 00009: printf("%d in 2's complement is ", x); 00010: show_bytes((byte_pointer) &x, sizeof x); 00011: printf("\n"); 00012: return 0; 00013: } 00014:
file:twoscomplement2.c [source]
00001: #include <stdio.h> 00002: 00003: int main() { 00004: int x=1; 00005: int i; 00006: for (i=0; i<28; i++) { 00007: x *= 3; 00008: printf("%d\n", x); 00009: } 00010: return 0; 00011: } 00012:
Example: Floating Point [3/8] |
file:float1.c [source]
00001: #include <stdio.h> 00002: #include <math.h> 00003: #include "show-bytes.c" 00004: 00005: int main() { 00006: float x; 00007: printf("Enter a floating point number\n"); 00008: scanf("%f", &x); 00009: printf("%f in 2's complement is ", x); 00010: show_bytes((byte_pointer) &x, sizeof x); 00011: printf("\n"); 00012: return 0; 00013: } 00014:
file:float2.c [source]
00001: #include <stdio.h> 00002: #include <math.h> 00003: #include "show-bytes.c" 00004: 00005: int main() { 00006: float v = 1.0, // 1.0 00007: w = 2.5, // 10.1 00008: x = 3.25, // 11.01 00009: y = 32.5; // 100000.1 00010: double z = 75.75; // 1001011.11 00011: float a = .5, 00012: b = 3./16.; 00013: int i=1; 00014: float c = *((float *) ((byte_pointer) &i)); 00015: float d = 10.0/0.0; 00016: float n1 = -1.0, 00017: n2 = -3.25; 00018: // float c = 0.00000011920928955078125; // smallest possible float 00019: 00020: printf("%f (1.0) = ", v); 00021: show_bytes((byte_pointer) &v, sizeof v); 00022: printf("\n"); 00023: 00024: printf("%f (10.1) = ", w); 00025: show_bytes((byte_pointer) &w, sizeof w); 00026: printf("\n"); 00027: 00028: printf("%f (11.01) = ", x); 00029: show_bytes((byte_pointer) &x, sizeof x); 00030: printf("\n"); 00031: 00032: printf("%f (100000.1) = ", y); 00033: show_bytes((byte_pointer) &y, sizeof y); 00034: printf("\n"); 00035: 00036: printf("%f (1001011.11) = ", z); 00037: show_bytes((byte_pointer) &z, sizeof z); 00038: printf("\n"); 00039: 00040: printf("%f (.1) = ", a); 00041: show_bytes((byte_pointer) &a, sizeof a); 00042: printf("\n"); 00043: 00044: printf("%f (.0011) = ", b); 00045: show_bytes((byte_pointer) &b, sizeof b); 00046: printf("\n"); 00047: 00048: printf("%1.50f (smallest possible float) = ", c); 00049: show_bytes((byte_pointer) &c, sizeof c); 00050: printf("\n"); 00051: 00052: printf("10.0/0.0 = %f =", d); 00053: show_bytes((byte_pointer) &d, sizeof d); 00054: printf("\n"); 00055: 00056: printf("%f (-1.0) = ", n1); 00057: show_bytes((byte_pointer) &n1, sizeof n1); 00058: printf("\n"); 00059: 00060: printf("%f (-11.01) = ", n2); 00061: show_bytes((byte_pointer) &n2, sizeof n2); 00062: printf("\n"); 00063: 00064: return 0; 00065: } 00066:
file:float-b2u.c [source]
00001: #include <stdio.h> 00002: #include <math.h> 00003: #include "show-bytes.c" 00004: 00005: unsigned int b2u(char *s) { 00006: unsigned int x=0; 00007: while (*s!='\0') { // while s not pointing to NULL 00008: if (*s!='1' && *s!='0') { s++; continue; } // skip non-digits 00009: x <<= 1; // shift left one bit 00010: if (*s=='1') { x++; } // add this bit 00011: s++; // advance s 00012: } 00013: return x; 00014: } 00015: int main() { 00016: double v; 00017: float f; 00018: float *p, *q; 00019: unsigned int x, y; 00020: 00021: p = (float *) &x; 00022: q = (float *) &y; 00023: 00024: v = 0; 00025: printf("%le = ", v); 00026: show_bytes((byte_pointer) &v, sizeof v); 00027: printf("\n\n"); 00028: 00029: v = HUGE_VAL; 00030: printf("%le = ", v); 00031: show_bytes((byte_pointer) &v, sizeof v); 00032: printf("\n\n"); 00033: 00034: v = -HUGE_VAL; 00035: printf("%le = ", v); 00036: show_bytes((byte_pointer) &v, sizeof v); 00037: printf("\n\n"); 00038: 00039: v = 15213; 00040: printf("%le = ", v); 00041: show_bytes((byte_pointer) &v, sizeof v); 00042: printf("\n\n"); 00043: 00044: f = 15213; 00045: printf("%e = ", f); 00046: show_bytes((byte_pointer) &f, sizeof f); 00047: printf("\n\n"); 00048: 00049: //x = b2u("111100001111"); 00050: //printf("%u = ", x); 00051: //show_bytes((byte_pointer) &x, sizeof x); 00052: //printf("\n\n"); 00053: 00054: // Pos and neg zero 00055: x = b2u("0 00000000 00000000000000000000000"); 00056: printf("%e = (+0)\n", *p); 00057: show_bytes((byte_pointer) p, sizeof (*p)); 00058: printf("\n\n"); 00059: 00060: y = b2u("1 00000000 00000000000000000000000"); 00061: printf("%e = (-0)\n", *q); 00062: show_bytes((byte_pointer) q, sizeof (*q)); 00063: printf("\n\n"); 00064: printf("(0)==(-0)? %d", x==y); 00065: printf("\n\n"); 00066: 00067: // Denormalized 00068: x = b2u("0 00000000 00000000000000000000001"); 00069: printf("%e = (0+epsilon)\n", *p); 00070: show_bytes((byte_pointer) p, sizeof (*p)); 00071: printf("\n\n"); 00072: 00073: x = b2u("0 00000000 11111111111111111111111"); 00074: printf("%e = (largest denorm)\n", *p); 00075: show_bytes((byte_pointer) p, sizeof (*p)); 00076: printf("\n\n"); 00077: 00078: // Normalized 00079: x = b2u("0 00000001 00000000000000000000000"); 00080: printf("%e = (smallest norm)\n", *p); 00081: show_bytes((byte_pointer) p, sizeof (*p)); 00082: printf("\n\n"); 00083: 00084: x = b2u("0 01111110 11111111111111111111111"); 00085: printf("%e = (1-epsilon)\n", *p); 00086: show_bytes((byte_pointer) p, sizeof (*p)); 00087: printf("\n\n"); 00088: 00089: x = b2u("0 01111111 00000000000000000000000"); 00090: printf("%e = (1)\n", *p); 00091: show_bytes((byte_pointer) p, sizeof (*p)); 00092: printf("\n\n"); 00093: 00094: x = b2u("0 11111110 11111111111111111111111"); 00095: printf("%e = (largest norm)\n", *p); 00096: show_bytes((byte_pointer) p, sizeof (*p)); 00097: printf("\n\n"); 00098: 00099: // Specials 00100: x = b2u("0 11111111 00000000000000000000000"); 00101: printf("%e = (+INFINTIY)\n", *p); 00102: show_bytes((byte_pointer) p, sizeof (*p)); 00103: printf("\n\n"); 00104: 00105: x = b2u("0 11111111 00000000000000000010000"); 00106: printf("%e = (NaN)\n", *p); 00107: show_bytes((byte_pointer) p, sizeof (*p)); 00108: printf("\n\n"); 00109: 00110: // Denormalized 00111: x = b2u("1 00000000 00000000000000000000001"); 00112: printf("%e = (0+epsilon)\n", *p); 00113: show_bytes((byte_pointer) p, sizeof (*p)); 00114: printf("\n\n"); 00115: 00116: x = b2u("1 00000000 11111111111111111111111"); 00117: printf("%e = (largest denorm)\n", *p); 00118: show_bytes((byte_pointer) p, sizeof (*p)); 00119: printf("\n\n"); 00120: 00121: // Normalized 00122: x = b2u("1 00000001 00000000000000000000000"); 00123: printf("%e = (smallest norm)\n", *p); 00124: show_bytes((byte_pointer) p, sizeof (*p)); 00125: printf("\n\n"); 00126: 00127: x = b2u("1 01111110 11111111111111111111111"); 00128: printf("%e = (1-epsilon)\n", *p); 00129: show_bytes((byte_pointer) p, sizeof (*p)); 00130: printf("\n\n"); 00131: 00132: x = b2u("1 01111111 00000000000000000000000"); 00133: printf("%e = (1)\n", *p); 00134: show_bytes((byte_pointer) p, sizeof (*p)); 00135: printf("\n\n"); 00136: 00137: x = b2u("1 11111110 11111111111111111111111"); 00138: printf("%e = (largest norm)\n", *p); 00139: show_bytes((byte_pointer) p, sizeof (*p)); 00140: printf("\n\n"); 00141: 00142: // Specials 00143: x = b2u("1 11111111 00000000000000000000000"); 00144: printf("%e = (-INFINTIY)\n", *p); 00145: show_bytes((byte_pointer) p, sizeof (*p)); 00146: printf("\n\n"); 00147: 00148: x = b2u("1 11111111 00000001000000000000000"); 00149: printf("%e = (NaN)\n", *p); 00150: show_bytes((byte_pointer) p, sizeof (*p)); 00151: printf("\n\n"); 00152: 00153: return 0; 00154: } 00155:
Example: Functions [4/8] |
file:square.c [source]
00001: #include <stdio.h> 00002: // prototypes 00003: int square(int numb); 00004: int main() 00005: { 00006: int val = 3; 00007: int sq_val; 00008: 00009: sq_val = square(val); 00010: 00011: printf("val = %d; sq_val = %d\n", val, sq_val); 00012: 00013: return 0; 00014: } 00015: 00016: int square(int numb) 00017: { 00018: return numb*numb; 00019: } 00020: 00021:
Example: Pass by Value [5/8] |
file:square2.c [source]
00001: #include <stdio.h> 00002: // prototypes 00003: void square(int numb); 00004: int main() 00005: { 00006: int val = 3; 00007: 00008: square(val); 00009: 00010: printf("val = %d\n", val); // prints 3 (not 4) 00011: 00012: return 0; 00013: } 00014: 00015: void square(int numb) 00016: { 00017: int sq; 00018: 00019: numb++; // 'numb' MODIFIED HERE to 4 00020: sq = numb * numb; 00021: printf("sq = %d\n", sq); // prints 16 00022: } 00023: 00024:
Example: Addresses and pointers [6/8] |
int *p; // p will contain the address of an int variable // in other words, p will point to an int variable
Example: The dereferencing operator [7/8] |
*&x
is the same as x
file:pointer.c [source]
00001: #include <stdio.h> 00002: 00003: int main() { 00004: int x; 00005: int *xptr; 00006: xptr = &x; 00007: *xptr = 5; 00008: printf("x = %d\nxptr *dereferenced is %d\n",x, *xptr); 00009: return 0; 00010: } 00011:
Example: Passing pointers as parameters [8/8] |
file:square4.c [source]
00001: #include <stdio.h> 00002: // prototypes 00003: void square(int *numb); 00004: int main() 00005: { 00006: int val = 3; 00007: 00008: square(&val); 00009: 00010: printf("val = %d\n", val); // prints 4, since val was changed in square 00011: 00012: return 0; 00013: } 00014: 00015: void square(int *numb) 00016: { 00017: int sq; 00018: 00019: (*numb)++; // '*numb' (same as 'val' in main) MODIFIED HERE to 4 00020: sq = (*numb) * (*numb); 00021: printf("sq = %d\n", sq); // prints 16 00022: } 00023:
Revised: 2007/04/05 17:41