CGS 3460 Unix Commands man – manual (man gcc) ls – list directory contents (ls) pwd – prints working directory (pwd) cd – change directory (cd <subdirectory>) mkdir – create directory (mkdir <new directory> rm – remove a file (rm <file to remove>) Use –r if removing a directory
71
Embed
CGS 3460 Unix Commands n man – manual (man gcc) n ls – list directory contents (ls) n pwd – prints working directory (pwd) n cd – change directory (cd.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CGS 3460
Unix CommandsUnix Commands man – manual (man gcc) ls – list directory contents (ls) pwd – prints working directory (pwd) cd – change directory (cd <subdirectory>) mkdir – create directory (mkdir <new directory> rm – remove a file (rm <file to remove>)
Use –r if removing a directory
CGS 3460
Unix Commands(cont)Unix Commands(cont) cp–copy a file (cp <source><destination>)
Use –r if copying a directory mv–move or rename files (mv <source> <destination>) jpico – text editor (jpico <file to edit>) gcc – compiler (gcc sourceFile.c)
-o option Directory shortcuts
~ home directory .. parent directory . sub directory
CGS 3460
Declaration of VariablesDeclaration 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;}
CGS 3460
Summary of Data TypeSummary of Data TypeType Examples Printf
char ‘a’, ‘\n’ %c
_Bool 0, 1 %i, %u
short int 1,100, -5 %hi, %hx, %ho
unsigned short int 1, 39, 100 %hu, %hx, %ho
int -1, 5, 0XAF, 0177 %i, %x, %o
unsigned int 5u, 0XAFu, 0177U %u, %x, %o
long int 0xffffL, 12l %li, %lx, %lo
unsigned long int 0xffffUL, 12ul %lu, %lx, %lo
CGS 3460
Summary of Data Type – cont.Summary of Data Type – cont.Type Examples Printf
long long int 0xffffLL, 12ll %lli, %llx, %llo
unsigned long long int 0xffffULL, 12ull %llu, %llx, %llo
float 12.3f, 3.1e-5f, 0x1.5p10
%f, %e, %g
%a
double 12.3, 3.1e-5, 0x1.5p10
%f, %e, %g
%a
long double 12.3l, 3.1e-5l %Lf, %Le, %Lg
CGS 3460
Operations for int typeOperations 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;
result of y/z will be truncated
CGS 3460
Operations for float typeOperations 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; result of y/z will NOT be truncated
CGS 3460
Assignment OperatorsAssignment Operators Join the arithmetic operators
Format: op=
Examples:
count = count + 10; count += 10;
count = count - 5; count -= 5;
a /= b + c; a = a / (b + c);
CGS 3460
Unary OperatorsUnary Operators Unary plus / minus
+ / - Example: -a
Unary increment/decrement ++ / --
M = M + 1; M += 1;
++M;
M++;
CGS 3460
PrecedencePrecedenceFIRST
() (3 + 5) * 8
++, -- x++
(Unary) +, - -x * 7
*, /, % 5 * 8
(Binary) +, - 3 + 5
= x = 4
LAST
CGS 3460
Operator Return Types (z = x ? y)Operator Return Types (z = x ? y)
x y z
int int int
float float float
int float float
float int float
CGS 3460
Getting InputGetting 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
CGS 3460
If statementIf 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 { }
Formatting OutputFormatting Output Sometimes you would like your output to have nice
tabular output Conversion specification (%i, %f, etc) allows for
formatting text Format : %[flags][width][.prec][hlL]type
[] mean its optional Only % and type are mandatory
CGS 3460
FlagsFlags
Flag Meaning- Left – justify value
+ Precede value with + or -
(space) Precede positive value with space character
0 Zero fill numbers
# Precede octal value with 0, hexadecimal value with 0x; display decimal point for floats; leave trailing zeroes for g or G format
CGS 3460
Width and PrecisionWidth and Precision
Specifier Meaningnumber Minimum size of field
* Take next argument to printf as size of field
.number Minimum number of digits to display for integers; number of decimal places for e or f formats; maximum number of significant digits to display for g; maximum number of characters for s format
.* Take next argument to printf as precision
CGS 3460
DeclarationsDeclarations Three ways
struct date{int day;char
month[10];int year;
};
struct date today;
typedef struct {int day;char
month[10];int year;
} date;
date today;
struct {int day;char
month[10];int year;
} today;
CGS 3460
InitializationInitialization
struct {
int day;
char month[10];
int year;
} today = {15, “June”, 2007};
typedef struct {int day;char month[10];int year;
} date;
date today = {15, “June”, 2007};
struct date{int day;char month[10];int year;
};
struct date today = {15, “June”, 2007};
CGS 3460
How to useHow to use 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.month[0]=‘J’;• today.month[1]=‘u’;• today.month[2]=‘n’;• today.month[3]=‘e’;• today.month[4]=‘\0’;
Main MemoryMain 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)
CGS 3460
Addresses in a 4 bit ComputerAddresses in a 4 bit ComputerByte Number Binary Address Hex Equivalent
0 0000 0x0
1 0001 0x1
2 0010 0x2
3 0011 0x3
4 0100 0x4
5 0101 0x5
6 0110 0x6
7 0111 0x7
8 1000 0x8
9 1001 0x9
10 1010 0xA
11 1011 0xB
12 1100 0xC
13 1101 0xD
14 1110 0xE
15 1111 0xF
CGS 3460
Pointer variablePointer 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.
CGS 3460
Declaring a pointer variableDeclaring 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).
CGS 3460
Initializing a pointer variableInitializing 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 }
CGS 3460
What this doesWhat this doesmain (){ int a = 10; // integer variable initialized to value 10 int *p; // integer pointer p = &a; // store address of a in p }
a p
10
CGS 3460
DereferencingDereferencing 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 *
CGS 3460
* Operator* 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.
CGS 3460
DemonstrationDemonstrationmain () { 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 }