CL001 - Brief Introduction To Borland C and Your First C Program

Languages : C
Tools : TC.EXE
Statements : main(), printf(), getchar()
Prerequisites : Install Borland Turbo C++ 1.01 (from Borland's website. Borland have setup what they call a museum which contains old but useful programs from past years. Download Turbo C++ from here, unzip the file and run INSTALL.EXE. After installing I recommend that you do the tour. Do this by double-clicking TCTOUR.EXE in the folder called TOUR in the folder where you installed Turbo C++, probably folder TC)

Why C?

The C programming language is a "high level" programming language, like Basic is. C is also what's known as a procedural language. That means we make it do things in a lot of separate procedures - more about this in a later lesson. It is also sometimes called the lowest high level language, because you can do things in it that otherwise you would need assembler to do. Programmer's opinions of C tend to fall into one of two groups: either "It's wonderful - you can do anything" or "It's terrible - you can do anything". If you don't understand the second opinion, it means that using advanced C concepts you can really mess up your computer if you are not careful. In fact there is an annual contest to see who can write the most obscure C code! If this is getting you worried, don't be. We won't be looking at such tricky coding for a while yet and even then, there won't be a problem as long as we are careful. On the other hand maybe the contest I mentioned intrigues you. If so search the Internet and start learning the fun world of C for yourself.

The other main reason for learning C is that several of the more advanced languages that we want to get to later are based on C - e.g. C++, Java and C#. Once you know how to code in C, the others will be much easier to pick up. So here we go.

Starting Turbo C++

WARNING : Turbo C++ is a DOS program, which means that you could have the same issues as with QBasic. If you need to swap between full screen and windowed, just remember Alt - Enter.

Hang on a minute, I thought you said C++ was based on C and we were going to learn C first. Why then are we using a C++ tool? Ah ha! C++ is what's known as a superset of C. That means that it will do everything that C will, plus some more things. Therefore it will work perfectly well with C programs and we can ignore the C++ component for now.

OK, to start Turbo C++, you need to double-click TC.EXE. This will be in a folder called BIN inside the folder where you installed Turbo C++, which will also be called TC if you used the default installation directory. (You may be wondering why the folder is called BIN. It's short for binary. Since all that computers really know are 1's and 0's, i.e. binary numbers, executable code or programs are often called binaries. Again, more later.) If you are like me you may get tired of looking for this every time so you may want to create a shortcut. Up comes a screen similar to the following :


Startup screen.

You may or may not have the About window. Press Enter to close it. Did you do the Tour? If not, now's a good time. (I did recommend it, didn't I :-)) Press Alt - X to exit Turbo C++ and run TCTOUR in the TOUR folder. When you are finished come back here.


Blank edit window. If you don't have a window named "NONAME00.C" or something similar, don't panic. Just use the file menu to create a new edit window.

Your First C Program

Type the following.  You'll notice that it appears in the section headed "NONAME00.C".

#include <stdio.h>

main()
{
    printf("This is my first C program - wahoo!!!\n");
    return(0);
}


All typed in. Note, NO uppercase except between the quotes.

Now before we do anything else let's save our work, or else we'll end up with a program called NONAME00.EXE. Not particularly useful. :-) You can save your program by clicking on Save in the File menu, but let's use the shortcut and press F2. In the dialog box that pops up, backspace the file name NONAME00.C and type first.c and press Enter. Our program is now FIRST.C which is a much better name. :-) (The C on the ends says that it is a C program - what a surprise.)


Save window. Hmm, directory stuff. Let's not worry about that for now.

Alright, let's try it out. Press Ctrl-F9 (i.e. hold down the key marked "Ctrl" and press the key marked "F9" on your keyboard).

Hmm, nothing much seems to have happened. If you typed in the program correctly, a box will have come up on the screen briefly displaying the title "Compiling", then disappeared. If the box stayed there and the number next to errors is not 0, then you've typed in something slightly different. Probably very slightly different. Programmers have a saying: "Computers are the most brilliant idiots in the world - they'll do exactly what you tell them, no matter how stupid!" :-) So if you're having problems, carefully compare your code to mine and play spot the difference. Everything is important, even uppercase and lowercase.

OK, assuming you didn't have any errors, then the good news is that something did happen, we just can't see it. Press Alt-F5. The screen changes to a black background, and up at the top left corner of the screen appears our message.


The User's screen and our message. Yay!

Yahoo it worked! Great. Now - er - how do we get back? Don't worry. Just press any key. Ah there we are. Now, for your information, the screen we just looked at is called the User Screen and can be reached from the Window menu. Another way of viewing text output like our message is with the Output window. Use the Window menu to open The Output window (no shortcut this time).


The output of your first C program! To swap between windows try pressing F6 a few times.

But what does it all mean?

Looking at the code of this program, it looks a lot more complicated than our single line in QBasic. All the extra bits are very important and, as we go on, very useful. However for now just accept that you need all the other lines. I'll only mention two.

First every C program needs a main() line. This tells the computer to run whatever follows the main() and is between the two curly brackets "{" and "}". This will become more useful as we go on. Secondly if you think that printf() appears to do much the same as PRINT in QBasic you'd be right.

We're now going to add two more lines to help with our output viewing. Change the code in First.C to the following:

#include <stdio.h>

main()
{
    printf("This is my first C program - wahoo!!!\n");
    printf("\n\n\nPress any key to continue...");
    getchar();
    return(0);
}


First.C amended. Hope I put all the " and ; in the right place.

Now run the program by pressing Ctrl-F9 and see what happens...


Hey I can see my output this time!

Can you guess what getchar() does? That's right, it gets a char(acter)! :-) By adding these two lines we've created a prompt for the user similar to the INPUT line in QBasic, but we don't care what the user types in. As soon as any key is pressed, getchar() gets it and then we end the program. Because the program was waiting for input, it displayed the User Screen without us having to do anything.

Right, one last thing. Quit Turbo C++ by pressing Alt-X or using the File menu. Had you saved your changed First.C? If you hadn't then you would have been given a warning message asking if you wanted to save it. This will only come up if you've changed a program since it was last saved. How nice is that? :-)


Whoops! Thanks for the reminder!

O.K. so you clicked Yes and saved your program and quit Turbo C++. Well, guess what? You can still run your new program even though you are not running Turbo C++! Look in the BIN folder where you clicked TC.EXE before and you'll find four new files, FIRST.C, FIRST.BAK, FIRST.OBJ, and FIRST.EXE. Double-click on FIRST.EXE and tadah! your program runs by itself! You can transfer this file to any DOS or Windows computer and it will work the same. Amaze your friends! Well they may not be amazed yet but you'll be learning more soon, and what's more important is that you now know how to create a C program that you can use anywhere. You are a genuine C programmer! Congratulations! :-)

For Fun

Just like with every other language you are learning, there is no reason that you can't start exploring for yourself. There are several C programs included with Turbo C++ in another folder called EXAMPLES. Open them up and see what they are like. You won't understand everything straight away but that's half the fun. Run them and you'll get some ideas.

Looking at our First.C program, I'm sure you noticed all those "\n" and that they didn't seem to appear on the output - or did they? Can you guess what they do? Try adding some more, even in the middle of a word, and see what happens. In C, the backslash "\" is called the escape character and can be used to create a number of effects. Here's some more to try: \t, \b, \r. The effect of the last two may not be immeadiately obvious but make sure you have some normal characters in the printf statement before and after the escape characters and you'll figure it out.

Here's another simple program you could try :

#include <stdio.h>

main()
{
    char userName[80];

    printf("\nHello! What is your name? (Type your name, then press Enter) ");
    scanf("%s", userName);
    printf("\nHello %s!\n", userName);

    getchar();
    printf("\n\n\nPress any key to continue...");
    getchar();
    return(0);
}

If this looks familiar, it's meant to. ;-) What happens if when we run the program, we just press Enter instead of entering some characters? Why do we need the extra getchar()? Could it be the same reason? (The answer is a little tricky and also explains why I haven't tried to recreate our first "For Fun" QBasic program. It can be done but it requires a bit more work. Sometimes C's power means that simple things are harder.) Have a chat in the forum if you can't figure it out - or even if you can! :-)

Copyright (C) Jonathan Field 2004
Version 20041028Thu
Comments / Suggestions