Cheat Sheet by pmg (pmg) via cheatography.com/596/cs/255/

read file char-by​ -char functions with a FILE pointer Casting at the end #include Casts in C are almost always wrong. When are they char *fgets​ (char *, int, FILE *); right?

FILE *h; int fputc(int, FILE *); isupper​ ((​ un​ signed int ch; int fputs(char *, FILE *); char)ch) h = fopen("​ f​ ile​ nam​ e", "​ b"); size_t fread(void *, size_t, size_t, FILE *); %p printf printf(​ "%​ p"​ , (void​ *)​ptr) specifiers /* error checking missing */ FILE *freop​ en(​ char *, char *, FILE *); while ((ch = fgetc(h)) != EOF) { size_t fwrite(​ void *, size_t, size_t, FILE *); Specifi​ cally a cast to the return value of mallo​ c() is a definite sign the code author ​ ​ ​ /* deal with ch */ int ungetc(​ int, FILE *); either didn't know what he was doing or } didn't choose a good language for the dynamic memory /* if needed test why last read failed */ impleme​ nta​ tion of whatever he's doing. Remember to #include ​ if (feof(h) || ferror(h)) /* whatever */; Alloc​ ate (BSD) sockets fclose(h); malloc ptr = malloc(n * sizeof *ptr); Headers needed You can replace fgetc(h) with getchar() to #include read from standard input. calloc ptr = calloc(n, sizeof *ptr); #include Change size #include read file line-by​ -line realloc newsize = n * sizeof *ptr; tmp = #include ​ #include realloc​ (ptr, newsize); if (tmp) ptr #include FILE *h; = tmp; else /* ptr is still valid */; initialize with char line[100]; Relea​ se getaddr​ info() h = fopen("​ f​ ile​ nam​ e", "r​ b"); free free(​ ptr​ ); loop to find and connect a socket /* error checking missing */ socket() while (fgets(​ line, sizeof line, h)) { remove trailing newline connect() ​ ​ ​ /* deal with line */ if needed: close() How do I remove the final newline in a } after loop: freeadd​ rin​ fo() string? /* if needed test why last read failed */ getpeer​ nam​ e(), getsock​ name() len = strlen(​ data); if (feof(h) || ferror(h)) /* whatever */; send() or recv() or sendto() or recvfrom() if (len && data[len - 1] == '\n') data[--​ len] = fclose(h); close() 0; or, if you don't need to keep and update Flexible Array Member Predefined C macros data length How to declare a FAM? data[st​ rcs​ pn(​ data, "\​ n")] = 0; __FILE__ By using empty brackets as the last If len is known in advance, do not call "fi​ len​ ame​ .c"​ or something like that member of a struct. strlen(). You can pass the updated len to __LINE__ How to define the size for an object the caller. 42 or another integer containg a FAM? ptr = malloc(​ sizeof *ptr + sizeof (FAMTY‐​ PE[w​ ant​ eds​ ize​ ]));

Do not use FAMs! They were known as struct hack before and, now as then, feel like a dirty hack.

By pmg (pmg) Published 17th February, 2012. Sponsored by ApolloPad.com cheatography.com/pmg/ Last updated 1st March, 2020. Everyone has a novel in them. Finish Page 1 of 2. Yours! https://apollopad.com C Cheat Sheet by pmg (pmg) via cheatography.com/596/cs/255/

Predefined C macros (cont)

__STDC__

1

__STDC_​ VER​ SION__

undefined for C89; 19990​ 1L for C99; 20111​ 2L for C11

__DATE__

"Feb 17 2012" for example

__TIME__

"15​ :16​ :17​ " for example

__func__

"ma​ in"​ for example

__STDC_​ HOS​ TED__

0 or 1

Reserved identif​ iers

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 ​ is present *_MAX

When #include ​ is present SA_* sa_* POSIX adds a few other identif​ iers

d_* ​ l_*; F_*; O_*; S_* gr_* pw_* ​ st_*; S_* tms_* ​ C_*; V_*; I_*; O_*; TC*; B[0-9]*

By pmg (pmg) Published 17th February, 2012. Sponsored by ApolloPad.com cheatography.com/pmg/ Last updated 1st March, 2020. Everyone has a novel in them. Finish Page 2 of 2. Yours! https://apollopad.com