CS19001/CS19002 Programming and Data Structures Laboratory | Autumn/Spring semester |
Getting started
The basic cycle
- Switch on your monitor.
- Switch on your PC.
- Allow the machine to boot. Wait till the login prompt comes.
- Supply your login and password:
Login: <sec><nn> Password: <sec><nn>Here <sec> is the letter denoting your section (A--J) and <nn> is the number of your PC. For example, PC 47 in Section 3 should supply c47 as both login and password. This opens your window manager (usually KDE) with icons, the bottom panel and so on. You are now ready to start your work.- Click on the terminal icon to open a shell (command prompt).
- Edit your program (new or already existing) by an editor. We recommend using the emacs editor. This is how you can run emacs:
emacs myprog.c &(The ampersand in the last command was not necessary, but is helpful, because it runs emacs in the background and the shell is free to listen to your other commands.)- Write your program in the editor and save it.
- Go to the shell and compile your program:
cc myprog.cIf compilation is successful, an executable called a.out will be created.- Run your program:
./a.out- Continue your edit-compile-debug-run-debug-print work.
...
Finally, it's pretty late. It's time to pack up. Wait! You still have some more things to do, before you rush for your lunch/snacks.
- Close all the windows you opened.
- Logout from your window manager. This leaves you again in the login console.
- Select the item to shut down the machine. Wait till the machine completely shuts down.
- Switch off your monitor.
- You can now leave. Don't remember later that you have forgot to sign the attendance sheet. You may not rely on our memory to certify in the next week that you were present in this lab session.
Some useful Unix commands
Here is a short list of useful commands that you can use from your shell (in addition to running emacs and compiling and running your program).
- Create a directory:
mkdir progs- Go to a new directory:
cd progs/Every directory provides you access to two directories: . (the current directory) and .. (the parent directopry).- View a list of all files in a directory:
ls -lFNote that ls -F, ls -l, ls -al, ls -alF etc and even simply ls also work. Try out and find the differences. In fact, the ls takes a lot of other options.- View the content of a file:
cat filenameIf the file is too big to fit in a single screen, use either the more or the less command.cat filename | lessSome commands that can be used during less viewing include:
Command Description Space/Control-F Scroll down by one page Control-B Scroll up by one page Control-D Scroll down by half page Control-U Scroll up by half page Return/Down Scroll down by one line Up Scroll up by one line g Go to the first page (top) G Go to the last page (bottom) q Quit - Copy one file to another:
cp file1.c file2.c cp file2.c mydir/file3.c- Move a file to another:
mv file1.c file2.c mv file2.c mydir/ cd mydir/ mv file2.c ../file1.c cd ../- Delete a file:
rm filenameNote: A deleted (by rm) or overwritten (by mv) file is lost for ever. In no way you can get it back. Be very, very careful, before running these commands.
On-line help
- Click on your browser icon and run either mozilla (also known as netscape) or konqueror.
- Visit the following URL:
http://www.facweb.iitkgp.ernet.in/~pds/
You may bookmark this link in your browser.
Your first C program
The following program prints Hello world! and terminates. They said it in the Bible that whenever you start learning programming, you must first write this program. And nothing else.#include <stdio.h> main () { printf("Hello world!\n"); }Complete your edit-save-compile-debug-run cycle on this code. Though it is not written in the Bible, we guarantee that none of your future programs will be as easy as this. So quickly get habituated to this programming process of Unix. (Somebody of you may have experience with an integrated development environment (IDE) like TURBO C or Microsoft VC++. In Unix such an environment is generally missing. But that's not a big issue, as you will understand soon.)
The second C program
Now it's a more complicated piece of code. For the time being, blindly (but with your eyes open) type this code and run. Just get acquainted to the process. And, of course, save this second program in a new file.#include <stdio.h> char name[100]; int i; main () { printf("Hello, may I know your full name ? "); scanf("%s",name); printf("Welcome %s.\n",name); printf("Your name printed backward is : "); for (i=strlen(name)-1; i>=0; i--) printf("%c",name[i]); printf("\n"); }Have you noticed something bad during the execution? You may now try the following:#include <stdio.h> char name[100]; int i; main () { printf("Hello, may I know your full name ? "); fgets(name,100,stdin); name[strlen(name)-1] = '\0'; printf("Welcome %s.\n",name); printf("Your name printed backward is : "); for (i=strlen(name)-1; i>=0; i--) printf("%c",name[i]); printf("\n"); }OOPS! It's not so easy then. Don't worry. The entire semester is waiting.