Warning
This page is located in archive.

Lab01 - Introduction to the Computer Laboratory and C Programming Language

  • Introduction to the computer environment - password settings
  • Introduction to the working environment - terminal control and elementary commands: ls, cd, pwd, mkdir, cp, mv, rm, touch, and man, cat, find, grep, wget, unzip, echo, and input/output modificators: |, >, ».
  • Compile the first program
  • Test the executed program
  • Become familiar with your selected favorite editor for editing sources, e.g., gedit, Sublime Text, Emacs, Vim.

Lab Code

Exercise 1

Simple program to output text:

#include <stdio.h>

int main()
{
    //this line is a comment, it will be ignored
    printf("Hello World!");
    return 0;
}

Practice compiling it:

gcc myprogram.c

Gcc will take your code and produce a binary executable from it. See if you can figure out:

How to customise your executable filename from gcc. Make it format correctly in the terminal.

Exercise 2

We can use printf(“here is an integer: %i”, myint); to format an integer. Similarly %f to format a float.

We can use scanf(“%f”, &fnum); to get a variable from the command line user.

Implement a function that calculates the euclidean distance of two numbers. That is, it takes two numbers, squares each of them, adds them, and returns the square root.

Basic terminal programs Help

Open terminal Ctrl + Alt + T New tab Ctrl + Shift + T End programs by running Ctrl+C Close by Ctrl + D

man

Manual to (almost) every terminal program can be found using man:

$ man <program>

You shoud start working in linux terminal by asking, how to use manual

$ man man

pwd

To find out, where are you in the directory tree, the present working directory, use pwd:

$ pwd

ls

To list files in your directory, use ls:

$ ls

There is also possibility to see a content of different directory

$ ls <relative/absolute address>

The useful parameters you should know are:

  • -a do not ignore entries starting with ., usualy hidden files,
  • -A the same as -a but it does not list implied . and ..,
  • -l uses a long listing format,
  • -p to append / at the end of the name of directories to differentiate them from files,
  • you can also sort them by -S size, -t time, etc.,
  • and combine them
$ ls -Al

cd

To change the directory use cd:

$ cd <relative/absolute address>

Go to the parent directory

$ cd ..

Go to the parent's parent directory

$ cd ../../

Go to the parent's parent directory and then to its child directory

$ cd ../../some_directory/

Go to the child's child directory

$ some_directory/some_subdirectory/

You can use absolute address:

$ /home/my_account/my_files/my_video/

There is also trivial way to your home directory:

$ cd

The manual is not presented, therefore use parameter –help to find out more

$ cd --help

For fast navigation, use keypad Tab. Use it frequently.

mkdir and touch

If you want to create a directory, use mkdir:

$ mkdir <directory name>

and touch for file creation:

$ touch <file name>

Please, do not use spaces in your names:

$ mkdir "I have come here from some advertising environment called Windows"
$ mkdir and\ I\ really\ like\ to\ troll

and if necessary, use _ underscore instead:

$ touch I_love_linux 

mv

Use mv for moving files or directories from one position in a directory tree to another:

$ mv <source> <destination>
$ mv random_file.txt ~/random_directory/
$ mv random_directory/ ~/some_directory/some_subdirectory/

Note that character ~ stands for your home directory.

Use mv also for renaming the files:

$ mv obsolete_name much_better_name

cp

Copying files can be done using cp:

$ cp <source> <destination>
$ cp random_file.txt ~/random_directory/
$ cp random_file.txt ~/random_directory/better_name.txt

To copy whole directory, use -r recursive option:

$ cp -r random_directory/ ~/other_directory/some_subdirectory/

rm

You can delete file using rm

$ rm <file name>

Beware! It is very hard to undo!

Deleting whole directory and its content needs -r recursive option:

$ rm -r obsolete_directory/

It is also possible to delete file (or directory) at different position in directory tree:

$ rm ~/random_directory/random_file.txt
$ rm ../../some_system_directory/some_essential_file
courses/be5b99cpl/labs/lab01.txt · Last modified: 2023/09/27 11:27 by ulricji1