Presentation on theme: "Technotronics GCECT '091 Decode C This is a C Programming event, where you will be given problems to solve using C only. You will be given a Linux system."— Presentation transcript:
Technotronics GCECT '091 Decode C This is a C Programming event, where you will be given problems to solve using C only. You will be given a Linux system with gcc, as the default C compiler. You may use gdb for debugging. Use of headers other than stdio.h, stdlib.h and math.h is probihited.
Technotronics GCECT '092 About the gcc compiler GCC stands for GNU Compiler Collection. It comprises of many language compilers like C, C++, Objective C, Java and Fortran. To compile your C file with gcc use the following minimal command line: gcc [ -o -g -lm ] The options in third brackets  are optional and if omited a.out is the default output file.
Technotronics GCECT '093 More about gcc To add debug symbols to your output file, the optional command line option -g is used. This is useful when debugging your compiled file with gdb. The command line options -lm for linking math libraries. The command line options, which are present in pairs, may be present anywhere. For example: gcc -o my.out my.c is the same as gcc my.c -o my.out
Technotronics GCECT '094 Even some more about gcc To only compile a file (and not link), use the -c command line switch. You will get a corresponding.o file. To view all warnings generated by gcc use the command line switch: -Wall. For additional you may view the man page of gcc with man gcc or you may want to run gcc -- help.
Technotronics GCECT '095 Running compiled programs Since. is not included in the path, you have to execute the output file with the command at the shell prompt: $./a.out (If the output file is a.out). To pass arguments to the command line just type the arguments after the file-name. To divert command line arguments use the '<' operator. E.g., to divert inputs from a file: inp.in to a.out use: $./a.out < inp.in
Technotronics GCECT '096 Source Files The main source file of your solution must be named as main.c (a rule of the event, not gcc!). To use / compile multiple source files, you can use the command line option: gcc main.c... [ -o ] Also note that your source files must end with a lower case 'c' not a higher case 'C' or it may be assumed to a C++ source file.
Technotronics GCECT '097 Debugging Debugging is the process of inspecting a program to find out it's potential or known bugs. Bugs are programming mistakes that are generally hard to detect but debugging makes this process much easier. We will provide gdb and gdbtui as debuggers. To start debugging use: gdb [ ] You may alternatively also use gdbtui, however it's interface is a bit buggy.
Technotronics GCECT '098 Debugging with gdb To view the program source, use the command: (gdb) list If you omit, the from the gdb prompt, use the command to load the output file (if called a.out): (gdb) file a.out Reading symbols from /home/gcect/C/Problem1/a.out...done. To start running the program, use run command.
Technotronics GCECT '099 Debugging – Crashes To inspect crashes, you have to use breakpoints or inspect other data like stack information. To see the stack trace, use the command: backtrace. To set break-points use the command: break. For setting a break-point before main : (gdb) break main Breakpoint 1 at 0x8049b6c: file myfile.c, line 33.
Technotronics GCECT '0910 Debugging - Navigation Once you have paused at the first break-point you might want to step through the execution. A few modes of stepping in gdb are given below along with the corresponding command: Step into (for n-lines) : step [n] Step over (for n-lines) : next [n] Run till n-counts or next breakpoint : continue [n] Jump to specified line number (n) : jump n Return from current function : return [retval]
Technotronics GCECT '0911 Debugging – Data The print or p command is really helpful in gdb. print Format may be one of x (hex),d (signed int),u (unsigned int),o (octal),c (char),f (float). (gdb) p /x x $1 = 0x33 The output is in the form of $a, where a is a whole number. Thus the value of 'x' is 51 (decimal) or 33 (hex).
Technotronics GCECT '0912 Debugging - Breaks To set a break-point at the line number 17 (just for example) : (gdb) break 17 To set conditional break points use : break [ ] if (gdb) break 107 if x == 50 To enable or disable break points use the commands enable and disable followed by the break-point number.
Technotronics GCECT '0913 Debugging – Data manipulation Continuing from the earlier example, the output with $a can be used to compute values in gdb. To change the value of data use: (gdb) set variable x=15 In order to get / set the values of variables in a different stack frame (i.e., in a different scope), use the scopr resolution operator preceded by the name of the function it appears in. Example: (gdb) p main::x $2 = 15
Technotronics GCECT '0914 Common Pitfalls The return type of main must be int and not void. Thus void main is wrong. The header files conio.h and alloc.h are not available in gcc. So getch() will not work. The Dynamic Memory Allocation routines are found in stdlib.h