Download presentation
Presentation is loading. Please wait.
Published byIsabella Horn Modified over 9 years ago
1
C Programming in Linux Jacob Chan
2
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++)
3
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#
4
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!!
5
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)
6
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
7
First C Program (Since we will use this in class) #include int main(void) { printf("Hello, world!\n"); return 0; } // Save this program as hello.c
8
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
9
Exercise Try printing out command line arguments int main(int argc, char* argv[]) { int I; for(i = 0; i < argc; i++) { printf("arg %s\n",argv[i]); } return 0; // non-zero means an error }
10
Exercise Try adding this line after printf: printf("%c\n", argv[i][0]); printf("%s\n", argv[i]+1); What happens?
11
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?
12
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; }
13
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”); }
14
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; }
15
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); }
16
Array assignment with malloc() and free() Normal array assignment int i[20]; //create an array of int with 20 elements Heap array assignment int a[] = malloc(sizeof(int) * 20); //create a heap Difference: dynamic vs static memory (because or realloc() ) Deallocate memory (needed after memory is used. Otherwise, there will be a MEMORY LEAK) free(a);
17
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
18
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
19
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_ _ _.tar Submit on the link provided for your section. Failure to do so will incur a -10 Deadline: Tonight @ 11:55pm
20
Next Meeting More on C Reading files Using file pointers Other features of C
21
THE END “Faith is taking the first step even if you do not see the whole staircase”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.