Thinking in C – Chapt 5: Compound data types

July 10, 2006

This chapter was a good refresher on structures in C. Anyways a little introduction to compound data types in C.There are three types of compund data types in C: array,strings, and structures. Strings are just a special type of character array where the last element is a null character.

We can read as well as write data incore instead of the regular console by using sscanf and sprintf.

eg: sscanf(str, “%d %f”, &num, &flo); In this example the data will be read from the buffer reffered to by “str” and not standard input.

eg: sprintf(str, “%d %f”, num, flo); In this example the data will be written to a buffer reffered to by “str” instead of standard output.

A structure is a data type that consists of primitive as well as other compund (including structures) data types. For eg: this is how we will represent an Employee record using structures.

Exercise: Make an Employee structure which will contain last_name, first_name, title, and salary. Accept details from the user and populate an array of Employees till the user submits “enter” for the last_name. The program then prints the list of employees.

Did the exercise.

When the program accepts a decimal, the newline that the user presses to submit the number lingers in the input stream and must be flushed or eaten. Otherwise it will be accepted in the next scanf.

Further Research:

  • What happens if the number of characters we enter are more than what the character array accepts.
  • What is the difference between gets() and scanf().

Thinking in C – Chapt 4: Controlling Program Flow

July 10, 2006

An interesting thing I learned here, is that way back in the sixties some scientists had proved that an alogorithm can be expressed using a :

  • Sequence of statements
  • Decision making constructs
  • Looping constructs
  • An arbitrary number of boolean flags

That was interesting…

A case statement can be used only with integers.

A scan with leading spaces before %c will ignore any number of whitespaces. If we do not include the space, then a space entered by mistake by the user will be read by scanf, ignoring the actual character value the user might have entered.

eg: scanf(” %c”, &c); //note the whitespace before %c

Chuck says that we should not declare variables within a switch clause. He did not explain the reason. Perhaps it is complicated and beyond the scope of this tutorial.

Excercise: Did the exercise. It was similar to the one in chapt 3, except that this time the user could enter an arbitrary number of integers (0 for quit).

Further Research:

  • Why should we not declare variables inside a swicth clause?

Thinking in C: Chapt 3: Operators

July 10, 2006

I am glad I remembered most of the things explained in this chapter. Operators in Java are very similar to operators in C. Infact that is one thing Java has taken right of from C++. C even supports short circuit for the logical operators.

The only difference in C is that it does not have boolean types. 0 is taken as false, while any non zero value is taken as true.

Operator precedance when using bitwise operators with other operators can get tricky, so it is always better to use parenthesis. Check the precedance table for more info.

Did the exercise. It involved reading three integers and printing the sum of odd and even integers that were entered. The purpose of the exercise was to explain the usage of operators.

Further Research:

  • Check formatting with Hexadecimal operators with the exersice.
  • Need a greater understanding of scanf. Why does scanf(“%d %d %d”) with values of 1 2 . yeild 2293674 for the . ?
  • What is the difference between puts() and printf()?

Thinking in C – Chapt 2: Fundamental Data Types

July 9, 2006

This chapter dealt with Fundamental Data Types in C: I was able to refresh many things that I had forgotten.

Characters are also numbers, but they are treated differently in C

There are 3 types of Integers (short, int, long), and the number of bits they may use is platform dependant. The size of an int is the word size on that platform (which in most modern platforms would be 32 bits), the size of short is 16 bits and the size of long is again platform dependant, but is at least 32 bits. This means that on some platforms short and int may be the same size and sometimes int and long may be the same size. However the limits for numeric type are always specified in certain header files. <limit.h> for integer limits and <float.h> for floating point limits.

There are 3 types of floating point numbers as well: float (single precision), double (double precision), and long double (extended double precision). The size of ‘double’ and ‘long double’ is platform dependant. Floating point numbers have a non standard distribution, which means that a number we want may not be representable. In such cases the system will round it to the nearest representable number. The direction of rounding is system dependant.

Characters are 8 bits wide which is why Unicode characters cannot be supported in C, however some implementations have a special type called ‘wchar_t’ (stands for wide char), which supports 16 bit characters.

Some examples of literals:
int i = 9, j = 017, k = 0x7F; (Base 10, Base 8, Base 16)
char c = ‘a’, c2 = 97 (97 may not mean the same character on different platforms)
long n = 1234567L;
float f = 1.23F;
double n = 1.34; (default)

constants can be initialized with other constants, literals, as well as variables. However in C a constant cannot be used to specify an array dimension. C++ on the other hand does allow this. In C we have to use # define to define a constant value that can be used to specify an array dimension.

Did the assignment. It ivolved reading in a floating point number, printing it out and then printing out the integer representation of the number. The purpose of the exercise was to show that when a floating point number is typecast into an integer, we lose the fractional part of the number.

Further Research:

  • Need to refresh concepts of mantissa, and power.

Starting a refresher course in C with Bruce Eckel’s “Thinking In C”

July 9, 2006

I have been meaning to refresh my C and C++ concepts from sometime now. An assignment has actually put it on top of my priority list.

For C I will take the multimedia course “Thinking In C” available on Bruce Eckels website. The course audio lectures and exercises. I might complement the course content with other online material. I still have’nt decided what to use for C++ yet.

Introduction: This was a good introduction to C. I was able to refresh a few things like:

Header files that use the standard C library always use <> to include their content
#include <stdio.h>
while user defined header files will use “”.
#include “custom.h”
The angular brackets tell the compiler to look for these files in a specific place, while the inverted commas indicate to the compiler to look for those files in the current directory. It is possible to make the compiler to look for the files in other directories by adding the directories to certain environment variables (PATH), however this feature is system dependant.

I remembered that in C we can redirect the standard output to a file like this: myprog > output.txt But now I have remembered that we can also redirect standard input, so that it comes from a file, like this: myprog < input.txt

Assignments: Compile the sample programs shown in the slides and experiment with I/O redirection.

Did the assignments, and experimented with output redirection.