850 likes | 861 Views
Introduction to Computer Organization & Systems. COMP 21000 Spring 2018. Topics: UNIX command line C - everything. UNIX and C. Unix. Passwords Changing passwd whoami finger and the .plan file Help about a command man cmdName apropos cmdName
E N D
Introduction to Computer Organization & Systems COMP 21000 Spring 2018 Topics: • UNIX command line • C - everything UNIX and C
Unix • Passwords • Changing • passwd • whoami • finger and the .plan file • Help • about a command • man cmdName • apropos cmdName • which cmdName ;gives path to the application
Unix • Directories • Seeing, manuvering, creating • cd /path/to/new/directory • pwd • ls [-a –l] • mkdir dirName • rmdir dirName • rm –r dirName • pushd . • popd
Unix • Files • Moving, deleting • cp src dest • rm fileName • Viewing • more fileName • cat fileName1 fileName2 • Permissions • ls –l • chmod 777 fileName
Unix • Processes • What’s running? • ps ;only your processes • ps –a ;all processes PID TTY TIME CMD 33129 ttys000 0:00.02 -bash 33178 ttys000 0:00.00 man builtin 33186 ttys000 0:00.00 /usr/bin/less -is 33131 ttys001 0:00.01 -bash 33130 ttys002 0:00.02 –bash
Kill • kill command is used to stop a running process • A user can kill all his process. • A user can not kill another user’s process. • A user can not kill processes System is using. • A root user* can kill System-level-process and the process of any user. * the root is the administrator
Kill • kill -9 pid;kill process pid • other ways to kill • kill by name • pkilla.out;kill all processes named a.out • kill on the command line • ^c ;hold down control key and press the c key ;kills currently running process
More Unix • Shells • Environmental variables (capitalization counts!) • Echo $ENV • $PATH • .bashrc and.bash_profile files • alias • changing $PATH • using source to act on changes to the file • Pipes • using the | operator to connect programs
More Unix • Redirection • using >and>> to redirect stdout to a file • > to overwrite • >> to append • using &> to redirect stdout and stderr to a file gcc –g -o ex1 ex1.c > err.txt &> err.txt • using < to redirect stdin from file • History • the ! operator
C Programming & Systems Programming • Specific type of programming • Not used in the development of most applications • Emphasis is on conciseness and efficiency (space and time) • Ignores readability and maintainability • You will get fired if you program like this in most situations!
The three attributes of a C++/C variable • A name • A type • A value
Note the & Note that all declarations must be at the beginning, either before main() or right after main( ). Cannot put them in the body! Cannot declare in a for statement! (except gcc does allow C++ like syntax) A C++ program that processes three integer values C++ program C program #include <stdio.h> #define bonus 5 int exam1, exam2, score; main( ){ scanf(“%d %d”, &exam1, &exam2); score = (exam1 + exam2)/2 + bonus; printf(“score = %d\n”, score); }
Output: printf printf(“score = %d\n”,score);
Input: scanf scanf(“%d %d”,&exam1, &exam2);
scanf #include <stdio.h> int main() { char C, B; int x; printf("What comes after G\n"); scanf("%c", &C); printf("What comes after O\n"); scanf("%c", &B); printf("What is your age?\n"); scanf("%d", &x); return 0; } Run this!
scanf • What’s the problem? Why does it occur? • \n is a character! • Fix: leave a space before the %c that you use in scanf: scanf(” %c”, &myChar); Note the space!
What if you replace %c with %d? A program to process a character variable #include <stdio.h> char ch; main (){ scanf(“%c”,&ch); printf(“Original character: %c\n”, ch); ch++; printf(“Following character: %c\n”, ch); }
The ASCII code for a “s” A program to process a character variable (Cont’d) Using %c Using %d bash-2.03$ a.out s Original character: 115 Following character: 116
Error Checking Admins-Computer-52:~/Classes/CS210/PractSoln barr$ !g gcc testPurge0.c Admins-Computer-52:~/Classes/CS210/PractSoln barr$ !a a.out Enter an integer: 5 Enter an integer: 6 Enter an integer: a Enter an integer: Enter an integer: Enter an integer: • What if the user enters the wrong type? • C will convert characters to int • But will (normally) only read a single character • Example #include <stdio.h> int main() { int x = 0; while (x != -1){ printf("Enter an integer: "); scanf("%d", &x); } return 0; }
Error Checking • What if the user enters the wrong type? • C will convert characters to int • But will (normally) only read a single character • Example #include <stdio.h> int main() { int x = 0; while (x != -1){ printf("Enter an integer: "); scanf("%d", &x); __fpurge(stdin); } return 0; } Note: fpurge is only available in BSD 4.4. __fpurge works in Linux. Note the two underscores before the namefpurge On some systems may have to include the lib<stdio_ext.h> No man page for __fpurge google it.
Error Checking NAME fflush, fpurge -- flush a stream LIBRARY Standard C Library (libc, -lc) SYNOPSIS #include <stdio.h> int fflush(FILE *stream); int fpurge(FILE *stream); DESCRIPTION The function fflush() forces a write of all buffered data for the given output or update stream via the stream's underlying write function. The open status of the stream is unaffected. If the stream argument is NULL, fflush() flushes all open output streams. The function fpurge() erases any input or output buffered in the given stream. For output streams this discards any unwritten output. For input streams this discards any input read from the underlying object but not yet obtained via getc(3); this includes any text pushed back via ungetc(3). Note that fflush only works on output streams! Useful to use fpurge before doing input
Other IO functions #include<stdio.h> intfgetc(FILE*stream); char*fgets(char*s,intsize,FILE*stream); intgetc(FILE*stream); intgetchar(void); char*gets(char*s); intungetc(intc,FILE*stream);
Other IO functions fgetc() reads the next character from stream and returns it as an unsignedchar cast to an int, or EOF on end of file or error. getc() is equivalent to fgetc() except that it may be implemented as a macro which evaluates stream more than once. getchar() is equivalent to getc(stdin).
Other IO functions gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with '\0'. No check for buffer overrun is performed. fgets(int *s, int n, FILE *stream)reads in at most one less than n characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer. To read from keyboard use stdin for stream ungetc() pushes c back to stream, cast to unsignedchar, where it is available for subsequent read operations. Pushed - back characters will be returned in reverse order; only one pushback is guaranteed.
Example: factorial #include <stdio.h> int factorial (int n) { int ans = 1; while (n > 1) { ans *= n; n = n - 1; } return(ans); } main() { int n, i = 0, x, y; printf("Please enter value of n = "); /* Note the & before the variable n */ scanf("%d", &n); if (n >= 0) while (n >= 0) { printf("factorial( %d) = %d\n", i, factorial(i)); i = i + 1; n = n - 1; } else printf("factorial of a negative number is undefined\n"); }
Example: using function prototypes #include <stdio.h> int factorial (int n); main() { int n, i = 0, x, y; printf("Please enter value of n = "); /* Note the & before the variable n */ scanf("%d", &n); if (n >= 0) while (n >= 0) { printf("factorial( %d) = %d\n", i, factorial(i)); i = i + 1; n = n - 1; } else printf("factorial of a negative number is undefined\n"); } int factorial (int n) { int ans = 1; while (n > 1) { ans *= n; n = n - 1; } return(ans); } A function prototype is required in some versions of C if the function is defined after it is used.
Types: floating point • Family of float types. • float (often 16 bits) • double (often 32 bits) • long double (often 64 bits)
Formatting instructions For floating point output Floating Point 1 #include <stdio.h> 2 3 /* print Fahrenheit-Celsius table 4 for f = 0, 20, ... , 300 */ 5 main() 6 { 7 int lower, upper, step; 8 float fahr, celsius; 9 10 lower = 0; /* lower limit of temperature table */ 11 upper = 300; /* upper limit */ 12 step = 20; /* step size */ 13 14 fahr = lower; 15 while (fahr <= upper) { 16 celsius = (5.0/9.0) * (fahr - 32.0); 17 printf("%4.0f %6.1f\n", fahr, celsius); 18 fahr = fahr + step; 19 } 20 }
Floating point input/output “%+5.2hf” A conversion specification begins with a percent sign character, %, and has the following elements in order: 1. Zero or moreflag characters: –, +, 0, #, or space, which modify the meaning of the conversion operation. 2. An optionalminimum field width expressed as a decimal integer constant. 3. An optionalprecision specification expressed as a period optionally followed by a decimal integer. This is the min number of digits to be printed in d, I, o, u, x and X conversions. Is the num of digits to right of decimal point in e, E, and f. Is the maximum number of characters in s conversion. 4. An optionalsize specification expressed as on of the letters ll, l, L, h, hh, j, z or twhich tells whether conversion is to a long or a short when necessary. 5. The conversion operation, a single character from the set a, A, c, d, e, E, f, g, G, I, n, o, p, s, u, x, X, %
Size specification changes output type Conversion
The if statement #include <stdio.h> #define LIMIT 100 int num; main () { printf("Enter an integer: \n"); scanf("%d", &num); if (num > LIMIT) { printf("high \n"); } else if (num < LIMIT) { printf("low \n"); } else { printf("right on! \n"); } } This is how you do constants in C. The const key word also works. Don’t forget the & in scanf. Why?
The switch statement #include <stdio.h> int guess; main() { printf("Enter a number between 1 and 3: \n"); scanf("%d", &guess); switch(guess) { case 1: printf("%d is way too low \n", guess); break; case 2: printf("%d is correct!! \n", guess); break; case 3: printf("%d is way too high \n", guess); break; default: printf("%d is not between 1 and 3!!\n", guess); break; } }
The switch statement (Cont’d) users-MacBook-Pro:Spring 11 user$ testSwitch Enter a number between 1 and 3: 1 1 is way too low users-MacBook-Pro:Spring 11 user$
The while statement #include <stdio.h> char letter; main() { printf("Enter a letter between a and z: \n"); scanf("%c", &letter); while (letter != '*') { printf("Enter a letter between a and z: \n"); scanf("%c", &letter); } } scanfcauses a problem. Why? How do we fix this? We can end input with a ^d. Will this help?
The while statement (Cont’d) #include <stdio.h> char letter; main() { printf("Enter a letter between a and z: \n"); gets(&letter); printf("You entered: %c\n", letter); while (letter != '*') { printf("Enter a letter between a and z: \n"); gets(&letter); printf("You entered: %c\n", letter); } } Does this work? Why do you get: warning: this program uses gets(), which is unsafe. What happens if you enter two letters on a line (like ab)? Why?
The do statement <stdio.h> printf(“%d”,cop);
The for statement with an array #include <stdio.h> #define SIZE 4 main() { inti; intv[SIZE]; for (i = 0; i < SIZE; i++) { printf("Enter an integer: \n"); scanf("%d", &v[i]); } for (i = 0; i < SIZE; i++) { printf("v[%d] = %d: \n", i, v[i]); printf("v[%d] = %d: \n", SIZE-i-1, v[SIZE - i - 1]); } } Why the “- 1” ?? How would you make the two print statements print side-bye-side?
The for statement with an array (Cont’d) users-MacBook-Pro:Spring 11 user$ !t testFor Enter an integer: 9 Enter an integer: 8 Enter an integer: 7 Enter an integer: 6 v[0] = 9: v[3] = 6: v[1] = 8: v[2] = 7: v[2] = 7: v[1] = 8: v[3] = 6: v[0] = 9: users-MacBook-Pro:Spring 11 user$
Pointer declaration Pointer assignment Accessing value that pointer points at Changing a value in variable that pointer points at Pointers #include <stdio.h> int main() { int n1, n2, *intPtr; intPtr = &n1; printf ( "Enter two numbers\n"); scanf("%d%d",&n1,&n2); printf ( "The numbers you entered are: %d and %d \n", n1, n2); printf ("n1 is %d\n", *intPtr); intPtr = &n2; printf ( "n2 is %d\n",*intPtr); *intPtr = n1 + n2; printf ( "and their sum is: %d\n",n1 + *intPtr); }