C: Difference between revisions
No edit summary |
|||
(13 intermediate revisions by the same user not shown) | |||
Line 55: | Line 55: | ||
Clang supports most GCC warning flags, as well as some extras. | Clang supports most GCC warning flags, as well as some extras. | ||
* <code>-Weverything</code>: All warnings, may include new warnings in future versions (so code may compile under <code>-Weverything</code> now but not if clang is upgraded). | * <code>-Weverything</code>: All warnings, may include new warnings in future versions (so code may compile under <code>-Weverything</code> 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 == | == Changes from C89 to C99 == | ||
Line 100: | Line 100: | ||
== Links == | == Links == | ||
* [https://zig.news/kristoff/cross-compile-a-c-c-project-with-zig-3599 Cross-compile a C/C++ Project with Zig] | |||
* [https://kristoff.it/blog/maintain-it-with-zig/ Maintain it with Zig] | |||
* [https://www.sqlite.org/whyc.html Why Is SQLite Coded In C] | |||
* [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://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://raphlinus.github.io/programming/rust/2018/08/17/undefined-behavior.html With Undefined Behavior, Anything is Possible] | |||
* [https://docs.google.com/presentation/d/1h49gY3TSiayLMXYmRMaAEMl05FaJ-Z6jDOWOz3EsqqQ/preview#slide=id.gec7eb408_3500 Some Dark Corners of C] | * [https://docs.google.com/presentation/d/1h49gY3TSiayLMXYmRMaAEMl05FaJ-Z6jDOWOz3EsqqQ/preview#slide=id.gec7eb408_3500 Some Dark Corners of C] | ||
* [https://vorpus.org/blog/why-does-calloc-exist/ Why does calloc exist?] - Good explanation of the differences between <code>calloc</code> and <code>malloc</code> followed by <code>memset</code>. | * [https://vorpus.org/blog/why-does-calloc-exist/ Why does calloc exist?] - Good explanation of the differences between <code>calloc</code> and <code>malloc</code> followed by <code>memset</code>. | ||
Line 114: | Line 121: | ||
* [http://lwn.net/Articles/586838/ C11 atomic variables and the kernel] | * [http://lwn.net/Articles/586838/ C11 atomic variables and the kernel] | ||
* [http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list The Definitive C Book Guide and List] | * [http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list The Definitive C Book Guide and List] | ||
* [http://www.seebs.net/c/c_tcn4e.html C: The Complete Nonsense] - Critique of Herbert Schildt's | * [http://www.seebs.net/c/c_tcn4e.html C: The Complete Nonsense] - Critique of Herbert Schildt's ''C: The Complete Reference''. | ||
* [http://www.davros.org/c/schildt.html The Annotated Annotated C Standard] | * [http://www.davros.org/c/schildt.html The Annotated Annotated C Standard] | ||
* [http://www.cprogramming.com/ CProgramming.com] | * [http://www.cprogramming.com/ CProgramming.com] | ||
Line 133: | Line 140: | ||
* [http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html GCC Warning Options] - The various warning options available in the GCC compiler. | * [http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html GCC Warning Options] - The various warning options available in the GCC compiler. | ||
* [http://cm.bell-labs.com/cm/cs/who/dmr/chist.html The Development of the C Language] - History of the C language and its development, by Dennis Ritchie (the 'R' in 'K&R'). | * [http://cm.bell-labs.com/cm/cs/who/dmr/chist.html The Development of the C Language] - History of the C language and its development, by Dennis Ritchie (the 'R' in 'K&R'). | ||
* [https://stackoverflow.com/questions/15718021/gcc-changes-less-than-to-less-than-or-equal-to GCC changes less than to less than or equal to] | |||
* [https://news.ycombinator.com/item?id=13112589 Write a shell in C] | |||
* [http://www.david.tribble.com/text/cdiffs.htm Incompatibilities Between ISO C and ISO C++] | |||
* [https://nullprogram.com/blog/2017/03/30/ How to Write Portable C Without Complicating Your Build] | |||
* [https://begriffs.com/posts/2019-01-19-inside-c-standard-lib.html Inside the C Standard Library] | |||
[[Category:Programming]] | [[Category:Programming]] |
Latest revision as of 08:19, 5 June 2023
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
- Cross-compile a C/C++ Project with Zig
- Maintain it with Zig
- Why Is SQLite Coded In C
- 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
- Inside the C Standard Library