Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Lab01 - Introduction to the Computer Laboratory and C Programming Language

  • Introduction to the computer environment - password settings and eventually ownCloud setup
  • 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: |, >, ».
  • Download sources using wget from the 1st lecture lab1.zip and unzip.
  • Compile the first program printf.c

clang -Wall -pedantic -std=c99 printf.c -o main

  • Test the executed the program as in the lecture example
  • Become familiar with your selected favorite editor for editing sources, e.g., gedit, Sublime Text, Emacs, Vim.

Lab tasks

  • Modify scanf.c to accept two numbers, add then together, and print the result
  • Rewrite the code to accept two numbers with a decimal part (float)
  • Modify var.c to print out what the filename is (and also the first command line parameter)

Further tasks

Implement programs in exercises and programming Projects of Chapters 2 and 3 of the K.N. King textbook.

Further reading

During presentation, professor Krajník recommended you some books. You should buy them in your favourite bookstore. You should not use your new gained skills and download some pirated pdf using for example:

$ wget http://datasets.chronorobotics.tk/s/S8kZk5fhIANDKBu/download -O cpl_recommended_book.pdf

.

basic terminal programs

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, 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

other programs

Please, read more about recommended terminal programs. Start with:

$ man <program>

or

$ <program> --help

Tmux

Peolple havily working in the terminal have to organize their work. One of very useful tools is a terminal multiplexer tmux.

Tmux is a tool run in the command line that allows you to do several important things. They are that you can have multiple terminals running inside one terminal, and also that you can disconnect and then reconnect to a termainal session. They can also be shared by many people.

To install tmux in Ubuntu, use a command:

$ sudo apt install tmux

Make sure, you have a root password. (It is not possible to install anything on school computers using your accounts.)

To start a tmux session, just run tmux in a command line.

Once you are inside a tmux session, to split the window into two panes, you can run tmux split-window, or for brevity, just tmux spl, to have one pane on top of the other. For them being side by side, add the flat -h (horizontal). You can do this repeatedly to have many panes. To remove one, its just like a normal terminal, so either type exit or Ctrl-D. Closing the last pane will end the session.

To move between panes, press Ctrl-b, release, and then use the arrow keys.

To disconnect from a session, use Ctrl-b, release, and then press d. Note that if you don't release the Ctrl-b, this will adjust the size of the panes instead.

To reconnect, use tmux attach.

To open a new window, use Ctrl-b, release, and then press c. You will see all your windows numbered across the bottom. To move between them, use Ctrl-b, release, and then press the number corresponding to that window.

To scroll inside a window, use Ctrl-b, release, and then press [. Use Ctrl-c to go back to the normal terminal mode.

If you want to know more beginner's stuff, try this guide.

If you are not interrested in learning, try babtism by fire.

Vim

There is a plenty of text editors with different approaches and sets of tools. One of the most favourite ones is Vim. Vim is terminal-based text editor useful for programming on remote machines or through SSH onto a robot.


To install Vim in Ubuntu, type this command:

$ sudo apt install vim

To start editing a file, use:

$ vim <file name>

It is also possible to create and start editing a new text file:

$ vim new_file.txt

Vim has three different modes: normal (or command), typing, and visual.

You enter the insert mode by pressing 'i'. Then you can type normally onto the text. Go back to normal mode with esc. From normal mode, you can, for example, search through text by pressing / and then typing.

To quit, make sure you're in normal mode (press esc), and then type :q. To save your changes :w. You can combine these using :wq, or quit without saving by :q!.

There is a huge amount of commands that helps experienced programmer to code efficiently. But Vim is also known for its steep learning curve. Before your final decision whether to code like a grown man or to stick with gedit, go through few links or read blogs from some enthusiasts.

Before using, we would recommend creating a file in your home folder called .vimrc, and adding this to it to add some nice defaults:

set number
set autoindent
set expandtab
set tabstop=4
set shiftwidth=4
syntax enable
set encoding=utf-8

"alternatively, you can try these

"status bar
set laststatus=2

"highlight search
set hlsearch

"turn off hlsearch by pressing backslash
nnoremap <Bslash> :noh<CR><C-L>
  
"distance to editing line
set relativenumber

Do you feel overwhelmed? Try this game!

If you do not like reading manuals, try cheatsheet!

courses/be5b99cpl/labs/lab01.txt · Last modified: 2021/09/22 15:10 by vintrtom