Download presentation
Presentation is loading. Please wait.
2
Cs288 Intensive Programming in Linux
Instructor: C F Yurkoski Section web site: Class 6 6-9-15
3
Topics today Test revu Final schedule Project Revu
Some last notes on bash Intro to C. Time for questions
4
Test Revu Written Revu Problem 12 Problem 13
Moodle would not let me assign grades over 100.
5
Average grade:
9
everyone Ø (nothing)
10
true x
11
two 3
12
Problem12, average grade: 71%
grep -v "^hourly status" | grep "^hourly" | cut -f2,4 | while read data do temp=($data) hours=${temp[$count-1]} let pay=${hours}*${1} x=0 let namesize=$count-1 while [ $x -lt $namesize ] echo -n "${temp[$x]} " let x=x+1 done echo -e \\t\$$pay
13
Extra credit afsconnect1-111 test1 >: diff makechecks extracredit 15c15 < done --- > done | sort afsconnect1-112 test1 >:
14
Prob13 average grade 57% >: ./getmany_twits "Ernst" "Luther Strange" "Trump" Ernst @SenJoniErnst Luther Strange @SenatorStrange no twitter handle found for Trump >:
15
if [ $# != 1 ] then echo "Usage: $0 name" >&2 exit 1 fi twit=`wget -q -O - | grep | grep "$1" | cut -f2 | cut -d"<" -f1` if [ "$twit" != "" ] echo else echo no twitter handle found for $1 >&2 exit 0
16
Use \ to make long lines more readable
while [ $# -gt 0 ] do twit=`wget -q -O - senate/members?lang=en | grep | grep "$1" | cut -f2 | cut -d"<" -f1` if [ "$twit" != "" ] then echo -e $1\\t else echo no twitter handle found for $1 fi shift done
17
Final schedule The schedule can be downloaded at this link: 5/11/ :00 PM-8:30 PM GITC 1400
18
Project revu
19
cfy
21
cfy
22
cfy
23
cfy
24
Some final notes on bash
IFS mkdir mv cp rm tail touch find man source
26
Some Basics of C Created by Dennis Ritchie of Bell Labs in the early 70s. 6-9-15
27
text cfy
28
Tokens in C Keywords Identifiers Constants Strings Symbols Operators
6-9-15
29
Character Set Letters Digits Special characters 6-9-15
30
Identifiers Start with underscore or a letter
Can contain letter, underscore and numbers Case sensitive Cannot be a keyword No longer limited in length 6-9-15
31
C keywords auto break case char const continue default
do double else enum extern float for goto if int long register return short signed sizeof static struct typedef union unsigned void volatile while 6-9-15
32
Some Basic Types char int float short long double 6-9-15
33
Data types char int float 6-9-15 signed char unsigned char short
short int signed short signed short int unsigned short unsigned short int int signed signed int unsigned unsigned int long long int signed long unsigned long unsigned long int long long … float 6-9-15
34
Contants Integer range -32768 to +32767
Real expressed as mantissaeexponent e.g.: 12.3e4 range -3.4e38 to 3.4e38 char constants are ascii characters String contants- series of characters terminated by a null character. 6-9-15
35
Instructions Declaration I/O Arithmetic Control 6-9-15
36
Operators Arithmetic Relational Logical Bit-wise Assignment
Conditional Increment and decrement 6-9-15
37
Arithmetic Operators + = * / % 6-9-15
38
Relational Operator > >= < <= == != cfy
39
Logical Operators && || ! 6-9-15
40
Bitwise Operators* (complement, or, and, xor and shift)
~ | & ^ >> << *these cannot be used with floats cfy
41
Increment & decrement ++ -- cfy
42
Assignment Operators = += -= *= %= /= cfy
43
Conditional Operator ? : exp1 ? exp2 : exp3
If exp1 is non-zero value of expression is exp2 otherwise it is exp ? 2 : 3 cfy
44
Operator Precedence left to right
* / % + - cfy
45
Some Basics /* Comments */ { blocks } Parameters ( ) # preprocessor directive Statements end with ; Loops: for while Conditionals if then 6-9-15
46
First program /* This is a comment */ #include <stdio.h> /* preproc d*/ main() { printf("hello world\n"); } 6-9-15
47
Basic printf formats %i or %d int %c char %f float %s string
cfy
48
Width and precision %d print as decimal integer
at least 6 characters wide %f print as floating point %6f print as floating point, at least 6 characters wide %.2f print as floating point, 2 characters after decimal point cfy
49
I/O C=getchar(); putchar(C); cfy
50
for statement for(init;condition;increment) { statement(s); } for(i=0;i<5;i++) printf(“%d\n”,i); cfy
51
Symbolic Contants #define name replacement text #define LIMIT 30
cfy
52
Escape sequences \a 07 Alarm (Beep, Bell) \b 08 Backspace
\f 0C Formfeed \n 0A Newline (Line Feed) \r 0D Carriage Return \t 09 Horizontal Tab \v 0B Vertical Tab \\ 5C Backslash \‘ 27 Single quotation mark \“ 22 Double quotation mark \? 3F Question mark \nnn any The character whose numerical value is given by nnn interpreted as an octal number \xhh any The character whose numerical value is given by hh interpreted as a hexadecimal number 6-9-15
53
if statement if ( expression) statement else if ( expression ) else if ( expression ) cfy
54
arrays int myarray[10]; cfy
55
functions int function ( int param1, char param2); … int function ( int param1, char param2) { int local1, local2; /* call by value */ retval=function2 (local1); return(10); } cfy
56
argv, argc #include <stdio.h> int main (int argc, char *argv[]) { printf(“%s\n”,argv[0]); return 0; } cfy
57
compiler cc file.c –o file cfy
58
More basic C More on arrays:
Array initialization during declaration: int num[5]; num[0]=1; int num[5]={1,2,3,4,5}; cfy
59
Multi-dimensional arrays
int a[2][3]; int a[2][3]={{1,2,3},{4,5,6}}; cfy
60
Multi-dimensional arrays cont
#include <stdio.h> main() { int a[2][3]={{1,2,3},{4,5,6}}; int i,j; for(i=0;i<2;i++) for(j=0;j<3;j++) printf("a[%d][%d]=%d\n",i,j,a[i][j]); } cfy
61
Multi-dimensional arrays cont
afsconnect1-58 >: ./a.out a[0][0]=1 a[0][1]=2 a[0][2]=3 a[1][0]=4 a[1][1]=5 a[1][2]=6 cfy
62
Strings are just arrays of characters.
#include <stdio.h> main() { char name1[25]="NJIT"; char name2[25]={'N','J','I','T','\0'}; printf("name1=%s\n",name1); printf("name2=%s\n",name2); } >: ./a.out name1=NJIT name2=NJIT cfy
63
Strings (cont). main() { char name1[25]="NJIT"; char name2[25]={'N','J','I','T','\0'}; char *name3="NJIT"; printf("name1=%s\n",name1); printf("name2=%s\n",name2); printf("name3=%s\n",name3); } afsconnect1-67 public_html >: ./a.out name1=NJIT name2=NJIT name3=NJIT cfy
64
Strings (cont). Header file string.h Some examples: strcmp String Compare strcat String Concatenation memcpy Copy Memory Block strlen String Length cfy
65
General form: Strcat(string1,string2) Appends string2 to string1;
Strings (cont). General form: Strcat(string1,string2) Appends string2 to string1; cfy
66
Addresses! & used to get the address of a variable main() { int i=666;
printf("i= %d\n",i); printf("&i= %d\n",&i); } afsconnect1-72 public_html >: ./a.out i= 666 &i= cfy
67
More C keywords switch break, continue, goto sizeof Storage classes: auto register extern static cfy
68
switch( Grade ) { case 'A' : printf( "Excellent\n" ); case 'B' : printf( "Good\n" ); case 'C' : printf( "OK\n" ); case 'D' : printf( "Mmmmm....\n" ); case 'F' : printf( "You must do better than this\n" ); default : printf( "What is your grade anyway?\n" ); } cfy
69
label: while (true) { if(a) break; if(b) continue; if(c) goto label; }
cfy
70
printf("sizeof i= %d\n",sizeof(i)); printf("sizeof array= %d\n",
main() { int i=666; char array[15]; double n; printf("sizeof i= %d\n",sizeof(i)); printf("sizeof array= %d\n", sizeof(array)); printf("sizeof n= %d\n", sizeof(n)); } >: ./a.out sizeof i= 4 sizeof array= 15 sizeof n= 8 cfy
71
static Has number of implications:
Local static variables initialized only once and retain their value. Guaranteed to be automatically initialized to zero if not otherwise initialized. Global static variable inaccessible outside the file. Can be applied to functions. cfy
72
Structures! struct <name> { element1; element2; … } cfy
73
Structures example struct class { char name[30]; int number; char instructor[25]; int students[35]; }; cfy
74
Structures element dereferencing
structure-variable.stucture-member; e.g.: x=class.number; cfy
75
Arrays of Structures struct class semester[30]; struct grades { char name[30]; int id; int grades[10]; }g[35]; cfy
76
Unions struct class semester[30]; union students { long x; char grades[8]; }g[35]; cfy
77
sizeof can be applied to unions and structures
Structures can be nested. cfy
78
Pointers General form: datatype *ptrname; int x=666, y=555; Int *p; p = &x; y = *p; cfy
79
Homework Write a hello world program – do today in class
Do problem 1-4 from text Write a program which reads stdin and converts any local case characters to upper case. Redo problem 4 from class 2 as a C program. Do problem 1-8 from text cfy
80
problem 4 Write a C program, sort.c, which sorts a list of command line parameters in ascending order. For example, your command will look something like: $ sort and type enter. Your program will return: cfy
81
Homework cont. Do exercise 1-19 from the text.
cfy
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.