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

1. Integer sum function

Compile with gcc filename.c -o outputFilename

#include <stdio.h>


int sum(int a,int b)
{
	return (a + b);
}

int main()
{
	int myAge = 29;

	int a =10;
	int b =20;
	
	int result = sum(a,b);

	printf("My name is George, and I am %i years old.\n", myAge);
	printf("My sum is%i \n", result);

	return 0;
}

2. Float sum function

Compile with gcc filename.c -o outputFilename

#include <stdio.h>


float sum(float a,float b)
{
	return (a + b);
}

int main()
{
	int myAge = 29;

	float a =10.1;
	float b =20;
	
	float result = sum(a,b);

	printf("My name is George, and I am %i years old.\n", myAge);
	printf("My sum is%.3f \n", result);

	return 0;
}

3. Square root function Important! Compile with gcc filename.c -o myoutput -lm

#include <stdio.h>
#include <math.h>

float getRoot(float a)
{
    //this function calculates the square root, and returns it
    float result = sqrt(a);
	return a;
}

int main()
{
	float myAge = 29;
    float rootAge = sqrt(myAge);

	printf("My name is George, and I am %f years old.\n", myAge);
	printf("The square root of that is %.3f \n", rootAge);

	return 0;
}

Further tasks

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: 2022/09/21 13:15 by brouggeo