Skip to content

Latest commit

 

History

History
186 lines (134 loc) · 14.1 KB

File metadata and controls

186 lines (134 loc) · 14.1 KB

C

Compiling C with gcc

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.c

then to run the code in Ubuntu, other Linux distros, WSL, or MacOS

./a.out

or under Cygwin

./a.exe

Not 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.

More gcc flags

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++).

C Notes & Tutorials

Advanced C Tutorials/Examples

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.

Static Analysis

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

Memory Usage & Data Alignment

  • 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

Macros and Multi-file Programs

  • 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

#endif

to the end of the file.

Threads

Why Learn C

The History & Development of C

C Standards

ANSI C vs C11

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 */
}

Problems with C's Design

Object-Oriented Programming with 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.

Articles about C

Real-world C examples

Bugs & Exploits

  • 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.

C Coding Tips