CS120 Day02: Execution Environment & gdb ---------------------------------------------- GDB ======================================== See James' notes and example files on bitbucket for using gdb! - C "features" - obscure code - illicit hacking - system level programming - really efficient memory usage - shoot yourself in the foot - different language versions evolved over time - different implementations (compilers) based on platform/hardware DATA TYPES =============================== - primitive data types - not as strongly typed as Java - exact sizes of each are implementation defined! (platform dependent) - char, short, int (default), long, float, double, long double - unsigned qualifier can apply to any (signed is default) - const qualifier (final in Java) - integer constants - decimals (base 10) - int: 13, 23, -14, etc. - long: 13L, -14l, etc. - octal (base 8) - precede value with 0 - digits range from 0 to 7 - 032 - hexadecimal (base 16) - precede value with 0x - digits range from 0 to 9 and a-f or A-F - 0x15, 0xA - binary (base 2) - precede value with 0b - digits are 0 and 1 only - 0b1000, 0b01101 - sizeof operator: returns integer # bytes of object sizeof (myarray), sizeof(anytype), sizeof varname # arrayelements = sizeof (myarray) / sizeof(arraytype) - compiling into assembly and examining the code OPERATIONS ================================== if (num = 10) // true if (num == 10) - bitwise operators num1 & num2 - bitwise and num1 | num2 - bitwise inclusive or num1 ^ num2 - bitwise exclusive or ~num1 - bitwise complement num << shiftbits - left shift of num by shiftbits places, pad 0 right num >> shiftbits - similar right shift, pad is system dependent Usage/Examples: - mask as application of & - set bits with | - see encrypt.c for examples - built-in math functions - use #include - may need to compile with gcc -lm option - generally take double arguments, return double values - eg: sqrt(x), pow(x,y), exp(x), log(x), ceil(x), floor(x), sin(x) - output - void putchar(char); - puts("some string"); - printf("format string", args...); - formats: (Appendix B) %i %d %f %c %s %n %ld %lf %o octal %x hex - field widths %cols.decf - eg. %.2f, %.6f right justified by default use %- to left justify - and cols works for %d and %s also - input - int getchar() - EOF predefined constant for end of file