Saturday, September 27, 2008

my learnings of the week

This week we tackled how to use/apply the conditional statements and the six types of it (their general form).


Types of conditional statements:

The If Statement
The If-Else Statement
The Nested-If Statement
The If-Else-If Ladder
The Switch Statement
The Nested Switch Statement




The If Statement

The general form of the If statement is:
if ( expression)
statement;

The If-Else Statement

The general form of the if-else statement is:

If (expression)
statement_1;
else
statement_2;
The general form of the if-else statement with block of statement is:


If (expression)
{ statement_sequence;
}
else
{
statement_sequence;
}

The Nested-If Statement

Consider the following situations:
– Situations 1. The else at number 3 is paired with the if in number 2 since it is the nearest if statement with the else statement.
1. if…..
2. if ……

3. else
Consider the following situations:
Situations 2. The else in number 5 is paired with the if in number 1.
1. if ….
2. {
3. if ….
4. }
5. else


The If-Else-If Ladder

The general form of the if-else-if ladder statement is:

if ( expression_1)
statement_1;
else if (expression_2)
statement_2;
else if (expression_3;
statement_3;
:
:
else
statement_else;
l Where:
– If and else are reserve words in C
– Expression_1, expression_2 up to expression_n in relational or boolean expression that evaluates to a TRUE (1) or False (0) value.
– Statement_1, statement_2 up to statement_else may either be a single C statement or a block of C statement.


The Switch Statement

The general form of the switch statement is:

switch (variable)
{
case constant1:
statement sequence_1;
break;
case constant2:
statement_sequence_2;
break;
:
:
default:
statement_sequence_default;
}


The Nested Switch Statement

switch (variable)
{
case constant:{
switch (variable)
{
case constant1:
statement sequence_1;
break;
case constant2:
statement_sequence_2;
break;
}
break;
}
case constant2:
statement sequence;
break;
default:
statement sequence;
}

What a...!

Want to know the features of Turbo C?!!!...
Well... Let's find out!


Features and Deficiencies of TurboC

Features

  • TurboC provides most of the console-i/o functionality of Turbo C's conio.h header, mapping it to related ncurses functionality. (Currently, every conio function except cscanf is supported.)
  • TurboC provides most of the "BGI" functionality of Turbo C's graphics.h header. At present (20020608), implementation of these features is mostly complete except for the temporary lack of stroked-font support.
  • TurboC provides a sprinkling of other missing functionality like the strupr and strlwr functions.
  • Check out the complete list of available functions.

Deficiencies

  • Refer to the complete list of bugs and issues. So far, the library is only as complete as it needs to be for the kind of programs I typically used to write in Turbo C. As I port more of my old programs to *nix, the TurboC libraray will presumably become more complete. Presently, support for conio.h is essentially complete, and support for graphics.h is essentially complete except for displaying text.
  • In the ported program, the text-console window is logically sized properly, but is not physically sized properly unless xterm is used. If other terminals are used (such as KDE Konsole), the window must be resized manually by the user.
  • Borland's Turbo C is a 16-bit compiler, whereas the GNU gcc compiler I'm targeting is 32-bit. This results in a discrepancy in the integer datatypes. TurboC handles the (hopefully) most common cases by means of macros; unhandled cases will cause compile-time errors and must be fixed manually. This is covered in detail on the usage page.
  • Mixing and matching stream-based functionality (like getchar ,printf, and so on) with "console" based functionality (like getch, cprintf, and so on) probably won't work. Again, look at the usage page.

Download and Install TurboC

Download Source-Code Tarball
Description Download
*Most-current development code (20040218) TurboC-dev.tar.gz
20020602 snapshot TurboC-20020602.tar.gz
20020419 snapshot TurboC-20020419.tar.gz
20020418 snapshot TurboC-20020418.tar.gz
20020323 snapshot TurboC-20020323.tar.gz
20020317 snapshot TurboC-20020317.tar.gz
20020312 snapshot TurboC-20020312.tar.gz

The tarball contains a directory called TurboC-source. Within this directory, you can build the library simply by running make. This must be GNU make, and may be called gmake (for example, on FreeBSD or Mac OS X). (Note: In FreeBSD, you may need to edit the Makefile and comment/uncomment a couple of lines at the top, in a way that will be obvious to you.) I've verified that the most recent release snapshot (as opposed to the development snapshot) compiles and that a test program works on the following systems:

Linux Intel 'x86 (SuSE 8.0)
Linux PowerPC (SuSE 7.3)
FreeBSD (4.5)
(Early versions of the TurboC library which indicated that they worked with FreeBSD were probably over-optimistic in that assessment. Please upgrade to a version of at least 20020420, and be sure to follow the advice on running the ported programs.) The most current development snapshot has been tried only on SuSE 8.2.

Put the libraries (libTurboC.a and libTurboCu.a) wherever libraries normally go on your system -- usually /usr/lib, I suppose. Put all of the header files wherever they normally would go -- /usr/include, I suppose. Or, you can just symbolically link them in these directories. Or, put them wherever you like and add appropriate -L and -I switches to your gcc command line.

Here's the way I would do it on my own system:

cd ~
tar -xzvf TurboC-dev.tar.gz
cd TurboC-source
... edit Makefile in FreeBSD ...
make (or gmake)
su
ln -s *.h /usr/include
ln -s libTurboC.a /usr/lib
exit
You may find (for example, in FreeBSD) that if you #include TurboC header files in the programs you're trying to port, you get compiler errors about symbolic links being "too deep". In this case, remove the "-s" switch from the ln commands above.

Also, the Makefile makes certain assumptions about the locations of the X-window system header files and libraries; if these assumptions are wrong, you may need to alter the compiler switches "-L" and/or "-I" within the makefile. The X-window system is needed to compile the complete library, or to run a ported program, if you use any functions from graphics.h. If you don't intend to use these functions, you can use ported programs or compile the library on a system without X, by editing the Makefile and removing the compiler switch "-DWITH_X".

Mac OS X

[Note: The following instructions were written before graphics.h support was added to the TurboC library. This support requires the X-window system. Therefore, you must either disable TurboC graphics support (by removing the command-line switch "-DWITH_X" from the Makefile), or you must install the X-window system. At this point, I've not tried either of these with Mac OS X, so you're pretty much on your own.]

As most people in the *nix world know, Mac OS X is based on FreeBSD. Knowing this, you might suppose that if the TurboC library works on FreeBSD then it will work on Mac OS X. Alas! this optimistic appraisal is revealed as simplistic when viewed in the of harsh light of reality.

The TurboC library has indeed reached the stage where some versions (though not necessarily the current snapshot) compile and partially work under Mac OS X, if ncurses happens to be installed. Apple does not include ncurses in Mac OS X -- at least not as of the version 10.1 release. But ncurses available as a download from macosx.forked.net.

If you download and install ncurses, you should then be able to build the TurboC library as follows:

gunzip TurboC-dev.tar.gz
tar -xvf TurboC-dev.tar
cd TurboC-source
make
What you do after this, is a mystery to me, though. I don't know how get the TurboC libraries nor the header files installed, because I can't figure out how to get the root-level permissions required to do so! Also, as I've said, the library only partially works. Perhaps it's a problem in the TurboC library ... perhaps it's a problem in the ncurses port. If anyone wants to explain this stuff to me, I'd be happy to know about it. Otherwise, for me, working with Mac OS X is too much like pulling teeth, so it's going to be quite a while before I work out the problems on my own initiative.

Future plans

Well, it's pretty obvious that this kind of project has almost nowhere to go, since its whole purpose is to port code from a long-obsolete compiler. However, I'll probably add new functions as the need for them arises in my own projects. I might not be motivated to waste my energies writing functions that don't appear in my Turbo C programs, but if you've created any such missing functions for yourself, I'd love to add them to the library (crediting you appropriately) if you only tell me about them.

Of course, feel free to tell me about bugs in TurboC. (Before doing so, of course, check out the list of already-known bugs.) I'll certainly make every effort to fix them.


I can say that Turbo C is an absolutely fascinating program!