C
Languages

C

CSC216 Chapter 2

Objectives of this Chapter:

  • Learn a few Linux Commands
  • Familiarize yourself with Vim or Emacs
  • Recognize a source file (.c)
  • Recognize an object file (.o)
  • Determine if a file is executable.
  • Run a program.

Open Powershell and SSH to (george, jane, judy, or elroy). Please do not all pick george or you will get a very slow machine.

ShellSession
PS c:\Users\myuser>  ssh s1234567@somemachine
s1234567@somemachine:~$ 

The ls, mkdir, and cd commands (cd is short for chdir). ls is short for list; it will show you the contents of a directory or folder. Since this is a new setup, there is not much in the folder now. This particular folder is called my HOME folder. It is your spot to store all your files.

ShellSession
s1234567@somemachine:~$ ls
bin  snap
s1234567@somemachine:~$

The ls command can do more, and we will cover more of it later. Since you will be creating lots of files for your courses, it would be nice to have some kind of order in your HOME folder. The best system setup found is to create a directory named courses, and then inside that you will create a directory for each course you take in the curriculum. Inside each course directory, you can create directories as needed for the course, such as quizzes, labs, projects, programs, exams, sandbox,… Creating these folders helps keep things organized so you can find things faster.

For this course, you will need a thpes folder, a labexams folder, a homework folder, and a sandbox folder. So let’s create this structure. The mkdir will allow us to create directories, and the cd will allow us to change into a directory. Create the first directory and change into the directory. Then create the course directory and change into it. Then create the folders needed for this class. There is no difference between a folder and a directory. Watch how your prompt changes.

ShellSession
s1234567@somemachine:~$ mkdir courses
s1234567@somemachine:~$ cd courses
s1234567@somemachine:~/courses$ mkdir csc216
s1234567@somemachine:~/courses$ cd csc216
s1234567@somemachine:~/courses/csc216$  

The mkdir command can take a list of arguments for directories to create.

ShellSession
s1234567@somemachine:~/courses/csc216$ mkdir thpes labexams homework sandbox
s1234567@somemachine:~/courses/csc216$ ls
homework  labexams  sandbox  thpes
s1234567@somemachine:~/courses/csc216$

The pwd is short for present working directory. If you ever wonder where you are, you can issue the command pwd and the shell (terminal) will output the full path to the directory you are in.

cd without any arguments will automatically change the pwd to your HOME directory. Then, if you issue cd -, it will change the pwd back to the last directory.

ShellSession
s1234567@somemachine:~/courses/csc216$ pwd
/home/student/s1234567/courses/csc216
s1234567@somemachine:~/courses/csc216$ cd
s1234567@somemachine:~$ pwd
/home/student/s1234567
s1234567@somemachine:~$ cd -
/home/student/s1234567/courses/csc216
s1234567@somemachine:~/courses/csc216$ pwd
/home/student/s1234567/courses/csc216
s1234567@somemachine:~/courses/csc216$ 

The Sandbox

The sandbox is where you go to try things out. Do not worry; you will not hurt the system or do any harm to your directory with what we teach you. If you have any doubts, come and visit your instructor. We will compile and link our first program in C. Change into the sandbox and make a directory named hello, then change into this directory. Then open a file named hello.c with either vim or emacs.

I will be using vim for all my demonstrations and tutorials. There are a lot of tutorials on the internet on how to use both vim and emacs. I would suggest picking one editor and sticking with it. Do not try to learn too much at once. Just the basics. Insert, delete, save, and exit.

Vim has two modes: Normal mode and Insert mode. Press ‘i’ to enter insert mode, and when you are done, press ‘Esc’ to enter Command mode. Command mode is where you can save the file and quit. I will have a Vim cheat sheet for you out on the server. From Command mode, you can type ‘:w’ to save the file and ‘:q’ to quit. To start the exercise, I will assume that I do not know where I am on the file system; ~ is a shortcut for my home directory.

ShellSession
s1234567@somemachine:~/courses$ cd ~/courses/csc216/sandbox
s1234567@somemachine:~/courses/csc216/sandbox$ mkdir hello
s1234567@somemachine:~/courses/csc216/sandbox/hello$ cd hello
s1234567@somemachine:~/courses/csc216/sandbox/hello$ vim hello.c

Your terminal will change to show the contents of the empty file; look at the bottom. It should say “hello.c”. Type the following and save and quit. Press i to enter insert mode and Esc to return to Command mode. Remember :w is save and :q is quit.

C
#include <stdio.h>

int main( )
{
    printf( "Hello World!!\n\n" );
    printf( "This is fun!\n" );
    
    return 0;
}

After saving and quitting vim, you should be back at the terminal. If you issue the command ls, you should see the file name hello.c in the listing. cat is another command that is short for concatenate. If you issue the command ‘cat hello.c’ you will see the contents of the file being shown on the terminal.

ShellSession
s1234567@somemachine:~/courses/csc216/sandbox/hello$ cat hello.c
#include <stdio.h>

int main( )
{
    printf( "Hello World!!\n\n" );
    printf( "This is fun!\n" );

    return 0;
}

Compiling

To compile your program to an object file, the program name gcc will be used. This program expects the name of the input file to be provided “hello.c”, the name of the output “-o hello.o”, and the option to compile (“- c “). The entire command will look like this: gcc -o hello.o -c hello.c It is important to note that the name of the output file must come after the -o. If there were no errors, no output will be produced. The command will produce a file named hello.o; this is an object file.

ShellSession
s1234567@somemachine:~/courses/csc216/sandbox/hello$ gcc -o hello.o -c hello.c
s1234567@somemachine:~/courses/csc216/sandbox/hello$ ls
hello.c  hello.o
s1234567@somemachine:~/courses/csc216/sandbox/hello$ 

Linking

Now that you have an object file, you can link it with the libraries necessary to run your instructions and produce the executable. The gcc program will be used for this also, and the command is almost identical to compiling. The input file will be “hello.o”, the output “-o hello” note that it has no extension. The entire command will look like this: gcc -o hello hello.o. If the link was successful, no output will be produced. Order usually does not matter. When you include libraries, it does.

ShellSession
s1234567@somemachine:~/courses/csc216/sandbox/hello$ gcc -o hello hello.o
s1234567@somemachine:~/courses/csc216/sandbox/hello$ gcc hello.o -o hello
s1234567@somemachine:~/courses/csc216/sandbox/hello$ ls
hello  hello.c  hello.o

Execution

The program has been compiled and linked. To execute the program, just type the name at the prompt. ls, mkdir, cat, chdir, vim, emacs, gcc are all programs that you have been executing.

ShellSession
s1234567@somemachine:~/courses/csc216/sandbox/hello$ hello
hello: command not found

You tried that, and it failed. The Operating system couldn’t find the program. This is because your program is not in the search path. If you issue the command echo $PATH, you will see many paths that it searches for the program hello. If the program is not found, it outputs the error message. To get around this error, precede the name of the program with a ‘./’.

ShellSession
s1234567@somemachine:~/courses/csc216/sandbox/hello$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
s1234567@somemachine:~/courses/csc216/sandbox/hello$ ./hello
Hello World!!

This is fun!

The last commands to be introduced are file and ldd. Both are useful as a developer. If you invoke the command file and give it the name of a file, it will give you information about the file. If you invoke the command ldd and give it the name of an executable file, it will let you know what libraries are linked to it.

ShellSession
s1234567@blackhand:~/courses/csc216/sandbox/hello$ file hello.c
hello.c: C source, ASCII text

s1234567@blackhand:~/courses/csc216/sandbox/hello$ file hello.o
hello.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

s1234567@blackhand:~/courses/csc216/sandbox/hello$ file hello
hello: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=05777742b4598bd6144605c0e09ca6a91fe8324e, for GNU/Linux 3.2.0, not stripped


s1234567@blackhand:~/courses/csc216/sandbox/hello$ ldd hello.c
        not a dynamic executable
        
s1234567@jane:~/courses/csc216/sandbox/hello$ ldd hello.o
        not a dynamic executable
        
s1234567@jane:~/courses/csc216/sandbox/hello$ ldd hello
        linux-vdso.so.1 (0x0000ffffb8e1c000)
        libc.so.6 => /usr/lib/aarch64-linux-gnu/libc.so.6 (0x0000ffffb8c00000)
        /lib/ld-linux-aarch64.so.1 (0x0000ffffb8e20000)
s1234567@jane:~/courses/csc216/sandbox/hello$

Commands To Become Familiar With

  • ls
  • cd
  • mkdir – rmdir
  • pwd
  • $HOME
  • $PATH
  • gcc – compiling and linking
  • file
  • cat
  • echo

Comments

There are two types of comments: multi-line and inline. Multi-line comments start with a /* and ignore everything spanning lines until it finds the closing */. I never use these inside functions because you cannot nest them, and it makes debugging harder when it crashes. An inline comment begins with a // and ignores everything until the end of the line. I line these comments up with the line they are commenting. Use comments to explain your algorithm. If you comment what you want to do before you code the function, it makes it easier. All functions must be documented.

C
/* this is my first program that was used to
   demonstrate compiling and linking.
   This is a multiple line comment.
 */
#include <stdio.h>

int main( )
{
    // this is a inline comment
    // output a couple lines to the screen
    printf( "Hello World!!\n\n" );
    printf( "This is fun!\n" );

    // return 0 since it ran successfully.
    return 0;
}

What the code does.

C
/* this is my first program that was used to
   demonstrate compiling and linking.
   This is a multiple line comment.
 */
// Includes a library for common input and output tasks
#include <stdio.h>                             

int main( )

    // Say hello to the world with two ! and two new lines
    printf( "Hello World!!\n\n" );
    
    // output our mood
    printf( "This is fun!\n" );

    // return 0 since it ran successfully.
    return 0;
}