diff options
Diffstat (limited to 'dev/c')
-rw-r--r-- | dev/c/datatypes.html | 178 | ||||
-rw-r--r-- | dev/c/debugging.html | 99 | ||||
-rw-r--r-- | dev/c/elements.html | 63 | ||||
-rw-r--r-- | dev/c/index.html | 199 | ||||
-rw-r--r-- | dev/c/lib.html | 255 |
5 files changed, 728 insertions, 66 deletions
diff --git a/dev/c/datatypes.html b/dev/c/datatypes.html new file mode 100644 index 0000000..35b6ca4 --- /dev/null +++ b/dev/c/datatypes.html @@ -0,0 +1,178 @@ +<!DOCTYPE html> +<html dir="ltr" lang="en"> + <head> + <meta charset='utf-8'> + <title>Datatypes</title> + </head> + <body> + <a href="../index.html">Development Index</a> + + <h1>Datatypes</h1> + + <h2 id="types">Types</h2> + + <dl> + <dt>char</dt> + <dd>Integer, one byte.</dd> + <dt>int</dt> + <dd>Integer.</dd> + <dt>float</dt> + <dd>Single precision floating point.</dd> + <dt>double</dt> + <dd>Double precision floating point.</dd> + <dt>void</dt> + <dd>Absence of type.</dd> + </dl> + + + <h2 id="datatypes">Data types</h2> + + <h2 id="int">Integer</h2> + <p>Allowed <a href="elements.html#types">types</a> are char and int;</p> + <dl> + <dt>signed char</dt> + <dd>8 bit, from -128 to 127.</dd> + <dt>unsigned char</dt> + <dd>8 bit, from 0 to 255.</dd> + <dt>char</dt> + <dd>8 bit, used to store ASCII, ex; 'h', '\n', etc...</dd> + <dt>short int</dt> + <dd>16 bit, from -32,768 to 32,767.</dd> + <dt>unsigned short int</dt> + <dd>16 bit, from 0 to 65,535.</dd> + <dt>int</dt> + <dd>32 bit, from -2,147,483,648 to 2,147,483,647.</dd> + <dt>unsigned int</dt> + <dd>32 bit, from 0 to 4,294,967,295.</dd> + <dt>long int</dt> + <dd>32 bit, from -2,147,483,648 to 2,147,483,647.</dd> + <dt>unsigned long int</dt> + <dd>32 bit, from 0 to 4,294,967,295.</dd> + <dt>long long int</dt> + <dd>64 bit, from 9,223,372,036,854,775,808 + to 9,223,372,036,854,775,807.</dd> + <dt>unsigned long long int</dt> + <dd>64 bit, from 0 to 18,446,744,073,709,551,615.</dd> + </dl> + + <h2 id="double">Real number</h2> + + <p>Real numbers are fractional numbers, all floating points + are signed. Check float.h.</p> + + <dl> + <dt>float</dt> + <dd>From 1e-37 to 1e37</dd> + <dt>double</dt> + <dd>Largest of floating point.</dd> + <dt>long double</dt> + <dd>The long double data maybe larger than double</dd> + </dl> + + <h2 id="complex">Complex number</h2> + + <p><a href="https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Standard-Complex-Number-Types">Complex Number Types</a></p> + + <h2 id="enum">Enumeration</h2> + + <pre> + enum colors {black, orange, blue} color; + enum colors color; + </pre> + + <h2 id="union">Unions</h2> + + <pre> + union accounts { + int id; + float value; + } first_account, second_account; + </pre> + + <h2 id="struct">Structures</h2> + + <pre> + struct point { + int x, y; + } first_point; + struct point second_point; + </pre> + + <h2 id="array">Arrays</h2> + + <pre> + int account_array[9]; + int account_array[] = {0, 1, 2, 3}; + int account_array[] = {0, 1, 2, [9] = 9}; + </pre> + + <pre> + int account_array[2][9] { {1,2,3},{9,8,7} } + </pre> + + <p>Initializing string with individual characters the + null character must be defined;</p> + + <pre> + char black[] = {'b', 'l', 'a', 'c', 'k', '\0'} + </pre> + + <pre> + char black[] = "black"; + </pre> + + <pre> + union accounts { + int id; + float value; + }; + union accounts accounts_array[9]; + accounts_array[0].id = 8; + </pre> + + <pre> + struct point { + int x, y; + }; + struct point point_array[9]; + point_array[0].x=2; + point_array[0].y=3; + </pre> + + <h2 id="pointer">Pointers</h2> + <h2 id="it">Incomplete types</h2> + <h2 id="tq">Type qualifiers</h2> + <h2 id="st">Storage class</h2> + + <h2 id="format">Format Type Specifiers</h2> + + <dl> + <dt>%c</dt> + <dd>Character</dd> + <dt>%s</dt> + <dd>String of characters</dd> + <dt>%d</dt> + <dd>Decimal integer</dd> + <dt>%f</dt> + <dd>Decimal floating point</dd> + <dt>%llu</dt> + <dd>unsigned long long</dd> + <dt>%o</dt> + <dd>Signed octal</dd> + <dt>%u</dt> + <dd>Unsigned decimal integer</dd> + <dt>%x</dt> + <dd>Unsigned hexadecimal integer</dd> + <dt>%p</dt> + <dd>Pointer address</dd> + </dl> + + <a href="../index.html">Development Index</a> + <p>This is part of the c9 Manual. + Copyright (C) 2018 + c9 team. + See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a> + for copying conditions.</p> + + </body> +</html> diff --git a/dev/c/debugging.html b/dev/c/debugging.html new file mode 100644 index 0000000..4e5931f --- /dev/null +++ b/dev/c/debugging.html @@ -0,0 +1,99 @@ +<!DOCTYPE html> +<html dir="ltr" lang="en"> + <head> + <meta charset='utf-8'> + <title>Debugging</title> + </head> + <body> + <a href="../index.html">Development Index</a> + + <h1>Debugging</h1> + + <p>If the program needs arguments you can set it;</p> + + <pre> + (gdb)set args -parameter1 -parameter2 + </pre> + + <p>To start gdb in TUI mode press;</p> + + <dl> + <dt>Ctrl-x A</dt> + <dd>Enter or leave TUI.</dd> + <dt>Ctrl-x 0</dt> + <dd>TUI with only one window.</dd> + <dt>Ctrl-x 2</dt> + <dd>TUI with more than two windows.</dd> + <dt>Ctrl-x o</dt> + <dd>Change active window.</dd> + <dt>Ctrl-x s</dt> + <dd>TUI single key mode.</dd> + <dt>Ctrl-L s</dt> + <dd>Refresh screen.</dd> + <dt>Up</dt> + <dd>Scroll</dd> + <dt>Down</dt> + <dd>Scroll</dd> + <dt>Left</dt> + <dd>Scroll</dd> + <dt>Right</dt> + <dd>Scroll</dd> + </dl> + + <pre> + (gdb) info win + (gdb) fs next + (gdb) fs SRC + </pre> + + + <pre> + b - backtrace + info locals + display + print + x + catch syscall open + </pre> + + <p>When new thread is created you receive + a notification. To get information about + threads;</p> + + <pre> + info threads + </pre> + + <p>To select thread;</p> + + <pre> + thread 1 + </pre> + + <pre> + break linespec thread threadno + </pre> + + <pre> + strace -c ./program + </pre> + + + <p><a href="http://blog.fourthbit.com/2013/06/18/creating-an-open-source-program-in-c-with-autotools-part-1-of-2/">C program with autotools</a> + <a href="http://web.eecs.umich.edu/~sugih/pointers/gdbQS.html">GDB Quick Start</a>, + <a href="https://www.hackerschool.com/blog/5-learning-c-with-gdb">Learning C with GDB</a> + and <a href="http://www.dirac.org/linux/gdb/02a-Memory_Layout_And_The_Stack.php">Memory Layout and the Stack</a> + are great sources of introductory information. + <a href="http://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_39.html#SEC40">Stopping and Starting</a> + multi-thread programs</p> + + <a href="../index.html">Development Index</a> + + <p> + This is part of the c9-doc Manual. + Copyright (C) 2018 + c9 team. + See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a> + for copying conditions.</p> + </body> +</html> diff --git a/dev/c/elements.html b/dev/c/elements.html new file mode 100644 index 0000000..c5eb87e --- /dev/null +++ b/dev/c/elements.html @@ -0,0 +1,63 @@ +<!DOCTYPE html> +<html dir="ltr" lang="en"> + <head> + <meta charset='utf-8'> + <title>Elements</title> + </head> + <body> + <a href="../index.html">Development Index</a> + + <h1>Elements</h1> + + <h2 id="ident">Identifiers</h2> + + <p>Variable names, functions, data types and preprocessor macros. + Are allowed letters, decimal digits and underscore.</p> + + <h2 id="keywords">Keywords</h2> + + <p>Reserved identifiers part of C language.;</p> + + <p>auto break case char const continue default do double else enum + extern float for goto if int long register return short signed + sizeof static struct switch typedef union unsigned void volatile + while</p> + + <p>restrict inline _Bool _Complex _Imaginary</p> + + <p>__FUNCTION__ __PRETTY_FUNCTION__ __alignof __alignof__ __asm + __asm__ __attribute __attribute__ __builtin_offsetof __builtin_va_arg + __complex __complex__ __const __extension__ __func__ __imag __imag__ + __inline __inline__ __label__ __null __real __real__ + __restrict __restrict__ __signed __signed__ __thread __typeof + __volatile __volatile__</p> + + <h2 id="const">Constants</h2> + + <p>Constants are identifiers with fixed values that can't change + during execution. Data types of constants; integer, character, + double, strings.</p> + + <h2 id="op">Operators</h2> + + <p>Operators do operations such as; addition or subtraction.</p> + + <h2 id="sep">Separators</h2> + + <p>Language elements separators;<p> + <p>{} [] () ; , . :</p> + + <h2 id="white">White Space</h2> + + <p>White space is ignored except when separating elements.</p> + + <a href="../index.html">Development Index</a> + + <p> + This is part of the c9-doc Manual. + Copyright (C) 2018 + c9 team. + See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a> + for copying conditions.</p> + </body> +</html> diff --git a/dev/c/index.html b/dev/c/index.html index 1c3b478..aeff035 100644 --- a/dev/c/index.html +++ b/dev/c/index.html @@ -1,102 +1,169 @@ <!DOCTYPE html> <html dir="ltr" lang="en"> <head> - <meta charset='utf-8'> - <title>C & GDB</title> + <meta charset='utf-8'> + <title>C & GDB</title> </head> <body> - <a href="../index.html">Development Index</a> + <a href="../index.html">Development Index</a> - <h1>C & GDB</h1> + <h1>C & GDB</h1> - <p><a href="http://blog.fourthbit.com/2013/06/18/creating-an-open-source-program-in-c-with-autotools-part-1-of-2/">C program with autotools</a> - </p> + <h2 id="hello">Hello World</h2> - <pre> - $ touch NEWS README AUTHORS ChangeLog - $ mkdir -p src/bin src/lib - </pre> + <p>Create file hello.c with;</p> - <p> - <a href="http://web.eecs.umich.edu/~sugih/pointers/gdbQS.html">GDB Quick Start</a>, - <a href="https://www.hackerschool.com/blog/5-learning-c-with-gdb">Learning C with GDB</a> - and <a href="http://www.dirac.org/linux/gdb/02a-Memory_Layout_And_The_Stack.php">Memory Layout and the Stack</a> - are great sources of introductory information.</a> - </p> + <pre> + #include <stdio.h> - <p>To use gdb you need to compile program with -g flag. To - debug a program;</p> + int main() { + printf("Hello World!"); + return 0; + } + </pre> - <pre> - gdb program - </pre> + <p>Compile;</p> - <p>If the program needs arguments you can set it;</p> + <pre> + $ gcc -Wall hello.c -o hello + </pre> - <pre> - (gdb)set args -parameter1 -parameter2 - </pre> + <p>Run;</p> - <p>To start the program you can type run, this way gdb - will try to run the program until the end. If program - crash, gdb will stop it for debuging.</p> + <pre> + $./hello + Hello World! + </pre> - <pre> - (gdb) run - </pre> + <h2 ="sources">Multiple Sources</h2> - <pre> - n - execute next line - s - step in next line - b - backtrace - info locals - print - x - </pre> + <p>To organize code in multiple files split above + example in main.c, hello.c and hello.h. Content of + main.c;<p> - <h2>SysCalls</h2> + <pre> + #include "hello.h" - <pre>catch syscall open</pre> + int main() { + hello("world"); + return 0; + } + </pre> - <h2>Threads</h2> + <p>Header file contains declaration of the function hello, + content of hello.h;</p> - <p>When new thread is created you receive - a notification. To get information about - threads;</p> + <pre> + void hello(const char* name); + </pre> - <pre> - info threads - </pre> + <p>Implementation of hello function in hello.c;</p> - <p>To select thread;</p> + <pre> + #include <stdio.h> + #include "hello.h" - <pre> - thread 1 - </pre> + void hello(const char* name) { + printf("Hello, %s!\n", name); + } + </pre> + + <p>Compile;</p> + + <pre> + $ gcc -Wall main.c hello.c -o hello + </pre> + + <h2 id="makefile">Makefile</h2> + + <p>Make reads a Makefile by default on current directory, + Makefile defines targets, for example executables and their + dependencies, for example object files and source files.<p> + + <p>Create Makefile;</p> - <p><a href="http://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_39.html#SEC40">Stopping and Starting</a> - multi-thread programs</p> + <pre> + CC=gcc + CFLAGS=-Wall + + hello: main.o hello.o + + clean: + rm -f hello main.o hello.o + </pre> + + <pre> + $ touch NEWS README AUTHORS ChangeLog + </pre> + + <h2 id="debug">Debug</h2> + + <p>To use gdb you need to compile program with -g flag. Change + Makefile</p> + + <pre> + CC=gcc + CFLAGS=-Wall -g + + hello: main.o hello.o + + clean: + rm -f hello main.o hello.o + </pre> + + <pre> + $ gdb hello + </pre> + + <p>Set break point;</p> + + <pre> + (gdb) break main + </pre> + + <p>To start the program you can type run, this way gdb + will try to run the program until the end. If program + crash, gdb will stop it for debuging. Start program;</p> + + <pre> + (gdb) run + </pre> + + <p>Step in next line;</p> + + <pre> + (gdb) s + </pre> + + <p>Print variable "name" value;</p> <pre> - break linespec thread threadno + (gdb) print name + $1 = 0x4005b0 "world" + (gdb) </pre> - <h2>Strace</h2> + <p>Print variable "name" type;</p> <pre> - strace -c ./program + (gdb) ptype name + type = const char * + (gdb) </pre> - <a href="../index.html">Development Index</a> - <p> - This is part of the c9-doc Manual. - Copyright (C) 2016 - c9 team. - See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a> - for copying conditions.</p> + <p>Variable is a <a href="elements.html#const">string constant</a>. + Execute next line to end;</p> + <pre> + (gdb) n + </pre> + <a href="../index.html">Development Index</a> + <p> + This is part of the c9-doc Manual. + Copyright (C) 2016 + c9 team. + See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a> + for copying conditions.</p> </body> - </html> - diff --git a/dev/c/lib.html b/dev/c/lib.html new file mode 100644 index 0000000..fb4682b --- /dev/null +++ b/dev/c/lib.html @@ -0,0 +1,255 @@ +<!DOCTYPE html> +<html dir="ltr" lang="en"> + <head> + <meta charset='utf-8'> + <title>Libraries</title> + </head> + <body> + <a href="../index.html">Development Index</a> + + + <h1>Libraries</h1> + + <h2 id="basic">Basic</h2> + + <dl> + <dt>include <unistd.h></dt> + <dd>fork, pipe and I/O primitives (read, write, close, etc.) + + primitve types like uid_t, pid_t etc</dd> + + <dt>#include <stdlib.h></dt> + <dd>Standard lib, contains primitves for number conversion + and memory allocation</dd> + <dt>#include <stdio.h></dt> + <dd>Basic i/o lib: printf etc</dd> + <dd>glibc</dd> + + <dt>#include <string.h></dt> + <dd>String manipulations</dd> + <dd>glibc</dd> + + <dt>#include <time.h></dt> + <dd>Time related functions</dd> + <dd>glibc</dd> + + <dt>#include <signal.h></dt> + <dd>Signal handling</dd> + <dd>glibc</dd> + + <dt>#include <stdbool.h></dt> + <dd>Boolean type</dd> + <dd>glibc</dd> + + <dt>#include <math.h></dt> + <dd>Math functions</dd> + <dd>glibc</dd> + </dl> + + <h2 id="advanced">Advanced</h2> + + <dl> + <dt>#include <sys/socket.h></dt> + <dd>Socket connections</dd> + <dd>glibc</dd> + + <dt>#include <sys/types.h></dt> + <dd>Primitive types like uid_t, pid_t etc</dd> + <dd>glibc</dd> + + <dt>#include <netinet/in.h></dt> + <dd> internet address family</dd> + <dd>glibc</dd> + + <dt>#include <arpa/inet.h></dt> + <dd> definitions for internet operations</dd> + <dd>glibc</dd> + + <dt>#include <pthread.h></dt> + <dd> threads</dd> + <dd>glibc</dd> + + <dt>#include <stdatomic.h></dt> + <dd> mutual exclusion locks</dd> + <dd>gcc</dd> + </dl> + + <h2 id="random-numbers">Random Numbers</h2> + <pre> + // seed with current time: + // time_t t; + // srand(time(&t)); + void srand(unsigned seed); + + int rand(void); + </pre> + + <h2 id="signals">Signals</h2> + + <pre> + sig_t signal(int sig, sig_t func); + + int raise(int sig); + + int kill(pid_t pid, int sig); + </pre> + + <h2 id="sorting">Sorting</h2> + + <pre> + void qsort(void* values, size_t num_items, size_t item_size, int (*comparefunc)(const void*, const void*)); + </pre> + + <h2 id="strings">Strings</h2> + + <pre> + // SUCCESS: pointer to destination string + char *strcpy(char *dest, const char *src); + + // result == 0 -> strings are equal + // result < 0 -> str1 less than str2 + // result > 0 -> str2 less than str1 + int strcmp(const char *str1, const char *str2);; + + // result == NULL -> no tokens left to retrieve + // else: pointer to last token found + char *strtok(char *str, const char *delim); + + // NULL if no match + // else: pointer to first occurence in string + char *strstr(const char *string, const char *substring); + + // ERROR: result == 0 + int atoi(const char *str); + </pre> + + <h2 id="inter-process-communication">Inter Process Communication</h2> + <pre> + // ERROR: result < 0 + // result = 0 inside child + // result > 0 inside parent + pid_t fork(void); + + // ERROR: result < 0 + // SUCCESS: result == 0 + int pipe(int fd[2]); + </pre> + + <h2 id="file-io">File IO</h2> + + <h3>File Modes</h3> + + <pre> + 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. + </pre> + + <pre> + // ERROR: result == NULL (also when EOF is reached) + char *fgets(char *str, int strlen, FILE *stream); + + // ERROR: result == NULL + FILE *fopen(const char *filename, const char *mode); + + // result == EOF when finished reading the stream + // SUCCESS: number of matched items on success + int fscanf(FILE *stream, const char *format, ...); + + // ERROR: result == EOF + // SUCCESS: result == 0 + int fclose(FILE *stream); + + // ERROR: result == -1 + // SUCCESS: number of bytes written + ssize_t write(int fildes, const void *buf, size_t nbyte); + + // ERROR: result == -1 + // EOF when finished reading + // SUCCESS: number of bytes read + ssize_t read(int fildes, void *buf, size_t nbyte); + + // SUCCESS: result > 0 + // ERROR: result == EOF + int fputs(const char *restrict s, FILE *restrict stream); + </pre> + + <h2 id="shared-memory">Shared Memory</h2> + + <pre> + // ERROR: result < 0 + // SUCCESS: result == shmid + int shmget(key_t key, size_t size, int shmflg); + + // ERROR: result == NULL + void *shmat(int shmid, const void *shmaddr, int shmflg); + + // ERROR: result < 0 + // SUCCESS: result == 0 + int shmdt(const void *shmaddr); + </pre> + + <h2 id="networking">Networking</h2> + + <pre> + $ pkginfo -o socket.h + </pre> + + <pre> + #include "<sys/socket.h>" + </pre> + + <pre> + // ERROR: result == -1 + // example: sock = socket(AF_INET, SOCK_STREAM, 0); + int socket(int domain, int type, int protocol); + + // sockaddr_in struct (man ip 4) + struct sockaddr_in server; + 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)) + int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen); + + // ERROR: result < 0 + // SUCCESS: filedeskriptor for accepted socket + // example: fd = accept(sock, (struct sockaddr *) &client, &client_len); + int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); + + // SUCCESS: result == 0 + // ERROR: result == -1 + int listen(int socket, int backlog); + </pre> + + <h2 id="threads">Threads</h2> + + <pre> + // ERROR: result > 0 + // SUCCESS: res == 0 + // example: pthread_create(&threads[t], NULL, printHello, (void*)t); + int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void *), void *restrict arg); + + // SUCCESS: result == 0 + // ERROR: result > 0 + int pthread_join(pthread_t tid, void **ret); + + void pthread_exit(void *value_ptr); + </pre> + + <a href="../index.html">Development Index</a> + + <p> + This is part of the c9-doc Manual. + Copyright (C) 2018 + c9 team. + See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a> + for copying conditions.</p> + </body> +</html> |