c programming in linux
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/1.jpg)
C Programming in LinuxJacob Chan
![Page 2: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/2.jpg)
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++)
![Page 3: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/3.jpg)
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#
![Page 4: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/4.jpg)
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!!
![Page 5: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/5.jpg)
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)
![Page 6: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/6.jpg)
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
![Page 7: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/7.jpg)
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
![Page 8: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/8.jpg)
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
![Page 9: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/9.jpg)
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
}
![Page 10: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/10.jpg)
Exercise
Try adding this line after printf:
printf("%c\n", argv[i][0]);
printf("%s\n", argv[i]+1);
What happens?
![Page 11: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/11.jpg)
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?
![Page 12: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/12.jpg)
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;
}
![Page 13: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/13.jpg)
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”);
}
![Page 14: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/14.jpg)
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;
}
![Page 15: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/15.jpg)
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);
}
![Page 16: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/16.jpg)
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);
![Page 17: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/17.jpg)
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:4Jack FrostJake LongJane DoeJohnny Appleseed
Make sure that each line should be read as well
![Page 18: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/18.jpg)
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
![Page 19: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/19.jpg)
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
![Page 20: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/20.jpg)
Next Meeting
More on C Reading files Using file pointers Other features of C
![Page 21: C Programming in Linux](https://reader035.vdocument.in/reader035/viewer/2022081505/56815751550346895dc4f58e/html5/thumbnails/21.jpg)
THE END“Faith is taking the first step even if you do not see the whole staircase”