CS13002 Programming and Data Structures | Section: 3/C, Spring 2003--2004 |
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: c<nn> Password: u<nn>Here <nn> is the number of your PC. 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 &(Note that the ampersand in the last command was not necessary, but is helpful in the sense that 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.outYou may have to give the command ./a.out in case you are issued a message like a.out: Command not found.- 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.
- 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 filename- 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://facweb.iitkgp.ernet.in/~adas/course/lab/PDS/Spring04/
You may bookmark this link in your browser.
Your first C program
The following program prints Hello world! and terminates. They said 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 environment 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.