C Cheat Sheet by Pmg (Pmg) Via Cheatography.Com/596/Cs/255
Total Page:16
File Type:pdf, Size:1020Kb
C Cheat Sheet by pmg (pmg) via cheatography.com/596/cs/255/ read file char-by -char <std io.h> functions with a FILE pointer Casting at the end #include <std io.h> Casts in C are almost always wrong. When are they char *fgets (char *, int, FILE *); right? FILE *h; int fputc(int, FILE *); <cty pe.h> isupper (( un signed int ch; int fputs(char *, FILE *); char)ch) h = fopen(" f ile nam e", "r 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 <std lib .h> 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 <arp a/i net .h> read from standard input. calloc ptr = calloc(n, sizeof *ptr); #include <net db.h> Change size #include <str ing .h> read file line-by -line realloc newsize = n * sizeof *ptr; tmp = #include <sys /so cke t.h> #include <std io.h> realloc (ptr, newsize); if (tmp) ptr #include <uni std .h> 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 C99 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 <lim its .h> is present *_MAX When #include <sig nal .h> is present SA_* sa_* POSIX adds a few other identif iers <d ire nt.h> d_* <f cnt l.h> l_*; F_*; O_*; S_* <g rp.h> gr_* <p wd.h> pw_* <s ys/ sta t.h> st_*; S_* <s ys/ tim es.h> tms_* <t erm ios .h> 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.