Data types and Variables
Arrays |
|
|
|
|
|
Pointers |
|
|
|
|
Declares p a pointer. |
|
|
Declares pointer to the variable var. |
|
|
Declares pp a pointer to a pointer. |
|
|
Declares pointer to the pointer p. |
Structures |
|
|
struct tag_name {
<type> <element1>; <type> <element2>; } |
Structures allow a programmer to have a collection of elements of different types representing something. |
Formatting
Escape Characters |
|
|
Alert bell |
|
Backspace |
|
Newline |
|
Backslash |
|
Double quote |
|
Question Mark |
Conversion Specifiers |
|
|
char |
|
string |
|
int |
|
unsigned int |
|
long int |
|
octal |
|
hexadecimal |
|
double |
Formatted I/O |
|
|
Width of the printed field. ie. '123.5'
becomes ' 123.50'
. |
|
Fills unused space with zeros. ie. 21
becomes 0021
. |
|
Aligns the output to the right. |
|
Remove all characters but vowels. |
|
Remove all the vowels. |
|
Eliminate unnecessary characters. ie. 1/1/2001
can be stored in three integers as 1
, 1
and 2001
. |
Example:int integer = 1; printf("This is an integer: %d", integer);
Dynamic memory allocation
|
Allocates size contiguous bytes of memory and returns a void pointer to the first byte allocated. |
void* calloc(int items, int size)
|
Allocates iitems x size contiguous cleared (set to 0) bytes of memory and returns a void pointer to the first byte allocated. |
void* calloc(void* ptr, int new_size)
|
Resizes allocated memory being pointed at by ptr to be size bytes and returns a void pointer to the first byte allocated. |
|
Frees memory that is pointed at by ptr. |
Linked lists
struct node { int x; struct node* next; }; ... struct node* head;
|
A structure pointing to other nodes. The first element is assigned to a pointer head. |
struct node* ptr = head; while(ptr!= NULL){ printf("%d\n", ptr->x); ptr = ptr->next; }
|
Traversing the list. |
while(head != NULL){ ptr = head->next;{ free(head);{ head = ptr;{ }
|
Deleting an element. You need to free the elements in the right order. |
if(ptr == NULL){ ptr = malloc(sizeof(struct node)); ptr->x = 4; ptr->next = NULL; head = ptr; }
|
Adding an element. You need to cycle to the end first. |
A linked list is a dynamic data structure consisting of a sequence of records where each element contains a link to the next record. They can be linked singularly, doubly or circularly. Every node has a payloead and a link to the next node. The end is indicated by a NULL pointer. It needs a pointer to the first item in the list.
|
|
Basics of C
General functions |
|
|
|
|
Obtains character from input stream |
|
|
Returns size in bytes of |
Mathematical functions |
|
|
|
|
Square root of x |
|
|
x raised to the power of y |
|
|
Absolute value of x |
|
|
Rounds x to the smallest int no less than x |
|
|
Rounds x to the largest int not greater than x |
Command line Arguments |
|
|
int main (int argc, char* argv[]){ /*code*/ }
|
int main (int argc, char* argv[]){ /*code*/ }
Pre-processing
Pre-processor identifiers |
|
|
Current line being compiled. |
|
Name of source file. |
|
Date of compilation (mm dd yy). |
|
Time of compilation (hh:mm:ss) |
Macros |
|
|
The contents of #include
are read and merged into the file. |
|
Define a variable. |
#ifdef DEBUG expression #endif
|
Define a variable. |
#ifdef DEBUG expression #endif
|
Conditional compilation can be turned on by both setting #define DEBUG 1
or by -D
in the command line. |
#ifdef condition #error "Error message" #endif
|
Prints text as error message. |
Function-like macros are pre-processed and have no type checking and are not checked for compilation errors, but are executed faster than normal C functions.
Strings
|
|
Prints formattedoutput to stdout |
|
|
Reads formatted input from stdin |
|
|
Writes a string to stdout up to but not including the null character. A newline character is appended to the output. |
|
fgets(char *str, int n, FILE *stream)
|
Reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first. |
|
strcpy(char dest, char src)
|
Pass a string to another string variable. |
|
strcat(char *dest, char *src)
|
Appends the string pointed to by src to the end of the string pointed to by dest |
|
|
Computes the length of the string str up to, but not including the terminating null character. |
Sockets
int socket(int domain, int type, int protocol)
|
Creates a socket. |
|
Closes a socket. |
int bind(int sockid, struct sockaddr* addr, int addrlen)
|
Selects the port which is going to be used and reserves it for use by the socket. It can be skipped for TCP and UDP sockets. |
int listen(int sockid, int backlog)
|
Listens for connections. It's only used by a TCP server. |
int accept(int sockid, struct sockaddr* clientAddr, int* addrlen)
|
Establishes a connection for a TCP server. addrlen should be set to sizeof(clientAddr)
. |
int connect(int sockid, struct sockaddr* serverAddr, int addrlen)
|
Establishes a connection for a TCP client. addrlen should be set to sizeof(clientAddr)
. |
int send(int sockid, void* msg, int len, int flags)
|
Sends a message to a TCP client/server with length len
. |
int recv(int sockid, void* buffer, int len, int flags)
|
Receives a message from a TCP client/server with length len
. |
int sendto(int sockid, void* msg, int len, int flags, struct sockaddr* foreign, int addrlen)
|
Sends a message to a UDP client/server with length len
. |
int recvfrom(int sockid, void* msg, int len, int flags, struct sockaddr* foreign, int addrlen)
|
Receives a message from a UDPclient/server with length len
. |
|