YOUNGMIN KWON / TONY MIONE Some Useful UNIX Commands
CSE320 System Fundamentals II Hello World YOUNGMIN KWON / TONY MIONE Some useful UNIX commands
Directory related commands ◦ ls:lists directory contents. ◦ Example: ls –al # List all files with long (extended) info ◦ mkdir: create a directory. ◦ Example: mkdir abc # Create a new directory called abc in the current dir ◦ Example: mkdir –p development/src/mainmodule # Create directory mainmodule inside of src inside of development. Create any directories in that path that do not yet exist ◦ cd: change directory. ◦ Example: cd abc # Change working dir to abc ◦ Example: cd .. # Change working dir to parent of current directory ◦ rmdir: remove a directory. ◦ Example: rmdir abc # Remove directory abc inside the current directory ◦ pwd: print current directory. ◦ Example: pwd # Print the path of the current working directory
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 2 Some useful UNIX commands
File Related commands ◦ cp: copy files. ◦ Example: cp * abc/ # Copy all files in the current dir to dir abc ◦ Example: cp a.txt b.txt # Copy the file a.txt to the file b.txt ◦ mv: move files. ◦ Example: mv abc/* bcd/* # Move all files from dir abc to dir bcd ◦ Example: mv a.txt b.txt # Move the file a.txt to the file b.txt ◦ cat: print the contents of a file. ◦ Example: cat a.txt # Print contents of file a.txt to the screen ◦ grep: looking for a pattern. ◦ Example: grep hello * # Look for the string Hello in all files in # the current working directory
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 3 Some useful UNIX commands
man (manual page) -> Prints Unix reference pages ◦ section number 2 is for system calls, 3 is for library routines ◦ man 3 printf ◦ man 2 fork ◦ man sin ◦ man –k
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 4 Hello.c
// #include tells the compiler to copy the contents of // the specified file to this file. // The line below will copy the contents of stdio.h to hello.i #include
// main is the function that starts the program int main() {
// printf prints out the parameter to the screen printf("hello, world\n");
// returning 0 from main indicates a normal completion. // returning non-zero means abnormal termination. return 0; }
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 5 Compiling Hello.c To compile hello.c To create hello instead of a.out run gcc –o hello hello.c gcc hello.c It will create a.out. To run this, enter the following: ./hello To run a.out It will produce the same output as ./a.out a.out It will print out hello, world
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 6 printf
int main() { printf("char value: %c\n", 'a'); printf("int value: %d\n", 'a'); printf("char value: %c, int value: %d\n", 97, 97); printf("hex number: %x\n", 97); printf("float value: %f\n", 1.0f / 3); printf("double value: %lf\n", 1.0 / 3); printf("string value: %s, %s\n", "hello", "world"); }
Format Specifiers Tutorial 1
Format Specifiers Reference
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 7 printf
int main() { printf("char value: %c\n", 'a'); printf("int value: %d\n", 'a'); printf("char value: %c, int value: %d\n", 97, 97); // … }
char value: a int value: 97 char value: a, int value: 97
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 8 printf
int main() { //… printf("hex number: %x\n", 97); printf("float value: %f\n", 1.0f / 3); printf("double value: %lf\n", 1.0 / 3); printf("string value: %s, %s\n", "hello", "world"); }
hex number: 61 float value: 0.333333 double value: 0.333333 string value: hello, world
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 9 More on printf
// string literals are the addresses where the strings are printf("string pointer: %d, %d\n", "hello", "world");
// more about strings printf("%d, %c, %c, %c, %c, %c\n", "hello", "hello"[0], "hello"[1], "hello"[2], "hello"[3], "hello"[4]);
string pointer: 4196476, 4196470 4196476, h, e, l, l, o
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 10 More on printf
char *str = "hello"; printf("%d, %c, %c, %c, %c, %c\n", str, str[0], str[1], str[2], str[3], str[4]);
int adr = (int)"hello"; printf("%d, %c, %c, %c, %c, %c\n", adr, ((char*)adr)[0], ((char*)adr)[1], ((char*)adr)[2], ((char*)adr)[3], ((char*)adr)[4]);
4196476, h, e, l, l, o 4196476, h, e, l, l, o
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 11 More on printf
// Can we do this? printf("function pointer: %d\n", printf); printf("function pointer: %p\n", main); function pointer: 4195392 function pointer: 0x400534
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 12 scanf: to read user’s input
#include
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 13 More on scanf
#include
Enter a floating point number: 0.21 read 0.210000. sin(0.210000) = 0.208460.
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 14 Arithmetic Operators
#include
a: 255, b: 5, a + b: 260 a: 255, b: 5, a - b: 250 a: 255, b: 5, a * b: 1275 a: 255, b: 5, a / b: 51 a: 255, b: 5, a % b: 0
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 15 Bitwise Operators printf("a: %5d, b: %5d, a & b: %5d\n", a, b, a & b); printf("c: %5d, b: %5d, c | b: %5d\n", c, b, c | b); printf("a: %5d, b: %5d, a ^ b: %5d\n", a, b, a ^ b);
// One’s complement vs Two’s complement printf("b: %5d, ~b: %5d (%x)\n", b, ~b, ~b); printf("-1: %x, -2: %x, -3: %x\n", -1, -2, -3); printf("b: %5d, b << 1: %5d\n", b, b << 1); printf("b: %5d, b >> 1: %5d\n", b, b >> 1); a: 255, b: 5, a & b: 5 c: 80, b: 5, c | b: 85 a: 255, b: 5, a ^ b: 250 b: 5, ~b: -6 (fffffffa) -1: ffffffff, -2: fffffffe, -3: fffffffd b: 5, b << 1: 10 b: 5, b >> 1: 2
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 16 Bitwise Operators
11111111 a: 255, b: 5, a & b: 5 c: 80, b: 5, c | b: 85 & 00000101 00000101 01010000 | 00000101 01010101
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 17 Bitwise Operators
11111111 a: 255, b: 5, a ^ b: 250 b: 5, ~b: -6 (fffffffa) ^ 00000101 11111010
~ 00000101 11111010 => (0xfa)
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 18 Bitwise Operators
-1: ffffffff, -2: fffffffe, -3: fffffffd, -4: fffffffc, -5: fffffffb b: 5, b << 1: 10 b: 5, b >> 1: 2
00000101 >> 1 00000101 << 1 00000010 00001010
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 19 Logical Operators
printf("a: %5d, b: %5d, a == b: %5d\n", a, b, a == b); printf("a: %5d, b: %5d, a != b: %5d\n", a, b, a != b); printf("a: %5d, b: %5d, a > b: %5d\n", a, b, a > b); printf("a: %5d, b: %5d, a >= b: %5d\n", a, b, a >= b); printf("a: %5d, b: %5d, a < b: %5d\n", a, b, a < b); printf("a: %5d, b: %5d, a <= b: %5d\n", a, b, a <= b);
a: 255, b: 5, a == b: 0 a: 255, b: 5, a != b: 1 a: 255, b: 5, a > b: 1 a: 255, b: 5, a >= b: 1 a: 255, b: 5, a < b: 0 a: 255, b: 5, a <= b: 0
printf("a: %5d, b: %5d, a && b: %5d\n", a, b, a && b); printf("a: %5d, b: %5d, a || b: %5d\n", a, b, a || b); printf("a: %5d, !a: %4d, !!a: %5d\n", a, !a, !!a);
a: 255, b: 5, a && b: 1 a: 255, b: 5, a || b: 1 a: 255, !a: 0, !!a: 1
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 20 More Logical Operators
printf("a: %5d, b: %5d, a && b: %5d\n", a, b, a && b); printf("a: %5d, b: %5d, a || b: %5d\n", a, b, a || b); printf("a: %5d, !a: %4d, !!a: %5d\n", a, !a, !!a);
// Note that in C, any non-zero value is true (1)
a: 255, b: 5, a && b: 1 a: 255, b: 5, a || b: 1 a: 255, !a: 0, !!a: 1
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 21 Side Effects from a Compiler Optimization a > b && printf("a > b && print\n");
// a is greater than b – the next statement // is known false before && so it ‘short-circuits’ // and does not print a < b && printf("a < b && print\n");
// a is greater than b – the next statement // is known true before || so it ‘short-circuits’ // and does not print a > b || printf("a > b || print\n"); a < b || printf("a < b || print\n"); // a is greater than b a > b && print a < b || print
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 22 Side Effects from a Compiler Optimization // Typical recursive gcd: // gcd: Euclidean algorithm int gcd(int a, int b) { int gcd(int a, int b) if (a > b) { { return gcd(a-b, b); return a > b && gcd(a - b, b) || } else { a < b && gcd(b - a, a) || if (b > a) { printf("gcd: %d\n", a); return gcd(b-a, a); } } else { return a; } } }
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 23 Questions?
(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 24