Overview

C Programming Books / References

To access the O'Reilly Safari login page you will get a prompt to select your institution. UIC is not listed, so instead select the link for "Institution not Listed?" and provide your UIC email. On the O'Reilly books page search for the titles listed below:

  1. Programming in C, 4th Edition, by Stephen Kochan, which once you're logged in should be here

  2. Understanding And Using C Pointers, by Richard Reese. O'Reilly, 2013, which once you're logged in should be here.

If you don't have a UIC email to give you O'Reilly library access, see The C Book, by Banahan et. al.

vim Editor

The vim editor is found just about anywhere UNIX is installed. It is helpful to know basic editing commands. vim-adventures.com is a fun way to learn the basic commands. Alternatively type in vimtutor from a UNIX command line. Mac users, type in cmd-space bar and type in "terminal" to start a terminal session.

The key idea to remember in vim is that there are three modes:

  1. Normal mode, where key presses (e.g. h,j,k,l) move you around the surface of the text. This is the default when you start.

  2. Insert mode, where typing results in letters being placed into the document.

  3. Command mode, where you type commands on the bottom line of the screen.

You need to be able to move between modes. From normal mode type i to enter insert mode. Once typing in insert mode, press escape to exit back to normal mode. From normal mode type : to enter command mode. From command mode press q to quit, or q! to force quit, or wq to write and then quit. See a vim tutorial sheet and a simpler one.

An even easier (but not very efficient) editor is nano which can also be typed from the command line.

Debugging

As your programs become more complex, using a debugger is essential to quickly finding bugs in code. See the following guides [Thanks to Abhinav Kumar]:

  1. CLion documentation and video

  2. Visual Studio documentation and video

  3. Xcode documentation and video

Make utility

Make is a tool to help manage dependencies between multiple files in a project. When one of the files is updated, the other files that depend on it are also updated.

gdb (Gnu Debugger)

gdb is a command-line debugger for C and C++ programs.

valgrind to find memory leaks

You will need to run valgrind in the UIC bertvm environment, as it doesn't provide the desired helpful error messages when run in Replit. See a tutorial by Prof. Bell on how to get started on UIC bertvm.


Valgrind is a debugging and profiling tool can automatically analyze a compiled C++ program for memory leaks and other bugs such as double deletes, unitialized variables, etc. Your program will slow down noticeably, so take appropriate steps, such as running with small input sizes. Tutorials for valgrind are available online, see, for example, valgrind's web site at valgrind.org including FAQs and the Quick Start Guide.

Here is an easy way to use valgrind. Suppose you have a compiled C++ executable program foo in the current working directory. Then running

valgrind ./foo

will run foo, printing the results of the checks valgrind did. If foo was compiled using the -g flag to g++, valgrind will be able to print the line numbers of statements that are involved in bugs that it has found. If want to see more detailed information about memory leaks, run

valgrind --tool=memcheck --leak-check=yes foo

The output from valgrind's analysis can be lengthy and can be hard to read, especially if your program is also printing to the terminal. You may find it more convenient to divert valgrind's output to a file rather than to the terminal.

valgrind --log-file=out ./foo

You can then read the file in the editor, or search it with grep.

Compare results to test cases

It can be difficult to see exactly where your output is different from the expected output. Use diffchecker.com to highlight the differences for you.

gprof to profile code

Gprof is a program profiler that allows you to see where your code is spending its time. This allows you to identify bottlenecks and write those sections more efficiently. On-line documentation is plentiful and here are a few sources of information:

Online C Compilers

  • replit.com allows creating C, C++, and bash scripts, along with a terminal window where UNIX commands can be entered.

  • onlinegdb.com/online_c_compiler Allows you to edit, compile, run, and beautify C/C++ programs online.

  • cpp.sh Allows you to edit, compile and run C++ programs.c

  • IDEone.com Online environment for C programming

UNIX Shell Commands


Some of this page's contents was taken from UCSD's page here.