Intensive Programming in Linux with C.F. Yurkoski - CS288

cs288 n.w
1 / 54
Embed
Share

Join CS288 - Intensive Programming in Linux instructed by C.F. Yurkoski at NJIT. Learn C programming, solve exercises, and explore the basics of C language. Check out the course materials, including slides and assignments, to enhance your skills.

  • Programming
  • Linux
  • C
  • CS288
  • Learning

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Cs288 Intensive Programming in Linux Instructor: C F Yurkoski Christopher.f.yurkoski@njit.edu Section web site: https://web.njit.edu/~yurkowsk/cs288.html Class 6 6-9-15 1

  2. Topics today Homework revu More C Time for questions

  3. text 6-9-15 cfy 3

  4. Submit only .c files (or .h if I ask for header files too.) Insure that your code compliles on the afs Linux using the default options (no gcc) Write a hello world program done in class last week Do problem 1-4 from text Do problem 1-8 from text

  5. First program /* This is a comment */ #include <stdio.h> /* preproc d*/ main() { printf("hello world\n"); } 6-9-15 5

  6. Problem 1-4 Exercise 1 the corresponding Celsius to Fahrenheit table. Exercise 1- -4. 4. Write a program to print https://web.njit.edu/~yurkowsk/x/problem1-4.c

  7. Problem 1-8: ite a program to count blanks, tabs, and newlines. ./public_html/x/exer1-8A.c

  8. Some more Basics of C 6-9-15 10

  9. Conditional Operator ? : exp1 ? exp2 : exp3 If exp1 is non-zero value of expression is exp2 otherwise it is exp3 1 ? 2 : 3 6-9-15 cfy 26

  10. Operator Precedence left to right * / % + - 6-9-15 cfy 29

  11. Some Basics /* Comments */ { blocks } Parameters ( ) # preprocessor directive Statements end with ; Loops: for while Conditionals if then 6-9-15 30

  12. $ cat header.h #define ROOT 1 $ cat main1.c /* * This is a multi-line * comment block. */ #include <srdio.h> #include "header.h" /* This a one line comment */ main(){ int x=ROOT; /* this is an embeeded comment */ #ifdef ROOT printf("%d",x); #else printf("not root\n"); #endif }

  13. $ cc main1.c $ ./a.out 1$

  14. Basic printf formats %i or %d %c %f %s int char float string 6-9-15 cfy 33

  15. Width and precision %d %6d %f %6f print as floating point, at least 6 characters wide %.2f print as floating point, 2 characters after decimal point print as decimal integer print as decimal integer, at least 6 characters wide print as floating point 6-9-15 cfy 34

  16. I/O C=getchar(); putchar(C); 6-9-15 cfy 35

  17. for statement for(init;condition;increment) { statement(s); } for(i=0;i<5;i++) { printf( %d\n ,i); } 6-9-15 cfy 36

  18. Symbolic Contants #define name replacement text #define LIMIT 30 6-9-15 cfy 37

  19. Escape sequences \a \b \f \n \r \t \v \\ \ \ \? \nnn any as an octal number \xhh a hexadecimal number 07 08 0C 0A 0D 09 0B 5C 27 22 3F Alarm (Beep, Bell) Backspace Formfeed Newline (Line Feed) Carriage Return Horizontal Tab Vertical Tab Backslash Single quotation mark Double quotation mark Question mark The character whose numerical value is given by nnn interpreted any The character whose numerical value is given by hh interpreted as 6-9-15 38

  20. if statement if ( expression) statement else statement if ( expression ) statement else if ( expression ) statement 6-9-15 cfy 39

  21. arrays int myarray[10]; 6-9-15 cfy 40

  22. functions int function ( int param1, char param2); int function ( int param1, char param2) { int local1, local2; /* call by value */ retval=function2 (local1); } return(10); 6-9-15 cfy 41

  23. argv, argc #include <stdio.h> int main (int argc, char *argv[]) { printf( %s\n ,argv[0]); return 0; } 6-9-15 cfy 42

  24. compiler cc file.c o file 6-9-15 cfy 43

  25. More basic C More on arrays: Array initialization during declaration: int num[5]; num[0]=1; int num[5]={1,2,3,4,5}; 6-9-15 cfy 44

  26. Multi-dimensional arrays int a[2][3]; int a[2][3]={{1,2,3},{4,5,6}}; 6-9-15 cfy 45

  27. 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]); 6-9-15 cfy 46

  28. 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 6-9-15 cfy 47

  29. 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 6-9-15 cfy 48

  30. 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 6-9-15 cfy 49

  31. Strings (cont). Header file string.h Some examples: strcmp String Compare strcat String Concatenation memcpy Copy Memory Block strlen String Length 6-9-15 cfy 50

  32. Strings (cont). General form: Strcat(string1,string2) Appends string2 to string1; 6-9-15 cfy 51

  33. 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= 1814055836 6-9-15 cfy 52

  34. More C keywords switch sizeof Storage classes: auto register extern static break, continue, goto 6-9-15 cfy 53

  35. 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" ); } 6-9-15 cfy 54

  36. label: while (true) { if(a) break; if(b) continue; if(c) goto label; } 6-9-15 cfy 55

  37. 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 6-9-15 cfy 56

  38. 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. 6-9-15 cfy 57

  39. Structures! struct <name> { element1; element2; } 6-9-15 cfy 58

  40. Structures example struct class { char name[30]; int number; char instructor[25]; int students[35]; }; 6-9-15 cfy 59

  41. Structures element dereferencing structure-variable.stucture-member; e.g.: x=class.number; 6-9-15 cfy 60

  42. Arrays of Structures struct class semester[30]; struct grades { char name[30]; int id; int grades[10]; }g[35]; 6-9-15 cfy 61

  43. Unions struct class semester[30]; union students { long x; char grades[8]; }g[35]; 6-9-15 cfy 62

  44. sizeof can be applied to unions and structures Structures can be nested. 6-9-15 cfy 63

  45. Pointers General form: datatype *ptrname; int x=666, y=555; Int *p; p = &x; y = *p; 6-9-15 cfy 64

  46. Some cc command line options -I -L -c -o Compiling multiple C files. Linking multiple objects.

Related


More Related Content