Presentation is loading. Please wait.

Presentation is loading. Please wait.

Miguel Garzon CrUise Lab - SITE. #include dynamic memory allocationdynamic memory allocation Input/output String handlingString handling Mathematical.

Similar presentations


Presentation on theme: "Miguel Garzon CrUise Lab - SITE. #include dynamic memory allocationdynamic memory allocation Input/output String handlingString handling Mathematical."— Presentation transcript:

1 Miguel Garzon CrUise Lab - SITE

2 #include dynamic memory allocationdynamic memory allocation Input/output String handlingString handling Mathematical functionsMathematical functions Characters

3 Variables: Storage Duration and Scope Local variable –Local scope, store in the call stack Static local variable –Local scope, single and statically allocated Global variable –Accessible in every scope Example: vars.cvars.c

4 IO Functions Standard file IO: –FILE *fopen(const char *filename, const char *mode); –size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream); –size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream); –int fseek(FILE *stream, long int offset, int whence); –int fclose(FILE *stream); Example: file.c from C file input/output file.cC file input/output

5 IO Functions – printf(“the int value is %d”, var); printf(“the string value is %s”, var); printf(“the charactr and double values are %c and %e”, var); printf(“the double value is %e”, var); scanf(“enter an int: %d”, var); scanf(“enter a string: %s”, var); scanf(“enter a character and a double: %c %e”, var); scanf(“enter a string: %s”, var);

6 struct.c typedef struct{ int i; char c; float f; char cc[3]; } aaa; int len = sizeof(aaa);

7 sort.c void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );... int values[] = { 40, 10, 100, 90, 20, 25 }; int compare (const void * a, const void * b) {…} qsort (values, 6, sizeof(int), compare);

8 Pointers pointer.c int a[10]; int b[] = {1, 2, 3}; int *pb = &b[1]; char *arr[] = {"aaa", "bbb", "ccc", "ddd"}; char **p = arr; char *q = arr[0]; char **r = &arr[0];

9 dmem.c void *malloc(size_t size); void free(void *pointer); void *realloc(void *pointer,size_t size); Parameters pointer Pointer to a memory block previously allocated with malloc, calloc or realloc to be reallocated. If this is NULL, a new block is allocated and a pointer to it is returned by the function. size New size for the memory block, in bytes. If it is 0 and ptr points to an existing block of memory, the memory block pointed by ptr is deallocated and a NULL pointer is returned. malloccallocrealloc

10 Processes fork() creates a child process –On success, the PID of the child process is returned in the parent. and a 0 is returned in the child. –On failure, a -1 will be returned in the parent's context, fork.c int x = 1; /* Parent and child will get private copies*/ pid_t pid = fork(); if (pid == 0) printf("in child (%d), x = %d\n", getpid(), ++x); else printf("in parent(%d), x = %d\n", getpid(), --x); printf("bye from process (%d) with x = %d\n", getpid(), x);

11 … No boolean type in CNo boolean type in C #define TRUE 1 #define TRUE 1 #define FALSE 0 #define FALSE 0 while(1) { ; /* do nothing */ } Two functions cannot have the same name Variables must be declared at the top of a basic block (for some c systems)Variables must be declared at the top of a basic block (for some c systems) (Cela ne compile pas ) (Cela ne compile pas ) { int a; printf("Hello world\n"); char b; } { int a; printf("Hello world\n"); char b; } Exceptions? Garbage collection in CGarbage collection in C

12 bad.c char a[10]; char *b = "bbbbb"; b[0]='a'; a[8000]='b'; Bug: –a[8000] is accessing an invalid memory address

13 GDB Debug Steps $ gcc –o bad bad.c $ gdb./bad (gdb) break main [set break point at main()] (gdb) run [start program] (gdb) step 1 [step one line] Program received signal SIGSEGV, Segmentation fault. main () at bad.c:15 15 a[8000]='b';

14 Compiler GNU Compiler CollectionGNU Compiler Collection A compiler system produced by the GNU Project supporting various programming languages Compile C code to an executableCompile C code to an executable –gcc -o –gcc -o Reporting all warning messagesReporting all warning messages –gcc -Wall -o –gcc -Wall -o

15 Compiler An example of compiling two.c files into an executable programAn example of compiling two.c files into an executable program –gcc -Wall -o NomduFichier main.c list.c–gcc -Wall -o NomduFichier main.c list.c An example of compiling two files and then link them separatelyAn example of compiling two files and then link them separately –gcc -Wall -o main.o -c main.c–gcc -Wall -o main.o -c main.c –gcc -Wall -o list.o -c list.c–gcc -Wall -o list.o -c list.c –gcc -Wall -o assign1prob1 main.o list.o–gcc -Wall -o assign1prob1 main.o list.o

16 Compiler An example of compiling two.c files into an executable programAn example of compiling two.c files into an executable program –gcc -Wall -o NomduFichier main.c list.c–gcc -Wall -o NomduFichier main.c list.c An example of compiling two files and then link them separatelyAn example of compiling two files and then link them separately –gcc -Wall -o main.o -c main.c–gcc -Wall -o main.o -c main.c –gcc -Wall -o list.o -c list.c–gcc -Wall -o list.o -c list.c –gcc -Wall -o assign1prob1 main.o list.o–gcc -Wall -o assign1prob1 main.o list.o

17 Compiler enter gdbenter gdb –gdb–gdb Quit gdbQuit gdb –quit–quit printing line from a source fileprinting line from a source file –list–list running a programrunning a program –run–run breakpoint at a line numberbreakpoint at a line number –break : –break : –break –break break at a particular functionbreak at a particular function –break : –break : set a breakpoint with conditionset a breakpoint with condition –break if –break if deleting breakpointsdeleting breakpoints –delete–delete proceed onto the next breakpointproceed onto the next breakpoint –continue–continue Single stepSingle step –step–step

18 Make file Makefile and the make command –We can write many commands in a script file and run it all at once; –For example: all: gcc hello.c –o hello.o mkdir -p folderNameCreer mv hello.o folderNameCreer 1) Write the previous commands in a file called Makefile-1.txt; (in the same directory of hello.c); 2) Type the command: make -f Makefile-1.txt


Download ppt "Miguel Garzon CrUise Lab - SITE. #include dynamic memory allocationdynamic memory allocation Input/output String handlingString handling Mathematical."

Similar presentations


Ads by Google