C: Difference between revisions
Line 100: | Line 100: | ||
== Links == | == Links == | ||
* [http://phrack.org/issues/67/9.html A Eulogy for Format Strings] - Phrack article on exploiting string formatting in C. | |||
* [https://embeddedartistry.com/blog/2017/3/7/clang-weverything Warnings: -Weverything and the Kitchen Sink] | * [https://embeddedartistry.com/blog/2017/3/7/clang-weverything Warnings: -Weverything and the Kitchen Sink] | ||
* [https://stackoverflow.com/questions/4500158/how-can-i-force-linking-with-a-static-library-when-a-shared-library-of-same-name How can I force linking with a static library when a shared library of same name is present] | * [https://stackoverflow.com/questions/4500158/how-can-i-force-linking-with-a-static-library-when-a-shared-library-of-same-name How can I force linking with a static library when a shared library of same name is present] |
Revision as of 09:48, 8 May 2020
Versions
Three major revisions of C:
- ANSI C - also known as C89
- C99
- C11
Sample Makefile
CC=clang CFLAGS=-Weverything LDFLAGS= SOURCES=hello.c OBJECTS=$(SOURCES:.c=.o) EXECUTABLE=hello all: $(SOURCES) $(EXECUTABLE) $(EXECUTABLE): $(OBJECTS) $(CC) $(LDFLAGS) $(OBJECTS) -o $@ .c.o: $(CC) $(CFLAGS) -c $< -o $@ clean: rm -rf *.o $(EXECUTABLE)
Encapsulation
Using the static
keyword when defining a function will restrict that function to file-scope (likewise for variables).
Function parameters
Depending on the architecture, function parameters may be loaded into registers when the function is called. However, as the number of registers is small, even on RISC architectures, having more than 4-8 function parameters may impact performance.
Guaranteed data type sizes
Portable types such as uint16_t
(unsigned integer, 16 bits) allow data type sizes to be guaranteed across platforms and are defined in stdint.h
.
stdint.h
is only guaranteed to exist in C99 onwards.
Compiler options
-D
: Define this macro - e.g.-D_BSD_SOURCE
is the same as including#define _BSD_SOURCE 1
.
GCC warning flags
-Wall
: Enables a large number of warnings, though not all (despite the name).-Wextra
: Enables extra warnings above and beyond-Wall
.-Werror
: Make all warnings into errors. Effectively this means that code which produces warnings will fail to compile.
Clang warning flags
Clang supports most GCC warning flags, as well as some extras.
-Weverything
: All warnings, may include new warnings in future versions (so code may compile under-Weverything
now but not if clang is upgraded). If you include third party libraries, it is highly unlikely that your codebase will compile with this flag enabled.
Changes from C89 to C99
- Support for C++ style single-line comments, i.e.
// this is a comment
. - Variables no longer have to be declared before code, e.g.
for (int i = 0; i < 9, i++)
. - Support for variable length arrays, e.g.
int a[b][c]
.
Cygwin
C programs compiled under Cygwin will depend on cygwin1.dll
, regardless of whether any POSIX functions are used. You can distribute this DLL alongside your program, but if you do your source code must be made available.
To avoid having to release your source code, you need to remove any POSIX function calls (e.g. fork
), compile using MinGW and then use cygcheck
to ensure that your software no longer links against the cygwin1.dll
. Or you can pay RedHat lots of money for an opt-out on releasing your code.
cygcheck
has similar functionality to ldd
(Linux) and otool -L
(macOS).
Assertions
The debug-only macro assert
is available in assert.h
.
Embedded C
- Declaring a variable as
const
will cause it to be stored in the program code space rather than the limited variable storage space (e.g. RAM). - Use the
volatile
keyword for pointers to memory-mapped registers, and global variables that are shared between threads.
When laying out structs, order members in descending order of size. For example:
struct abc { char a; /* 1 byte */ short b; /* 2 bytes */ char c[5]; /* 5 bytes */ };
should be restructured as:
struct abc { char c[5]; /* 5 bytes */ short b; /* 2 bytes */ char a; /* 1 byte */ };
Links
- A Eulogy for Format Strings - Phrack article on exploiting string formatting in C.
- Warnings: -Weverything and the Kitchen Sink
- How can I force linking with a static library when a shared library of same name is present
- With Undefined Behavior, Anything is Possible
- Some Dark Corners of C
- Why does calloc exist? - Good explanation of the differences between
calloc
andmalloc
followed bymemset
. - How to C
- Understanding C by learning assembly
- Understanding glibc malloc
- strncpy? just say no
- A Guide to Undefined Behavior in C and C++
- The Lost Art of C Structure Packing
- Memory management in C programs
- Building C Projects
- Deep C
- To Become a Good C Programmer
- C11 atomic variables and the kernel
- The Definitive C Book Guide and List
- C: The Complete Nonsense - Critique of Herbert Schildt's C: The Complete Reference.
- The Annotated Annotated C Standard
- CProgramming.com
- comp.lang.c FAQ
- The Descent to C
- MSC06-CPP - CERT article on why your compiler might optimise away security functions.
- Does using SecureZeroMemory() really help to make the application more secure? - StackOverflow question, with some useful pointers in the answers.
- Setting variable to NULL after free - StackOverflow thread on avoiding dangling pointers.
- Open source development using C99
- Use of C99 Variable Length Arrays - Workaround for the fact that Visual C++ doesn't support variable length arrays.
- The Top 10 Ways to get screwed by the "C" programming language - Some mistakes to avoid.
- Linus Torvalds on Why C++ is a horrible language and C is the only sane choice
- A response to Linus Torvalds on C++
- What Is C For?
- Writing Insecure C, Part 1
- Writing Insecure C, Part 2
- Writing Insecure C, Part 3
- GCC Warning Options - The various warning options available in the GCC compiler.
- The Development of the C Language - History of the C language and its development, by Dennis Ritchie (the 'R' in 'K&R').
- GCC changes less than to less than or equal to
- Write a shell in C
- Incompatibilities Between ISO C and ISO C++
- How to Write Portable C Without Complicating Your Build