Using the Shell
- Use your ssh client to connect to a
CSE/ECE Sun
- Perhaps just use sunlab.cse.lehigh.edu
- On our Suns (and Linux) your default shell will be bash
(Bourne-Again Shell). Modern shells, including bash, offer:
- tab completion -- just type the first few letters and press tab to
complete the command name
- command history -- use the up and down arrows to select from your
history of past commands
- emacs-like keybindings -- C-a, C-e, C-t, C-b, C-f, C-p, C-n all same
- When finished with the shell, type exit. Some shells will
also allow CTRL-D (EOF) to signal the end of the session.
I use tcsh instead of bash because that is what I learned; again, like editors,
shell choice is often a 'religious' issue.
Comparison to DOS/Windows shell
- Some ideas are similar, such as a file structure of nested folders
- Differences
- UNIX filenames are case-sensitive, so "hello.txt" and "Hello.txt" are
not the same.
- UNIX pathnames use a slash (/) as a separator, not a backslash (\) as in
DOS/Windows. The backslash is used to escape special characters just like in
most programming languages.
- UNIX has no "drive names" like A, C, D. Instead, the entire filesystem is
mapped under a single root directory "/"
- Often the current directory is not in user's execution path by default.
Need, for example, ./progname
- All files and directories belong to some user, and have access permissions.
- There is a special "root" account for system administration (separate
from regular users), which
can read and write essentially anything, anywhere, regardless of access
permissions.
UNIX Commands 1
- Where are you?
- Who are you?
- Moving to another directory
io:~% cd cse271.131/
io:~/cse271.131% cd ..
io:~% pwd
/home/bdd3
io:~% cd .
io:~% cd
io:~% cd ~
io:~% cd /home/bdd3
io:~%
PWD=Print Working Directory
CD=Change Directory
UNIX Commands 2
- List directory contents
io:~/cse271.131% ls
hw1/
io:~/cse271.131% ls -l
total 2
drwxr-xr-x 2 bdd3 ugrad 512 Jan 19 01:16 hw1/
io:~/cse271.131% ls ..
Desktop/ cse271.131/ nsmail/ test.c
Mail/ mail/ test* test.c~
- Deleting files
Be careful about rm -rf!
LS=LiSt (files or contents of a directory)
RM=ReMove
Viewing Files
You can always open a text file in an editor to view it. However, there are
other ways --
cat,
less, and
more are typical
choices.
tritan 2% cat -n test.c
1 int a;
2 int main (sdfsdf a)
3 {
4 int b;
5 }
tritan 2%
less and
more are called
pagers -- they display a
large file, one page (screenful) at a time.
C Programming
- There are many more useful UNIX utilities that we will discuss
in the future, but let's consider building one.
- We saw that cat can display the contents of a
file.
- Let's write our own version in class. Since cat reads from files,
we'll need to use the open(2), read(2), and close(2) system calls.
- Note that the man system also has entries for system calls and
standard library functions as well as for programs.
Note the man page sections for the system calls.
Use man -s to specify which section of the manual pages to
search.
C versus C++
- C++ is mostly a superset of C
- So what are the major differences?
Do the students know?
Major C differences from C++
- No classes or objects -- all code is in functions
- C structures cannot have methods
- I/O in C is based on library functions
- No function overloading
- No new or delete (use library functions instead)
- No reference variables (aliases)
Other Differences
- In old versions of C, variables had to be declared at the beginning of a
block, typically at the beginning of the function
- No bool datatype
- No << and >> operators for I/O
- Cannot substitute and, or, and not for boolean
operators &&, ||, and !
- Different approach to strings
So what stays the same? Glad you asked...
Basic C Data Types
- Pretty much the same as in C++
- int (%d, -40), short int, long int, long long int, unsigned int
- float (%f, 3.14), double, long double
- char (%c, 'a'), unsigned char, signed char
- Variable names are also similar to C++
- made of letters, digits, underscore
- cannot start with a number
int is signed, 32 bits on typical (but not all) platforms. float typically 32bits, double typically 64, long double 128
Operators
As always, multiplication and division have precedence over addition/subtraction; use parenthesis to make things clear
Assignment Operators
- Assignment = (e.g., a = 4;)
- Modify and assign
- Increment ++ and Decrement -- (e.g., a++; --b;)
- Combinations *=, /=, %=, +=, -=, &=, ^=, |=, <<=, >>=
a += 4;
b <<= 1;
Type Conversions
- C permits (and performs) many kinds of type conversions
- e.g., implicitly (and explicitly) within assignments
- includes automatic type promotion and demotion
int i = 4.3;
float f = 3;
int g = 0;
int h = 0;
f = 3.7;
g = i + f + .4;
h = (int) i + (int) f + (int) .4;
-Wall will warn you about some conversions if they are
not explicitly cast (but not those above)
Type Conversions continued
Quick test: what is the result of 1.0 + 3/2?
Control Flow
while (expression)
statement
for (expr1; expr2; expr3)
statement
do
statement
while (expression);
The
break statement drops control out of the innermost loop while
continue moves on to the next iteration.
Control Flow
if (expression)
statement1
else
statement2
switch (expression) {
case const-expression: statements
case const-expression: statements
default: statements
}