C Notes

From SubfireWiki

Jump to: navigation, search

I've been doing a bit more C programming lately, and have come across a few features which surprised me.

Contents

Syntax

Types

char vs. unsigned char

Don't forget that chars are signed, so anything bigger than 0x7F will appear negative unless it's typed as unsigned char

size_t: int or long?

On ia32, size_t is a 32bit unsigned int, on amd64, size_t is a 64bit unsigned int. Due to typedefery, this leads to (otherwise) perfectly good printf()s generating warnings when compiling on amd64 because "%i" doesn't apply size_t on amd64. Using "%li" for size_t should be fine.

Standard Libraries

qsort

qsort obviously will write to the data structure. This can lead to segfaults if a consted array is passed in (at least when using gcc 4.1.3 with the C99 standard).

General ponderings

#include in what order?

Should system includes come before or after local includes?

I.e., which is "better",

#include <stdio.h>
#include "mytool.h"

or

#include "mytool.h"
#include <stdio.h>

?

Links/See also

Reading complicated C declarations

Personal tools