Unix Commands - PowerPoint PPT Presentation

1 / 71
About This Presentation
Title:

Unix Commands

Description:

CGS 3460. Unix Commands. man manual (man gcc) ls list directory contents (ls) ... n stars at the nth line. Print 1 star at the 1st line. Print 2 stars at ... – PowerPoint PPT presentation

Number of Views:428
Avg rating:3.0/5.0
Slides: 72
Provided by: rzh
Category:
Tags: commands | ls | stars | unix

less

Transcript and Presenter's Notes

Title: Unix Commands


1
Unix Commands
  • man manual (man gcc)
  • ls list directory contents (ls)
  • pwd prints working directory (pwd)
  • cd change directory (cd ltsubdirectorygt)
  • mkdir create directory (mkdir ltnew directorygt
  • rm remove a file (rm ltfile to removegt)
  • Use r if removing a directory

2
Unix Commands(cont)
  • cpcopy a file (cp ltsourcegtltdestinationgt)
  • Use r if copying a directory
  • mvmove or rename files (mv ltsourcegt
    ltdestinationgt)
  • jpico text editor (jpico ltfile to editgt)
  • gcc compiler (gcc sourceFile.c)
  • -o option
  • Directory shortcuts
  • home directory
  • .. parent directory
  • . sub directory

3
Declaration of Variables
  • type name initial_value
  • type name1 initial_value1, name2
    initial_value2,

int v1, v2, sum v1 50 v2 30 sum v1
v2
int v1 50, v2 30, sum sum v1 v2
4
Summary of Data Type
5
Summary of Data Type cont.
6
Operations for int type
  • Declaration
  • int x, y, z
  • Assignment
  • y 10
  • z 6
  • Calculation
  • Plus
  • x y z
  • Minus -
  • x y z
  • Multiply
  • x y z
  • Divide /
  • x y / z
  • Modulus
  • x y z

7
Operations for float type
  • Declaration
  • float x, y, z
  • Assignment
  • y 10.00
  • z 5.8
  • Calculation
  • Plus
  • x y z
  • Minus -
  • x y z
  • Multiply
  • x y z
  • Divide /
  • x y / z

8
Assignment Operators
  • Join the arithmetic operators
  • Format op
  • Examples

count count 10
count count - 5
a / b c
9
Unary Operators
  • Unary plus / minus
  • / -
  • Example -a
  • Unary increment/decrement
  • / --

M M 1
10
Precedence
11
Operator Return Types (z x ? y)
12
Getting Input
  • Need input from user
  • scanf
  • Same format as printf, except put in front of
    variable names
  • scanf(i, count)
  • means the "address of
  • to store whatever the user enters into the memory
    address where number is stored
  • Leaving out the will cause your program to work
    incorrectly!
  • Exception double uses lf in scanf and f in
    printf

13
If statement
  • Consists of keyword if
  • Followed by condition in parenthesis
  • Followed by the body of the if statement
  • This is what is executed if the condition
    evaluates to true
  • Body can consist of multiple statements if they
    are enclosed with

14
The if Statement
  • Format
  • if ( condition )
  • program statement
  • or
  • if ( condition )
  • program statement(s)

15
The if-else Statement
  • Format
  • if ( condition )
  • program statement 1
  • else
  • program statement 2
  • Or
  • if ( condition )
  • program statement(s) 1
  • else
  • program statement(s) 2

16
The else if Statement
  • Format
  • if ( condition1 )
  • program statement 1
  • else if (condition2)
  • program statement 2
  • Flow

17
Logical Operators
  • Why
  • Make a decision based on multiple conditions
  • What are they

18
Logical OR
  • Returns false only if both expressions are false
  • Example
  • (4 gt 5 6 lt 10)
  • (4 lt 5 6 10)
  • (4 gt 5 6 gt 10)
  • (4 lt 5 6 ! 10)

1 1 0 1
19
Logical AND
  • Returns true only if both expressions are true
  • Examples
  • (4 gt 5 6 lt 10)
  • (4 lt 5 6 10)
  • (4 gt 5 6 gt 10)
  • (4 lt 5 6 ! 10)

0 0 0 1
20
Logical NOT
  • Inverts the Boolean value of an expression
  • Example
  • _Bool a 0
  • if (!a)
  • printf(a is a false value (0)\n)
  • a 1
  • if (a)
  • printf(a is a true value (1)\n)

21
The switch Statement
  • When to use
  • The value of a variable successively compared
    against different values
  • Format
  • switch( expression )
  • case value 1
  • program statement 1
  • break
  • case value 2
  • program statement 2
  • break
  • ?
  • ?
  • case value n
  • program statement n
  • break
  • default
  • program statement n1
  • break

22
More on switch Statement
  • case value 1
  • program statement 1
  • case value 2
  • program statement 2
  • break

23
for loop
  • Format
  • for( init_expression loop_condition
    loop_expression )
  • program statement
  • Flow

24
Example
  • If we want to print following pattern

Print 1 star at the 1st line
Print 2 stars at the 2nd line
Print 3 stars at the 3rd line
Print n stars at the nth line
25
Code
  • include ltstdio.hgt
  • int main(void)
  • int row, col
  • for (row 1 row lt 5 row)
  • for (col 1 col lt row col)
  • printf("")

printf("\n")
26
while loop
  • Format
  • while (loop_condition) program
    statement
  • Flow

27
for loop vs while loop
for loop
while loop
28
Convert for loop to while loop
  • while (loop_condition) program
    statement
  • for( init_expression loop_condition
    loop_expression )
  • program statement

init_expression while(loop_condition)
program statement loop_expression
29
do-while loop
  • Format
  • do      program statement while
    (loop_condition)

30
while and do-while loop
do      program statement while
(loop_condition)
while (loop_condition) program statement
  • In while loop, program statement may never be
    evaluated. While in do-while loop, it is
    evaluated at least once

31
break
  • Used to break out of a loop immediately
  • Possibly due to detection of an error

int i for(i0 i lt 3 i)
printf(here\n) break
printf(there\n)
here
32
continue
  • Used to continue at the next point
  • Possibly due to detection of an error

int i 0 for(i0 i lt 3 i)
printf(here\n) continue
printf(there\n)
here here here
33
Array
  • What is an array
  • Data structure that holds a group (list) of
    homogenous elements of a specific type
  • Associate a set of values with a single variable
    name
  • All of these values must be of the same data type
  • Think grades for example
  • Why?
  • Process a group of values using a loop
  • Consider dealing with grades of 50 students
  • Without arrays you would declare 50 variables for
    each student

34
How to Define
  • Declaration
  • type ltvar_namegtsize
  • Example
  • To define an integer array called numbers of size
    5
  • int numbers5
  • Compare to normal integer declaration
  • Each number is called an element
  • Indexed from 0 to N-1 (4)

numbers
35
How to Use
  • How to refer to an individual element of an array
  • Accessed by their position in the array, e.g.
  • numbers1 2
  • Set the element at index 1 of numbers to 2
  • Do this for any element (0 4)
  • Operation on element in an array
  • Same as normal variable

2
36
Array Manipulation
int first, i int numbers5 numbers0
12 numbers1 14 numbers2 6 numbers3
8 numbers4 7 first numbers0 //first
becomes 12 numbers1 numbers0 10
numbers3 numbers0 numbers1
numbers2 i 2 numbersi 50
numbersi-1 numbersi numbersi
numbersi numbersi1
12
22
14
50
50
90
6
8
40
7
37
Initializing Arrays
  • Initializing an array using a comma-separated
    list of values in
  • int number 5, 7, 2
  • Size of an array is automatically set as the
    number of elements within

5
7
2
38
Initializing Arrays
  • Initializing part of an array, and other numbers
    will set to 0
  • int numbers5 3, 1

3
1
0
0
0
39
Initializing Arrays
  • Initializing part of an array, and other numbers
    will set to 0
  • int numbers5 0 3, 2 1

3
0
1
0
0
40
Character Arrays
  • You can have an array of characters
  • char word H,e,l,l,o,!

word0
41
Strings
  • A sequence of characters delimited by (not
    part of the string)
  • hello
  • Neko
  • This is some random sentence that I typed!
  • C does not have a string type
  • It uses an array of characters
  • Array contains a null character (\0) to denote
    the end of the string
  • Uses s to print

42
Getting String Input
  • Several ways scanf is simplest but most
    dangerous
  • Example take input and print it

// demonstrates string input include ltstdio.hgt
main () // variables declaration char
name11 // get input from user printf
("Your name (10 letters max)\n") scanf
("s", name) printf ("Hello s \n", name)

Your name (10 letters max) Neko Hello Neko
43
Declaring and Defining
  • Declaring a function must be done before main
    if used
  • return_value_type function_name( parameter-list)
  • How to define a function
  • If you declare the function, you can define it
    after main
  • If you dont declare the function you must define
    it before main
  • return_value_type function_name( parameter-list)
  • Declarations Definitions
  • Statements
  • Parameter-list format
  • type1 variable_name1, type2 variable_name2,typeN
    variable_nameN

44
Examples
  • Definitions
  • int main()
  • double calcMax(double a, double b)
  • Special cases
  • Use void for return_value_type if no value is
    needed to be returned.
  • Dont need to put anything for parameter-list if
    no parameters are needed to pass to the function
    (you can add void if you like)

45
Arguments
  • Arguments
  • Specific values for a particular function call
  • Parameters variables passed in to a function
  • Example
  • double CalcMax(double a10)
  • The values assigned to the array a are passed to
    the function at runtime
  • Increase usefulness and flexibility

46
Function III
  • How to return a value in a function
  • return // for void return type
  • return expression // to return the value to
    expression to the caller
  • How to call/invoke a function
  • function_name()
  • // for function with no return value
  • variable_name function_name()
  • // for function with return value

47
Calculating Area - Example
  • Create a function to calculate the area of a
    rectangle given its length and width
  • What does it need to calculate the area?
  • Length
  • floating point type
  • Width
  • floating point type
  • What will it give back
  • Area
  • floating point type

double
length,
width
double
double
CalcArea(
)
48
Example Area Calculation - 1
  • double CalcArea(double height, double width)
  • return height width
  • int main()
  • double h, w, area
  • printf(Please input height and width\n)
  • scanf(f, f, h, w)
  • area CalcArea(h, w)
  • printf(The area is f, area)
  • return 0

49
Example Area Calculation - 2
  • double CalcArea(double height, double width)
  • int main()
  • double h, w, area
  • printf(Please input height and width\n)
  • scanf(f, f, h, w)
  • area CalcArea(h, w)
  • printf(The area is f, area)
  • return 0
  • double CalcArea(double height, double width)
  • return height width

50
Pass by Value
  • Passing a copy of the value as an argument
  • Parameters receive a distinct copy of the
    caller's arguments, as if the parameters were
    assigned from the arguments
  • Changes made to parameters have no effect on the
    callers arguments
  • Examples

h 3 w 4 area CalcArea(h, w)
3, 4
double CalcArea(double height, double width)

height 3, width 4
51
Local Variables I
  • What is local variables
  • Variables declared within a function
  • Example
  • double CalcMax(double a10)
  • int i
  • double maxValue
  • int main()
  • int i
  • double a10
  • double maxValue
  • maxValue CalcMax(a)

52
Local Variables - 3
include ltstdio.hgt void foo(int x) int
main() int x 3, y 2 printf("x1
i\t\ty1 i\n", x, y) foo(x) printf("x4
i\t\ty4 i\n", x, y) printf("z i\n", z)
return 0 void foo(int x) int y 8, z
12 printf("x2 i\t\ty2 i\t\tz2 i\n", x,
y, z) x 7 printf("x3 i\t\ty3 i\t\tz3
i\n", x, y, z)
x1 3 y1 2 x2 3 y2 8
z2 12 x3 7 y3 8 z3
12 x4 3 y4 2
Syntax Error
53
Array as Parameters Example 1
include ltstdio.hgt void foo(int x10) int
main() int x10 4 5, 3 4,2
2 printf("x1 i\n", x0) foo(x)
printf("x4 i\n", x0) return 0 void
foo(int x10) printf("x2 i\n", x0)
x0 7 printf("x3 i\n", x0)
x1 0 x2 0 x3 7 x4 7
54
Pass by Reference
  • Passing the address itself rather than the value
  • Changes to parameters will affect the caller's
    arguments as well, for they are the same variable
  • Used for array, variable address
  • Use to get the location of a particular
    variable
  • Example

values
a
int values100, minVal minVal minimum(values)
double minimum(int a100)
int b, c swap(b, c)
void swap(int v1, int v2)
55
Automatic and static variables
  • By default, all variables defined within function
    are automatic local variables
  • Static variables
  • Using keyword static
  • Does not disappear after function call
  • Initialized only once

56
Example
  • void auto_static(void)
  • int autoVar 1
  • static int staticVar 1
  • printf("automatic i, static i\n",
    autoVar, staticVar)
  • autoVar
  • staticVar
  • int main()
  • int i
  • for(i 0 i lt 5 i)
  • auto_static()
  • return 0

automatic 1, static 1 automatic 1, static
2 automatic 1, static 3 automatic 1, static
4 automatic 1, static 5
57
Formatting Output
  • Sometimes you would like your output to have nice
    tabular output
  • Conversion specification (i, f, etc) allows for
    formatting text
  • Format flagswidth.prechlLtype
  • mean its optional
  • Only and type are mandatory

58
Flags
59
Width and Precision
60
Declarations
  • Three ways

struct date int day char month10 int
year struct date today
typedef struct int day char month10 int
year date date today
struct int day char month10 int year
today
61
Initialization
  • struct
  • int day
  • char month10
  • int year
  • today 15, June, 2007

typedef struct int day char month10 int
year date date today 15, June, 2007
struct date int day char month10 int
year struct date today 15, June, 2007
62
How to use
.day
  • To access the members in the structure
  • specify the variable name, followed by a period
    and the member name
  • today.day 15
  • today.year 2007
  • today.month0J
  • today.month1u
  • today.month2n
  • today.month3e
  • today.month4\0
  • OR
  • today.day 15
  • today.year 2007
  • today.monthJune

.month
today
.year
63
Main Memory
  • Main memory of computers (also called RAM or
    Random Access Memory) is made up of bytes.
  • The number of bytes in a computer with 512 MB of
    RAM is 512 1024 1024 5,3687,0912 bytes
  • Each byte in the main memory has a unique binary
    address that can be used to refer to it.
  • Earlier computers used to have a 16-bit address.
    Nowadays, most computers have a 32-bit (or even
    64-bit) addressing system.
  • The range of integers that can be stored in 32
    bit address is 0 through 4,294,967,295. Thus, in
    a 32-bit machine, we can have only 4 GB of
    addressable main memory (since we can only
    represent that many bytes with unique addresses)

64
Addresses in a 4 bit Computer
65
Pointer variable
  • A pointer variable is simply a variable that can
    be used to hold memory addresses (location) of
    another variable.
  • An integer pointer variable can be used to store
    the memory address of an integer variable.
  • A char pointer variable can be used to store the
    memory address of a character variable.
  • A float pointer variable can be used to store the
    memory address of a float type variable.

66
Declaring a pointer variable
  • A pointer variable should be declared before
    usage. Declaring an integer pointer variable p
  • int p
  • informs the compilier that variable p is a
    pointer variable.
  • int tells the compiler that variable p will be
    used to store memory address of integer variables
    (i.e. a pointer to an int).

67
Initializing a pointer variable
  • Before we can use a pointer, we should initialize
    it using the assignment operator.
  • For an integer pointer, we can only assign the
    address of some other integer variable.
  • The example below assigns the address of integer
    variable a to the pointer p

main () int a 10 // integer variable
initialized to value 10 int p // integer
pointer p a // store address of a in p
68
What this does
main () int a 10 // integer variable
initialized to value 10 int p // integer
pointer p a // store address of a in p
p
a
10
69
Dereferencing
  • Pointers store memory addresses.
  • Can access the contents of the memory address
    stored in a pointer. (access the value a pointer
    points to)
  • This is called dereferencing a pointer
  • Done using the operator

70
Operator
  • Returns the value stored at an address
  • Place in front of a pointer to return the value
    stored at the pointers address
  • int a 7
  • int p a
  • p ? 0x7e473d (the location of a)
  • p ? 7(the value stored at the location of a)
  • Note that if p is a pointer variable, then p is
    an alias for the object to which p currently
    points to.

71
Demonstration
main () int a 10 // integer variable
initialized to value 10 int p1, p2, p3 //
3 integer pointers p1 a // store address
of a in p1 p2 p1 // copy value in p1
(address of a) to p2 p3 p2 // copy value in
p2 (address of a) to p3 int c p3 //
dereference p2 (value of a) and assign it to c
printf (" d d d \n", p1, p2, c) // output
will be 10 10 10
1336
1336
1336
c
10 10 10
10
Write a Comment
User Comments (0)
About PowerShow.com