You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// basic#include<unistd.h>// fork, pipe and I/O primitives (read, write, close, etc.) + primitve types like uid_t, pid_t etc#include<stdlib.h>// standard lib, contains primitves for number conversion and memory allocation#include<stdio.h>// basic i/o lib: printf etc#include<string.h>// string manipulations#include<time.h>// time related functions#include<signal.h>// signal handling#include<stdbool.h>// boolean type#include<math.h>// math functions// advanced#include<sys/socket.h>// socket connections#include<sys/types.h>// primitive types like uid_t, pid_t etc#include<netinet/in.h>// internet address family#include<arpa/inet.h>// definitions for internet operations#include<pthread.h>// threads#include<stdatomic.h>// mutual exclusion locks
Format Type Specifiers
specifier Output
%c Character
%s String of characters
%d or i Signed decimal integer
%f Decimal floating point
%llu unsigned long long
%o Signed octal
%u Unsigned decimal integer
%x Unsigned hexadecimal integer
%p Pointer address
File Modes
mode Description
"r" Opens a file for reading. The file must exist.
"w" Creates an empty file for writing. If a file with the same name already exists, its content is erased and the file is considered as a new empty file.
"a" Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist.
"r" Opens a file to update both reading and writing. The file must exist.
"w" Creates an empty file for both reading and writing.
"a" Opens a file for reading and appending.
Important Function Signatures and Return Values
Random Numbers
// seed with current time:// time_t t;// srand(time(&t));voidsrand(unsignedseed);
intrand(void);
voidqsort(void*values, size_tnum_items, size_titem_size, int (*comparefunc)(constvoid*, constvoid*));
Strings
// SUCCESS: pointer to destination stringchar*strcpy(char*dest, constchar*src);
// result == 0 -> strings are equal// result < 0 -> str1 less than str2// result > 0 -> str2 less than str1intstrcmp(constchar*str1, constchar*str2);;
// result == NULL -> no tokens left to retrieve// else: pointer to last token foundchar*strtok(char*str, constchar*delim);
// NULL if no match// else: pointer to first occurence in stringchar*strstr(constchar*string, constchar*substring);
// ERROR: result == 0intatoi(constchar*str);
Inter Process Communication
// ERROR: result < 0// result = 0 inside child// result > 0 inside parentpid_tfork(void);
// ERROR: result < 0// SUCCESS: result == 0intpipe(intfd[2]);
File IO
// ERROR: result == NULL (also when EOF is reached)char*fgets(char*str, intstrlen, FILE*stream);
// ERROR: result == NULLFILE*fopen(constchar*filename, constchar*mode);
// result == EOF when finished reading the stream// SUCCESS: number of matched items on successintfscanf(FILE*stream, constchar*format, ...);
// ERROR: result == EOF// SUCCESS: result == 0intfclose(FILE*stream);
// ERROR: result == -1// SUCCESS: number of bytes writtenssize_twrite(intfildes, constvoid*buf, size_tnbyte);
// ERROR: result == -1// EOF when finished reading// SUCCESS: number of bytes readssize_tread(intfildes, void*buf, size_tnbyte);
// SUCCESS: result > 0// ERROR: result == EOFintfputs(constchar*restrict s, FILE*restrict stream);
Shared Memory
// ERROR: result < 0// SUCCESS: result == shmidintshmget(key_tkey, size_tsize, intshmflg);
// ERROR: result == NULLvoid*shmat(intshmid, constvoid*shmaddr, intshmflg);
// ERROR: result < 0// SUCCESS: result == 0intshmdt(constvoid*shmaddr);
Networking
// ERROR: result == -1// example: sock = socket(AF_INET, SOCK_STREAM, 0);intsocket(intdomain, inttype, intprotocol);
// sockaddr_in struct (man ip 4)structsockaddr_inserver;
server.sin_family=PF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(8080);
// ERROR: result < 0// SUCCESS: result == 0// example: bind(sock, (struct sockaddr *) &server, sizeof(server))intbind(intsockfd, conststructsockaddr*my_addr, socklen_taddrlen);
// ERROR: result < 0// SUCCESS: filedeskriptor for accepted socket// example: fd = accept(sock, (struct sockaddr *) &client, &client_len);intaccept(intsockfd, structsockaddr*addr, socklen_t*addrlen);
// SUCCESS: result == 0// ERROR: result == -1intlisten(intsocket, intbacklog);
Threads
// ERROR: result > 0// SUCCESS: res == 0// example: pthread_create(&threads[t], NULL, printHello, (void*)t);intpthread_create(pthread_t*restrict thread, constpthread_attr_t*restrict attr, void*(*start_routine)(void*), void*restrict arg);
// SUCCESS: result == 0// ERROR: result > 0intpthread_join(pthread_ttid, void**ret);
voidpthread_exit(void*value_ptr);