Tuesday 15 October 2013

C Programs in Linux

Writing and Compiling C Programs in Linux.
Contents
1 File Management in Linux 1
1.1 The Command Line Prompt
1.2 Getting Help in Linux 
1.3 Viewing the Current Directory
1.4 Viewing the Files and Sub-directories of the Current Directory
1.5 Changing the Current Directory
1.6 Creating an Empty File on Disk 
1.7 Deleting a File on Disk 
1.8 Creating a Directory on Disk 
1.9 Deleting a Directory 
1.10 The Midnight Commander Navigation Program 
2 Editing and Viewing Text Files
3 Compiling and Running C Programs 
4 Exercises

1 File Management in Linux
The commands that will be described are meant to be executed from a command line terminal.
In order to open a terminal, access from the menu: Applications → Accessories → Terminal.
You will see a window opening, with a command line prompt where you can type commands.

1.1 The Command Line Prompt
The command line prompt that shows up in the terminal window contains useful information.
It has the form:
murphy@murphy-laptop:~/tp$
There are three parts in the command line prompt, separated by @ : and $
murphy @ murphy-laptop : ~/tp $
The first part before the @ shows the user which is logged in at the moment. In our example
it is the user murphy. The second part between the @ and the : shows the name of the computer
you are working on. In our example it is murphy-laptop. The third part, between the : and the
$ shows the current directory you are working in. In our example it is ~/tp (~ is a shortcut for
the user’s home directory; ~/tp is equivalent to /home/murphy/tp). When you will change the
current directory, you will see the third part of the command line prompt changing.
1Exercise Identify the three parts of the command prompt on the computer you are working
on. What is the name of the logged in user? What is the name of the computer? Which is the
current directory immediately after opening up the terminal window?

1.2 Getting Help in Linux
Whenever you need help about commands in Linux, you can read the man pages, which contain
detailed information about commands and their options. To see the man page about a command,
type
man <command name>
Information about the command will appear on the screen. You can move up and down
using the arrow keys. In order to exit from the man page and return to the command line type
the letter q.
Exercise Type man man to read information about the man command itself.

1.3 Viewing the Current Directory
At any time, in order to see the current directory you can type
pwd
The letters come from print name of current/working directory.
Example
murphy@murphy-laptop:~/tp$ pwd
/home/murphy/tp
Exercise Type pwd and see the full path of the current directory.

1.4 Viewing the Files and Sub-directories of the Current Directory
In order to see the list of files and directories that are found in the current directory, type
ls
The name of the command comes from list directory contents.
Example
murphy@murphy-laptop:~/tp$ ls
laborator01 test.c
Two names are listed: laborator01 and test.c. Usually they are printed with colors, and
based on the color we can tell which are files and which are directories.
In order to see more details about the files and directories, type
ls -l
murphy@murphy-laptop:~/tp$ ls -l
total 4
drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01
-rw-r--r-- 1 murphy murphy 0 2009-02-17 23:42 test.cThe same two entries are listed, but with more information next to each of them. We can
see access rights, time of last modification, owner, etc. What is the most important for the
moment is the first letter in each line. If the first letter in a line is d, then the name on that
line is a directory. If the first letter is - then the name on that line is a file.
Exercise Type ls to see the content of the current directory. Then type ls -l. Identify the
files and the directories based on the first letter in each line. Then determine which color shows
directories in the short form of ls, and which colors show files.

1.5 Changing the Current Directory
In order to change the current directory, type
cd <new path on disk>
The letters come from change the current directory. You must specify the path of the new
current directory.
Example For changing the current directory to the sub-directory laborator01 you type
murphy@murphy-laptop:~/tp$ cd laborator01
murphy@murphy-laptop:~/tp/laborator01$ pwd
/home/murphy/tp/laborator01
For going one level up, to the parent directory, you type
murphy@murphy-laptop:~/tp/laborator01$ cd ..
murphy@murphy-laptop:~/tp$ pwd
/home/murphy/tp
The root directory is /
murphy@murphy-laptop:~/tp$ cd /
murphy@murphy-laptop:/$ pwd
/
An absolute path can also be specified, starting from the root directory:
murphy@murphy-laptop:/$ cd /home/murphy/tp
murphy@murphy-laptop:~/tp$ pwd
/home/murphy/tp

1.6 Creating an Empty File on Disk
In general the text editors are able to create empty files on disk. However, sometimes it is useful
to create an empty file directly from the command line. In order to do it, type:
touch <file name>
An empty file with the specified name will be created.
If a file with that name already exists on disk, it will remain there and moment of last
update is changed in the properties of the file.Example Watch the date shown next to the test.c file in the files listing. After executing
touch upon the file, the date will change:
murphy@murphy-laptop:~/tp$ ls -l
total 4
drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01
-rw-r--r-- 1 murphy murphy 0 2009-02-17 23:42 test.c
murphy@murphy-laptop:~/tp$ touch test.c
murphy@murphy-laptop:~/tp$ ls -l
total 4
drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01
-rw-r--r-- 1 murphy murphy 0 2009-02-18 00:00 test.c
Note the appearance of the new file exemplu.c after running the touch command:
murphy@murphy-laptop:~/tp$ touch exemplu.c
murphy@murphy-laptop:~/tp$ ls -l
total 4
-rw-r--r-- 1 murphy murphy 0 2009-02-18 00:00 exemplu.c
drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01
-rw-r--r-- 1 murphy murphy 0 2009-02-18 00:00 test.c
Exercise Create an empty file named file.txt in your home folder. Type ls -l and see the
last modification time of the file. Wait a minute or so and run touch on the file. See again the
last modification time and notice the change.

1.7 Deleting a File on Disk
It is done through
rm <file name>
The letters come from remove files or directories.
Example In order to delete the test.c file, we type:
murphy@murphy-laptop:~/tp$ rm test.c
murphy@murphy-laptop:~/tp$ ls -l
total 4
-rw-r--r-- 1 murphy murphy 0 2009-02-18 00:00 exemplu.c
drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01
Exercise Delete the file test.txt created earlier.

1.8 Creating a Directory on Disk
A new directory is created with the command
mkdir <directory name>
The letters come from make directories.Example In order to create the directory laborator02 we type:
murphy@murphy-laptop:~/tp$ mkdir laborator02
murphy@murphy-laptop:~/tp$ ls -l
total 8
-rw-r--r-- 1 murphy murphy 0 2009-02-18 00:00 exemplu.c
drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01
drwxr-xr-x 2 murphy murphy 4096 2009-02-18 00:07 laborator02
Exercise Create a directory named test.dir in your home folder. Change the current direc-
tory to it. Type pwd to view the current directory.

1.9 Deleting a Directory
In order to delete a directory, type
rmdir <directory name>
or
rm -r <directory name>
The first command deleted only empty directories. If the directory contains any file or
sub-directory, a warning will be displayed and the directory will not be deleted. The second
command deletes the folder, recursively, together with its files and sub-directories. Use with
care!
Example In the directory laborator02 we have created other files and sub-directories. First
we try to delete the directory laborator02 using rmdir, then using rm -r. Notice the warning
message issued after we try the first command.
murphy@murphy-laptop:~/tp$ rmdir laborator02
rmdir: failed to remove ‘laborator02’: Directory not empty
murphy@murphy-laptop:~/tp$ rm -r laborator02
murphy@murphy-laptop:~/tp$ ls -l
total 4
-rw-r--r-- 1 murphy murphy 0 2009-02-18 00:00 exemplu.c
drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01
1.10 The Midnight Commander Navigation Program
It’s a similar program with Total Commander from Windows, but with text interface. You can
start it by typing:
mc
It allows you to visually navigate through the hierarchy of directories and files, to create
and delete directories and files, etc. It is a more user friendly alternative to the command line.

2 Editing and Viewing Text Files
Linux offers several programs for editing and viewing text files. There are text editors with
plain text interface, or with graphical interface.
On the computers in the laboratory, you can find installed: emacs, vi, nano, gedit, and
mcedit, the editor of mc.
In order to view the content of a file, you can either open the file in one of the editors, or
you can type in a terminal one of the commands:cat <file name>
or
less <file name>
The first command prints the content of the file on the screen, and then returns the control
to the command line. It is useful for small files.
The second command prints the content of the file and allows you to navigate up and down
using the arrow keys. It is useful for large files. In order to quit and return to the command
line, type the letter q.

3 Compiling and Running C Programs
We will compile C programs from the command line. We will use the gcc compiler. You can
find details about it on the website http://gcc.gnu.org/ or in its man page (man gcc).
For starters we will use the compiler in a simple form:
gcc -Wall -o <output executable file> <input C file>
The result of running the command is that the input C file will be compiled and linked,
and the output binary file will have the name specified by us. The -Wall option instructs the
compiler to print out all warnings about our source code.
Example Suppose we have a program stored in a file test.c:
#include <stdio.h>
int main(void)
{
printf("Will it compile?\n");
return 0;
}
The content of the current directory is:
murphy@murphy-laptop:~/tp$ ls -l
total 8
drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01
-rw-r--r-- 1 murphy murphy 85 2009-02-18 00:22 test.c
We compile the file:
murphy@murphy-laptop:~/tp$ gcc -Wall -o test test.c
If the file would have errors, they would be printed on the screen. If nothing is printed, it
means the file was compiled and linked successfully. Now the current directory contains:
murphy@murphy-laptop:~/tp$ ls -l
total 16
drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01
-rwxr-xr-x 1 murphy murphy 6369 2009-02-18 00:24 test
-rw-r--r-- 1 murphy murphy 85 2009-02-18 00:22 test.c
The test file appeared, which is our output executable file. In Linux executable programs
don’t require the .exe extension. In order to run the program we have just compiled, we type:murphy@murphy-laptop:~/tp$ ./test
Will it compile?
The ./ characters are important when we are running programs from the current directory.
We can see that the program ran successfully, and our message got printed.

4 Exercises
1. Create the following hierarchy of directories in your home directory:
~/test/test1/test11
~/test/test1/test12
~/test/test2/test21
~/test/test2/test22
(a) without using the cd command
(b) using the cd command
2. Delete the directory test21, having as current directory test
(a) using a relative path
(b) using an absolute path
3. List the contents of the directory test and its subdirectories.
4. Delete the directory test with all its subdirectories.
5. Perform the above operations from mc.
6. Write a C program that tells the user to think about a number between 1 and 100 and then
tries to guess the number by asking questions of the form “Is your number greater than
...?” and “Is your number smaller than ...?”. Save the program in a file called guess.c.
Compile and link the file with gcc. Run the program.


No comments:

Post a Comment