We will be using gcc for CSC240. Always compile your code using the following command line options to ensure you are writing standard C code:
gcc --std=c11 -pedantic -Wall filename.cthen to run the code in Ubuntu, other Linux distros, WSL, or MacOS
./a.outor under Cygwin
./a.exeNot using gcc with these compile flags may hide errors that result in a grade of 0 on your project! Also, you should use gcc version 4.7 or later. Earlier versions may be missing some features you will need for this class.
The flags above are required, bug gcc has a lot of other flags you may want to use to help you find potential bugs in your code.
- Recommended gcc flags on Stack Overflow
- This blog post mentions some other warning flags you may want to enable to help you avoid common mistakes (applies to gcc & g++).
- A quick video walking through the C basics in 25 minutes
- Essential C from The Stanford CS Education Library has an excellent summary of the C language. It covers most of what we will discuss about C this semester. There are also PDFs about pointers, linked lists, and binary trees in C.
- The C FAQ has a great discussion of pointers vs. arrays.
- Beej's Guide to C Programming
- If you are looking for a C book, here is StackOverflow's Definitive C Book Guide & List
- A guide to building (compiling) programs with gcc
- The right-left rule for deciphering C declarations.
- A C Reference Manual from Bell Labs (by Dennis Ritchie)
- C Operator Precedence
- The Descent to C is a nice intro to C for Java programmers
- If you need to split up a string in to words (tokens) you can use
strtok. Here is a tutorial - Efficient String Operations in C - specifically copying & concatenation
- VIDEO: What Are Pointers is an excellent introduction to pointers. The video uses C++ but the first half of the video is applicable to C as well.
- A video on Pointers that is part of a C programming tutorial. There are several other videos on pointer topics in the playlist following this video.
- The Clockwise/Spiral Rule is a technique to help you understand what is going on with a C declaration.
The links below go far more in depth into C than we will in this class, but if you are interested in C you may find them interesting.
- Inside the C Standard Library
- I don’t have strong opinions about coding style, other than your code should be clear & consistently formatted. But you may want to review the Linux coding standards
- A list of obscure C features(that you should probably avoid, but you may see out in the wild)
- Back to Basics discusses how C strings and malloc work and why it is important to know it.
- Signal handling in C
See the C++ Information page for information about cppcheck which is the static analyzer I'll be using on your C & C++ assignments.
- Splint is a static analyzer for security vulnerabilities and general coding issues
- Recursion, Run time Environment, & the Call Stack shows how function calls work in C
- Some lecture notes about how C uses memory(Unix oriented, but the first section is applicable anywhere)
- We will talk in class about how C structs sometime waste space due to word alignment. The Lost Art of Structure Packing is a good article about how rearranging your structs can wind up saving a lot of space in your program. There are a few things we probably won't cover about in the article, but you should know enough C by the end to understand most of it.
- A bug report related to how data is aligned in memory
- There is some good files & the pre-processor in the GNU libc manual and the gcc manual
- To prevent a file from being included multiple times in the same program, we add
#ifndef __PERSONNEL_H__
#define __PERSONNEL_H
to the beginning of the file and
#endifto the end of the file.
- A great video of the roots of C, going back to the first electronic computers, is Olve Maundal’s talk History & Spirit of C
- An article on how the C language has evolved over the years
- An overview of 5 C standards
- A series of blog posts on the differences between C90 & C99
- C11 The new standard
- C23: a slightly better C
- C23 is Finished: Here is What is on the Menu
For Loops: In ANSI C (C89) you cannot declare the loop variable in the for loop - you have to declare it before the for statement like this:
int ii;
for (ii=0;ii<10;ii++) {
/* loop stuff */
}NOT like this:
for (int ii=0;ii<10;ii++) {
/* loop stuff */
}- The Most Expensive One-Byte Mistake about is null-terminated strings.
- Tony Hoare calls null references/pointers a Billion Dollar Mistake.
- Conflating pointers with arrays is another thing that causes confusion.
- C's array notation is a lie
- So you think you know C? is a short quiz that points out some places where undefined behavior pops up in C.
- Defining the Undefinededness of C has some interesting discussion about undefined behavior (focused more on C11).
- A list of breaking changes in C11 has information about features in ANSI C that no longer work in C11.
- Why C & C++ Are Awful Programming Languages is written from a teaching perspective, but has some interesting points about both C & C++.
- The paper The Case for Writing Network Drivers in High-Level Programming Languages argues that network drivers should be written in higher level languages
- C11 Defects - C threads are not realizable with POSIX threads
- How Do I Declare a Function Pointer in C?
Axel-Tobias Schreiner has written a book on Object Oriented Programming in C (available for download under the "books" link). The book touches on some advanced topics we will likely not have time for this semester, but it is interesting if only for the fact that there is no "++" at the end of the book title. While we will discuss many programming language paradigms CSC240, it is important to keep in mind that you can take the techniques from any paradigm and apply them to your language of choice.
- Rob Pike's Notes on Programming in C. His 5 Rules for Programming are also worth reading.
- C Puzzles has some interesting (very advanced) examples of C programs with surprising output.
- Is C still relevant in the 21st Century? has some good points, though it glosses over C's use in embedded systems and micro-controllers which is probably where the bulk of future C programming will happen.
- f() vs f(void) function declarations in C & C++
- The Top 10 Ways to Get Screwed by the C Programming Language (though most of these are the result of poor style or practice on the part of the programmer!)
- What Every Programmer Should Know About Undefined Behavior in C (and part 2 & part 3).
- A Guide to Undefined Behavior in C
- An interview with Ken Thompson who was one of the developers of UNIX and who was involved in C in the early days.
- D as a better C - The D language was developed to address some of the shortcomings of C. This article compares C with a subset of D. Some interesting things (from a programming language theory standpoint) are addressed in this article.
- C the Immortal Language - C is still the dominant language in embedded systems
- Some principles for programming in C. A lot of these apply to any language.
- An implementation of the movie Inception in C
- A Proposal for a Friendly Dialect of C suggests some features that would make C program safer.
- Don’t write your code like this, but the International Obfuscated C Contest has some interesting examples of valid C code.
- How C array sizes became part of the binary interface of a library
- The Python compiler is written in C. This online class on the CPython compiler/runtime has some excellent examples of using C.
- The DOOM source code is available on GitHub.
- SQLite is a very small database engine that is included as a library in a lot of languages, including Python and Java. This code walk through touches points out some good programming practices that show up in the source code.
- A guide to low-level programming in C on the Raspberry Pi.
- String-matching using C & Assembly
- VIDEO Programming A Chess Engine in C (the first video in a series)
- A few years ago Cloudflare reported a bug (found by Google) that exposed memory locations which contained data that had gone out of scope. The C related description starts in the “Root cause of the bug” section.
- Leaving braces off a single line loop is usually not a good idea. Doing that let to the Apple SSL/TLS bug from a few years ago
- Manual Control Flow Guards are a Windows feature that help avoid buffer overflow exploit.
- Some good C Programming Tips from Philip Guo who taught the Python source code class listed above.
- How to shoot yourself in the foot with C