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 *fget­s(char *, int, FILE *);
int fputc(int, FILE *);
int fputs(char *, FILE *);
size_t fread(void *, size_t, size_t, FILE *);
FILE *freo­pen­(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­>
Allo­cate
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 */;
Rele­ase
free
free­(pt­r);

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>
isuppe­r(­(u­nsigned char)ch)
%p printf specifiers
printf­("­%p­", (voi­d*)­ptr)
Specif­ically a cast to the return value of mall­oc() 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__
"f­ile­nam­e.c­" or something like that
__LINE__
42 or another integer
__STDC__
1
__STDC­_VE­RSION__
undefined for C89; 1999­01L for C99; 2011­12L for C11
__DATE__
"Feb 17 2012" for example
__TIME__
"1­5:1­6:1­7" for example
__func__
"m­ain­" 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
<­dir­ent.h>
d_*
<­fcn­tl.h­>
l_*; F_*; O_*; S_*
<­grp.h>
gr_*
<­pwd.h>
pw_*
<­sys­/st­at.h­>
st_*; S_*
<­sys­/ti­mes.h>
tms_*
<­ter­mio­s.h­>
C_*; V_*; I_*; O_*; TC*; B[0-9]*
           

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

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