260 likes | 369 Views
ecs30 Winter 2012: Programming and Problem Solving # 03: Chapters 2~ 3. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. Do I need to know exactly where to store/save the value of the variable “ miles ” ?.
E N D
ecs30 Winter 2012:Programming and Problem Solving#03: Chapters 2~3 Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu ecs30 winter 2012 Lecture #03
Do I need to know exactly where to store/save the value of the variable “miles”? ecs30 winter 2012 Lecture #03
“Address” • Linguistics in C programming Language We need a way to represent “address”! 01101001 10110001 11100000 10100111 0x69b1e0a7 ecs30 winter 2012 Lecture #03
0 = 0000 = 0 * 8 + 0 * 4 + 0 * 2 + 0 * 1 1 = 0001 = 0 * 8 + 0 * 4 + 0 * 2 + 1 * 1 2 = 0010 = 0 * 8 + 0 * 4 + 1 * 2 + 0 * 1 3 = 0011 = 0 * 8 + 0 * 4 + 1 * 2 + 1 * 1 ....... 7 = 0111 = 0 * 8 + 1 * 4 + 1 * 2 + 1 * 1 8 = 1000 = 1 * 8 + 0 * 4 + 0 * 2 + 0 * 1 9 = 1001 = 1 * 8 + 0 * 4 + 0 * 2 + 1 * 1 10 (a) = 1010 = 1 * 8 + 0 * 4 + 1 * 2 + 0 * 1 11 (b) = 1011 = 1 * 8 + 0 * 4 + 1 * 2 + 1 * 1 12 (c) = 1100 = 1 * 8 + 1 * 4 + 0 * 2 + 0 * 1 13 (d) = 1101 = 1 * 8 + 1 * 4 + 0 * 2 + 1 * 1 14 (e) = 1110 = 1 * 8 + 1 * 4 + 1 * 2 + 0 * 1 15 (f) = 1111 = 1 * 8 + 1 * 4 + 1 * 2 + 1 * 1 09FE (in Hex) == ? (in Binary) == ? (in Decimal) ecs30 winter 2012 Lecture #03
$ chmod640 h.c moobilenet-105-214:/Users/wu% ls -lh.c -rw------- 1 wu staff 107 Jan 18 20:46 h.c moobilenet-105-214:/Users/wu% chmod640h.c moobilenet-105-214:/Users/wu% ls -lh.c -rw-r----- 1 wu staff 107 Jan 18 20:46 h.c moobilenet-105-214:/Users/wu% -rw------- -110000000 -rw-r----- -110100000 ecs30 winter 2012 Lecture #03
$ chmod 751 a.out -rwxr-x--x -111101001 ecs30 winter 2012 Lecture #03
“Memory Box” Each “address-able” box is ONE Byte 1 byte = 8 bits 1 bit is 0 or 1 Binary representation ecs30 winter 2012 Lecture #03
Binary Representation • Integer (int) 4 bytes • intx; • Double 8 bytes • double miles; • Character 1 byte • char c; • And, it depends on OS and hardware… ecs30 winter 2012 Lecture #03
Write a Program to find out…. /* an example program for the sizeof operator in C */ #include <stdio.h> int main(void) { printf("The size of an integer is %d.\n", sizeof(int)); printf("The size of a float is %d.\n", sizeof(float)); printf("The size of a double is %d.\n", sizeof(double)); printf("The size of a character is %d.\n", sizeof(char)); } Output: The size of an integer is 4. The size of a float is 4. The size of a double is 8. The size of a character is 1. ecs30 winter 2012 Lecture #03
Character /* an example program for the sizeof operator in C */ #include <stdio.h> int main(void) { printf("The size of an integer is %d.\n", sizeof(int)); printf("The size of a float is %d.\n", sizeof(float)); printf("The size of a double is %d.\n", sizeof(double)); printf("The size of a character is %d.\n", sizeof(char)); } Output: The size of an integer is 4. The size of a float is 4. The size of a double is 8. The size of a character is 1. ecs30 winter 2012 Lecture #03
How do I represent a character? (Internal coding) ASCII (American Standard Code for Information Interchange) use 7 bit to represent a character (might or might not on your keyboard) Example: 'A' 1100001 (in Byte, 0110001) (Appendix D, A) Decimal: 65, Hex: 41. Question: How many different characters can we represent? ecs30 winter 2012 Lecture #03
“Memory Box” Decimal “1” Each “address-able” box is ONE Byte 1 byte = 8 bits 1 bit is 0 or 1 Binary representation ecs30 winter 2012 Lecture #03
“Memory Box” 00000000 00000000 00000000 00000001 Each “address-able” box is ONE Byte 1 byte = 8 bits 1 bit is 0 or 1 Binary representation ecs30 winter 2012 Lecture #03
“Memory Box” 00000001 00000000 00000000 00000000 Each “address-able” box is ONE Byte 1 byte = 8 bits 1 bit is 0 or 1 Binary representation ecs30 winter 2012 Lecture #03
scanf(“%lf”, &miles); ecs30 winter 2012 Lecture #03
scanf(“%lf”, &miles); “Format String” -- I am going to take a Long Float # (double) from the stdin (Standard Input device)” ecs30 winter 2012 Lecture #03
scanf(“%lf”, &miles); 0x 7014ae00 0x 7014ae01 0x 7014ae02 0x 7014ae03 0x 7014ae04 0x 7014ae05 0x 7014ae06 0x 7014ae07 8 bytes ecs30 winter 2012 Lecture #03
scanf(“%lf”, &miles); 0x 7014ae00 0x 7014ae01 0x 7014ae02 0x 7014ae03 0x 7014ae04 0x 7014ae05 0x 7014ae06 0x 7014ae07 8 bytes scanf(“%lf”, 0x7014ae00); ecs30 winter 2012 Lecture #03
printf(“%lf\n”, &miles); printf(“%lf\n”, miles); printf(“%lf\n”, 0x7014ae00); printf(“%lf\n”, 30.5); 8 bytes 0x 7014ae00 0x 7014ae01 0x 7014ae02 0x 7014ae03 0x 7014ae04 0x 7014ae05 0x 7014ae06 0x 7014ae07 ecs30 winter 2012 Lecture #03
printf(“%lf\n”, &miles); printf(“%lf\n”, miles); printf(“%lf\n”, 0x7014ae00); printf(“%lf\n”, 30.5); 0.0000 30.5 0.0000 30.5 8 bytes 0x 7014ae00 0x 7014ae01 0x 7014ae02 0x 7014ae03 0x 7014ae04 0x 7014ae05 0x 7014ae06 0x 7014ae07 ecs30 winter 2012 Lecture #03
printf(“%lf\n”, &miles); printf(“%lf\n”, miles); printf(“%lf\n”, 0x7014ae00); printf(“%lf\n”, 30.5); printf(“%lf\n”, *(&miles)); printf(“%lf\n”, *((double *) 0x7014ae00); 8 bytes 0x 7014ae00 0x 7014ae01 0x 7014ae02 0x 7014ae03 0x 7014ae04 0x 7014ae05 0x 7014ae06 0x 7014ae07 C gives you many options to access and control your memory! ecs30 winter 2012 Lecture #03
printf(“%lf\n”, &miles); printf(“%lf\n”, miles); printf(“%lf\n”, 0x7014ae00); printf(“%lf\n”, 30.5); printf(“%lf\n”, *(&miles)); printf(“%lf\n”, *((double *) 0x7014ae00); 8 bytes 0x 7014ae00 0x 7014ae01 0x 7014ae02 0x 7014ae03 0x 7014ae04 0x 7014ae05 0x 7014ae06 0x 7014ae07 In C, Pointer = Address + Type! And, Address is merely a 32 bit unsigned integer! ecs30 winter 2012 Lecture #03
Figure 3-4: #define pi 3.1415926 #define Area 3.14 * R_big * R_big /* get the radius of bigger circle R_big */ /* get the radius of bigger circle R_inner*/ A = pi * r * r; A = 3.1415926 * r * r; ecs30 winter 2012 Lecture #03
{ … } • { … } • {… { … {…}…} … {…}…} • int main (void) { … } • float myfunc (int x) { …; return f;} • { <declarations> <statements> } ecs30 winter 2012 Lecture #03