In this class we will interact with UNIX and Linux primarily through the command line. The command line, or shell, is very simple: it prompts you to type a command, then prints the output of the command (if any), then goes back to the prompt (like the shampoo).
At first, navigating the file system through the command line will be
confusing. The two most important commands if you feel lost are ls
and pwd
.
In UNIX there are no C: or A:, there is only one filesystem. The very top-level
directory is /
. Physical disks are mounted at certain locations in the
filesystem tree. For our needs, we won't worry about this. We will work out of
our home directories, aka ~
or $HOME
. You will create subdirectories for
your projects, e.g. ~/lab0/
, then edit and compile your programs there.
Here are the most important UNIX commands. You should be able to do everything
using these commands, but feel free to explore other commands. Most commands
will print out a brief help screen with the -h
or --help
options. All
commands have more in-depth man pages which you can read by typing man
followed by the command name.
ls
list filespwd
print current working directorycp
copy filesmv
move filesrm
remove filesln
link filescd
change directorymkdir
create directoryrmdir
remove directorycat
view filesless
page through fileshead
view the beginningtail
view the endnl
number linesod
view binary dataxxd
view binary datastat
display file attributeswc
count bytes/words/linesdu
measure disk usagefile
identify file typeschmod
change file permissionsgzip
compress files (GNU Zip) zip
compress files (Windows Zip) tar
work with tarballscc
or gcc
Now work through project 0.
This is an excerpt of the major points of chapter 1 of "The C Programming Language" (K&R). The chapter and these notes are an overview, we won't go into all the details of these things here.
C programs consist of functions and variables.
Hello world:
#include <stdio.h>
main()
{
printf("hello, world\n");
}
#include <stdio.h>
means include the standard input/output header. A header is a file that tells the compiler what functions are available.main
is a function. The special name main
is a special function: the one that runs when you run your program.printf
is a function from the stdio library (which is why we did #include <stdio.h>
), it prints to the console."hello, world\n"
is a string. \n
means print a newlineAs in algebra, variables are placeholders for values. In C variables have a type, e.g. int or float. (In-class excercise, write the fahrenheit-celsius table program.)
Introduce if
and while
Introduce stdin
and stdout
, getchar()
and putchar()
Write the line counting program in class. Discuss printf("%d\n",nl)
Overview arrays and functions