AMAT-2120, Lab3

Programs:     Thursday     Friday

Comments

1. Let's keep our work organized: make a separate directory for each week's lab. You can facilitate your work with C code using editor's settings, such as syntax highlighting and font size. (In nedit, go to menu Preferences, then to an appropriate subitem, check it, then click on Save defaults.) If the file in the editor window is not a ".c" file, then syntax highlighting may be disabled.

2. You can open the editor from the menu icon in the left lower corner of the screen. Alternatively, you can type nedit & in the console command line. (The character & -- ampersand -- means that the typed command is to be detached, i.e. executed independently, from the console).

3. Compiling the program with warning options turned on
      gcc -Wall lab3.c
we can spot the following deficiencies which could otherwise remain unnoticed:
  -- Declared but unused variables;
  -- Mismatch of the number of format specifiers in a prinf statement and the number of parameters (variables) in the output list in printf (after comma, outside the quotes). Like this:
      printf("The value of i=%d(dec)=%o(oct)\n",i);
(More format specifiers than arguments. Result: random output of the second (%o) value.)
      printf("The value of i=%d(dec)\n",i,i);
(More arguments than format specifiers. Result: warning when compiling with -Wall option. Perhaps the programmer forgot to supply a format specifier for the second argument.)

4. Missing initialization of a value yields random results.
(If the line i=65 in Friday's program is commented out, then the printed value of i is an unpredictable, usually huge, number).

5. Using format specifier %f for integer variables yields strange (though, in principle, predictable results). As an example, at one time on Friday I had this line in the program:
      printf("The value of i=%f\n",i);

6. There should be no big trouble predicting results of usual arithmetic operations and their shorthands, except possibly the integer division and the remainder operation %. Do your experiments (see last part of Friday's program) and review p.44 of textbook. Use a cast to float or double to obtain quotients of integers as decimal fractions.