Show Menu
Cheatography

C

read file char-b­y-char

#include <st­dio.h>
 
FILE *h;
int ch;
h = fopen(­"­fil­ena­me", "­rb");
/* error checking missing */
while ((ch = fgetc(h)) != EOF) {
 ­ ­ ­ /* deal with ch */
}
/* if needed test why last read failed */
if (feof(h) || ferror(h)) /* whatever */;
fclose(h);
You can replace fgetc(h) with getchar() to read from standard input.

read file line-b­y-line

#include <st­dio.h>

FILE *h;
char line[100];
h = fopen(­"­fil­ena­me", "­rb");
/* error checking missing */
while (fgets­(line, sizeof line, h)) {
 ­ ­ ­ /* deal with line */
}
/* if needed test why last read failed */
if (feof(h) || ferror(h)) /* whatever */;
fclose(h);

Flexible Array Member

How to declare a FAM?
By using empty brackets as the last member of a struct.
How to define the size for an object containg a FAM?
ptr = malloc­(sizeof *ptr + sizeof (FAMTY­PE[­wan­ted­siz­e]));
Do not use FAMs! They were known as struct hack before C99 and, now as then, feel like a dirty hack.

<st­dio.h> functions with a FILE pointer at the end

char *fgets­(char *, int, FILE *);
int fputc(int, FILE *);
int fputs(char *, FILE *);
size_t fread(void *, size_t, size_t, FILE *);
FILE *freop­en(char *, char *, FILE *);
size_t fwrite­(void *, size_t, size_t, FILE *);
int ungetc­(int, FILE *);
 

dynamic memory

Remember to #include <st­dli­b.h>
Allocate
malloc
ptr = malloc(n * sizeof *ptr);
calloc
ptr = calloc(n, sizeof *ptr);
Change size
realloc
newsize = n * sizeof *ptr; tmp = reallo­c(ptr, newsize); if (tmp) ptr = tmp; else /* ptr is still valid */;
Release
free
free(ptr);

remove trailing newline

How do I remove the final newline in a string?

len = strlen­(data);
if (len && data[len - 1] == '\n') data[-­-len] = 0;

or, if you don't need to keep and update data length

data[s­trc­spn­(data, "­\n")] = 0;
If len is known in advance, do not call strlen(). You can pass the updated len to the caller.

Casting

Casts in C are almost always wrong. When are they right?
<ct­ype.h>
isupper((unsigned char)ch)
%p printf specifiers
printf­("%p", (void*)ptr)
Specif­ically a cast to the return value of malloc() is a definite sign the code author either didn't know what he was doing or didn't choose a good language for the implem­ent­ation of whatever he's doing.

(BSD) sockets

Headers needed
#include <ar­pa/­ine­t.h>
#include <ne­tdb.h>
#include <st­rin­g.h>
#include <sy­s/s­ock­et.h­>
#include <un­ist­d.h>

initialize with
getadd­rinfo()

loop to find and connect a socket
socket()
connect()
if needed: close()
after loop: freead­dri­nfo()

getpee­rna­me(), getsoc­kname()
send() or recv() or sendto() or recvfrom()

close()
 

Predefined C macros

__FILE__
"­fil­ena­me.c­" or something like that
__LINE__
42 or another integer
__STDC__
1
__STDC­_VE­RSION__
undefined for C89; 199901L for C99; 201112L for C11
__DATE__
"Feb 17 2012" for example
__TIME__
"­15:­16:­17" for example
__func__
"­mai­n" for example
__STDC­_HO­STED__
0 or 1

Reserved identi­fiers

Reserved for all uses anywhere
_[A-Z]*; __*
E[A-Z]*; E[0-9]*
is[a-z]*; to[a-z]*
SIG[A-Z]*; SIG_[A-Z]*
LC_[A-Z]*
*_t
str[a-z]*; mem[a-z]*; wcs[a-z]*
all math functions possibly followed by f or l
When #include <li­mit­s.h> is present
*_MAX
When #include <si­gna­l.h> is present
SA_*
sa_*
POSIX adds a few other identi­fiers
<di­ren­t.h>
d_*
<fc­ntl.h>
l_*; F_*; O_*; S_*
<gr­p.h>
gr_*
<pw­d.h>
pw_*
<sy­s/s­tat.h>
st_*; S_*
<sy­s/t­ime­s.h>
tms_*
<te­rmi­os.h­>
C_*; V_*; I_*; O_*; TC*; B[0-9]*
           
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Regular Expressions Cheat Sheet
          Python Cheat Sheet