UNIX Redirection, C Basics
Prof. Brian D. Davison
Computer Science & Engineering, Lehigh University
Redirection
- One of the great features of UNIX is the ability to connect multiple programs that read from stdin and stdout.
- cat, for example, writes to standard out
- In UNIX, stdin and stdout (and stderr) can be redirected.
- "ls -l > ls-l.txt" will take the output of "ls -l" and write it to a file called ls-l.txt
- "more < file1" will use file1 as the standard input to more
- "prog1 >> file1" will append prog1 stdout to file1 (not replace)
- "prog1 2> errorfile" will only write stderr messages to errorfile
- "prog1 &> bothfile" will send both stdout and stderr to bothfile
- "sort < numfile > sortednums" redirects both stdin and stdout
Pipes
- Programs can be chained together using "pipes"
- "cat longfile | wc" sends the output of "cat longfile" to the input of "wc"
- "cat numfile | sort > sortednums" sends the output of "cat numfile" to the input of "sort", whose output is written to "sortednums"
- A more complex (realistic) example:
cat airweb-access_log airweb-access_log.2005Jan* | grep -v airweb |
cut -d' ' -f11 | sort | uniq -c | sort -nr | more
Takes web server logs with entries such as:
192.114.107.4 - - [23/Jan/2005:09:20:10 -0500] "GET / HTTP/1.1" 200 3515
"http://www2005.org/tutorials/accept.html" "Mozilla/4.0 (compatible; MSIE
6.0; Windows NT 5.0; .NET CLR 1.0.3705)"
and generates reports like:
1410 "-"
24 "http://www.cse.lehigh.edu/~brian/"
21 "http://www2005.org/tutorials/"
18 "http://timconverse.com/blog/"
17 "http://www2005.org/tutorials/workshops.html"
16 "http://nike.psu.edu/dbevents/"
8 "http://www.kdnuggets.com/news/2004/n24/28i.html"
8 "http://blog.searchenginewatch.com/blog/050121-124259"
Let's get back into C...
C versus C++
- C++ is mostly a superset of C
- So what are the major additions?
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
- Variables must 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
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.
Conditionals
if (expression)
statement1
else
statement2
switch (expression) {
case const-expression: statements
case const-expression: statements
default: statements
}