Upgrade to Pro — share decks privately, control downloads, hide ads and more …

06-NumbersOperators.pdf

Avatar for William Albritton William Albritton
September 04, 2015
180

 06-NumbersOperators.pdf

Avatar for William Albritton

William Albritton

September 04, 2015
Tweet

Transcript

  1. #define  Usually at the top of the code 

    Used to create symbolic constants  Used to create macros
  2. #define format  #define identifier replacement-text  All subsequent occurrences

    of identifier will be replaced before the program is compiled  Occurrences that appear in string literals (strings in double-quotes) will not be replaced
  3. #define PI 3.14159265358979323846 Symbolic Constant  Creates a name for

    a constant in the program  NO semi-colon!!!!  Redefining a symbolic constant with a new value is an error  See example program: define.c
  4. Macro  Macros enable you to name a small instruction

    and use the name throughout the program.  Example:  #define AREA b*h  More about this later…
  5. Data Types are System-Dependent  On UH UNIX operating system,

    we are using GCC  GNU Compiler Collection (GCC) is a compiler system, which was created by the GNU Project and supports languages such as C (gcc), C++ (g++), and others %gcc –v gcc version 3.4.6
  6. Data Types are System-Dependent Integers char Maximum = 127 short

    Maximum = 32,767 int Maximum = 2,147,483,647 long Maximum = 2,147,483,647 *GCC on UH UNIX
  7. Data Types are System-Dependent Floating Point float 6 digits of

    precision Maximum exponent 38 Maximum value 3.402823e+38 double 15 digits of precision Maximum exponent 308 Maximum value 1.797693e+308 long double 33 digits of precision Maximum exponent 4932 Maximum value 1.189731e+4932 *GCC on UH UNIX
  8. Example Program  Try running this program on different platforms

    to see if you get different outputs  See program at: maximum.c
  9. Number Representation  Integer numbers are exactly represented as binary

    numbers  Floating-point numbers are approximately represented as binary numbers, with three parts: sign, exponent, and mantissa  Because of round-off errors, floating-points are not exact, except for negative powers of two (0.5, 0.25, …)
  10. Scientific Notation  Floating point numbers are expressed as a

    mantissa times a power of ten  Exponential notation: the x10n is substituted for e n  Examples:  155.97 = 1.5597x102 = 1.5597e2  2.3 = 2.3 x 100 = 2.3e0
  11. Precedence of Arithmetic and Assignment Operators Precedence Operator Associativity 1

    Parentheses: ( ) Innermost first 2 Unary operators: + - ++ -- (type) Right to left 3 Binary operators: * / % Left to right 4 Binary operators: + - Left to right 5 Assignment operators: = += -= *= /= %= Right to left
  12. Overflow and Underflow  This occurs when numbers are either

    too big or too small to be stored. overflow overflow within allowed range within allowed range underflow close to zero large negative large positive
  13. Integer Division  Integer division produces an integer result. 

    Integer division truncates  1/2  1 divided by 2 is 0  9/5  9 divided by 5 is 1
  14. Modulus  The remainder after division (%)  10 %

    5  10/5 = 2, remainder 0  19 % 5  19/5 = 3, remainder 4  1 % 2  1/2 = 0, remainder 1
  15. Assignment Operators  x = x + 5;  Shortcut:

    x += 5;  a *= b + 5;  Evaluates to a = a *(b + 5);  And not a = a * b + 5;
  16. Implicit Conversion  When using operands of different types, the

    “narrower” one will be converted to the “wider” one
  17. Implicit Conversion  Examples:  1 / 3.0 (int/float) evaluates

    to 0.33333  1.0 / 2 (float/int) evaluates to 0.5
  18. Explicit Conversion  Also know as Type Casting  Converting

    one data type into another by using the (datatype) operator  Usually implies a potential loss of data precision.
  19. Explicit Conversion  variable2 = (datatypeOfVariable2) variable1;  See example

    program at: number.c double number1 = 12.34; int number2 = 0; number2 = (int)number1; //number2 will be 12, loss of precision
  20. Characters  Char variables are enclosed in single quotes: 'a'

     Don’t forget that integer 3 is not the same as character '3' char letter = 'a'; //0x61 char integer3 = 3; //0x03 char letter3 = '3'; //0x33
  21. printf  The printf function allows us to print to

    standard output (the screen)  printf statements usually contain  Control string: To label the output  Conversion specifier: To format the output  Argument: What needs to be printed
  22. Conversion Specifiers Variable Type Output Type Specifier Integer Values char,

    short, int int %i, %d int short %hi, %hd long long %li, %ld int unsigned int %u int unsigned short %hu long unsigned long %lu
  23. Conversion Specifiers Variable Type Output Type Specifier Floating-Point Values float,

    double double %f, %e, %E, %g, %G long double long double %LF, %Le, %LE, %Lg, %LG Character Values char char %c
  24. Using Flags Flag Description - Left justify + Precede numerical

    values with the corresponding sign # Prefix O when using octal specifier Prefix OX when using hexadecimal specifier Forces decimal point for floating points printed with e, E, f, g or G without a fractional part For G and g, trailer zeroes are not eliminated 0 Pad a field with leading zeroes
  25. Function puts()  Function puts() displays a string and a

    newline puts(“Aloha!”); puts(“Hey, y'all!”); /* Aloha! Hey, y'all! */