250 likes | 485 Views
C Programming in Linux. Jacob Chan. C/C++ and Java. Portable Code written in one system and works in another But in C, there are some libraries that need to be run on a certain environment Pthred not in Windows Similarity in programming syntax
E N D
C Programming in Linux Jacob Chan
C/C++ and Java • Portable • Code written in one system and works in another • But in C, there are some libraries that need to be run on a certain environment • Pthred not in Windows • Similarity in programming syntax • Looping, conditionals, switches, functions, arrays, declarations/assignments (different in C, but almost similar in C++)
Differences Between C/C++ and Java • C is faster • JVM has a write-once, read anywhere code (this adds overhead) • Games are usually written in C/C++ • Java is easier to learn • C/C++ is hard to grasp because of POINTERS! • Java uses pointers, but they are blackboxed • Every object in Java is a pointer • Alternative to Java: C#
Differences Between C/C++ and Java • C/C++ is more prone to runtime errors • Declaring new variables in C/C++ usually has garbage values • In Java, they are automatically set them to 0, null, or default value • POINTERS • Segmentation faults • Memory allocation is sometimes manually set by programmer • a[10] with 5 elements will return garbage values for the next 5 • Memory errors will print out a segmentation fault error • Effect: CRASH!!
Differences Between C/C++ and Java • Object-oriented-ness of Java • Java is object-oriented, so everything is stored in an object • C is more on procedural • There are no classes in C, but there are structs • C++ is a mix of both object-oriented and procedural programming • What you can do in C, you can also do in C++ (in most cases)
Differences Between C/C++ and Java • Why is Java taught first here? • Debate over Java vs C/C++ (Java won) • C/C++ needs to be taught still because it is better in some cases than Java • Implementation wise, packages in java are more functional than libraries in C/C++ • C/C++ needs external libraries (OpenGL, OpenCV) • Java is “younger” and more standardized than C/C++ • Packages
First C Program (Since we will use this in class) #include <stdio.h> int main(void) { printf("Hello, world!\n"); return 0; } // Save this program as hello.c
First C Program (Since we will use this in class) • Then, compile the program with: gcc –o hello hello.cpp • And run it as: ./hello • The ./ is required in Linux • In Windows, it’s okay to ignore this already
Exercise • Try printing out command line arguments int main(intargc, char* argv[]) { int I; for(i= 0; i < argc; i++) { printf("arg %s\n",argv[i]); } return 0; // non-zero means an error }
Exercise • Try adding this line after printf: printf("%c\n", argv[i][0]); printf("%s\n", argv[i]+1); • What happens?
More on POINTERS • Any variable type can be a pointer type • Java declares everything as a pointer int a; int *b; //int pointer b = &a; // & (used for accessing memory address of variable) a = 5; printf(“%d\n”,*a); //what happens?
Functions • Same as creating methods in Java, but needs to have been declared first before being used void fxn(){ printf(“I am at fxn\n”); } Int main(){ fxn(); return 0; }
Functions • C/C++ has this feature called prototyping (allows functions to be declared before being used) void fxn(); int main() { fxn(); return 0; } void fxn() { printf(“I am fxn\n”); }
Functions: Exercise • What is the difference between these two swap methods? void swap(int a, int b){ int temp; temp = a; a = b; b = temp; } void swap (int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; }
Inputting in Console printf () – to print out in console scanf () – to input in console int main(){ char aChar; printf("\nEnter a character:"); scanf("%c", &aChar); printf(“Character is %c”, aChar); }
Array assignment with malloc() and free() • Normal array assignment inti[20]; //create an array of int with 20 elements • Heap array assignment inta[] = malloc(sizeof(int) * 20); //create a heap • Difference: dynamic vs static memory (because or realloc() ) • Deallocatememory (needed after memory is used. Otherwise, there will be a MEMORY LEAK) free(a);
LAB 2: Sorting algorithm • Write a program sort.c which sorts a list of names • I should be able to run this command: ./sort • The first line consists of the number of elements in the list • Then the program should be able to print out the sorted list • The entire thing should be able to sort the names by first name. (to make things simpler) Example input: 4 Jack Frost Jake Long Jane Doe Johnny Appleseed • Make sure that each line should be read as well
LAB 2: Sorting algorithm • Sample first lines: int N; //number of elements in list char *name[]; //string = char * in C char ipline[128]; //read each line scanf("%d\n", &N); name = (char **)malloc(sizeof(char *) * N); for(j = 0; j < N; ++j) { gets(ipline); name[j] = (char *) malloc(strlen(ipline)+1); strcpy(name[j], ipline); } //sort //printout sorted array //free
LAB 2: Sorting algorithm • Of course, I will have to check if you have a CERTIFICATE OF AUTHORSHIP (failure to do so will null and void your lab) • File name: CS162B_Lab2_<Section>_<Surname>_<ID Number>.tar • Submit on the link provided for your section. Failure to do so will incur a -10 • Deadline: Tonight @ 11:55pm
Next Meeting • More on C • Reading files • Using file pointers • Other features of C
THE END “Faith is taking the first step even if you do not see the whole staircase”