|
| 1 | +## Compilation |
| 2 | + |
| 3 | +After setting up your IDE (VS Code, JetBrains CLion, Vim are among many |
| 4 | +of the options available) and having your program ready, you have to compile it |
| 5 | +(i.e. turn the text of the program into a machine code which your computer will |
| 6 | +be able to run later). |
| 7 | + |
| 8 | +This can be done like this, in your terminal: |
| 9 | +``` |
| 10 | +gcc programname.c -o outputname |
| 11 | +``` |
| 12 | + |
| 13 | +If there were errors or notes during the compilation process, the compiler |
| 14 | +(here we are using the GCC compiler) will notify you. If the program compiled |
| 15 | +correctly, you can later run the generated executable: |
| 16 | +``` |
| 17 | +./outputname |
| 18 | +``` |
| 19 | + |
| 20 | +## General tutorial |
| 21 | + |
| 22 | +### Code |
| 23 | + |
| 24 | +Code for execution goes into files with “.c” suffix. Shared decl’s |
| 25 | +(included using #include “mylib.h”) in “header” files, end in “.h” |
| 26 | + |
| 27 | +### Comments |
| 28 | + |
| 29 | +Characters to the right of `//` are not interpreted; they’re a comment. |
| 30 | +Text between `/*` and `*/` (possibly across lines) is commented out as well. |
| 31 | + |
| 32 | +### Data types |
| 33 | + |
| 34 | +* `char` - an ASCII value: e.g. ‘a’ (see: man ascii) |
| 35 | +* `int` - a signed integer: e.g. 97 or hex 0x61, oct 0x141 |
| 36 | +* `float` - a floating-point (possibly fractional) value |
| 37 | +* `double` - a double length float |
| 38 | + |
| 39 | +`char`, `int`, and `double` are most frequently and easily used in small |
| 40 | +programs. |
| 41 | + |
| 42 | +sizeof(double) computes the size of a double in bytes. |
| 43 | + |
| 44 | +Zero values represent logical false, nonzero values are logical true. |
| 45 | + |
| 46 | +### Functions |
| 47 | + |
| 48 | +A function is a pointer to some code, parameterized by formal parameters, |
| 49 | +that may be executed by providing actual parameters. Functions must be |
| 50 | +declared before they are used, but code may be provided later. |
| 51 | + |
| 52 | +A `sqrt` function for positive `n` might be declared as: |
| 53 | + |
| 54 | +``` |
| 55 | +int addNumbers(int a, int b) // function definition with return type, name and parameters |
| 56 | +{ |
| 57 | + int result; |
| 58 | + result = a+b; |
| 59 | + return result; // return statement |
| 60 | +} |
| 61 | +Functions that do not return anything return `void`. |
| 62 | +There must always be a main function that returns an int: |
| 63 | +```c |
| 64 | +int main() |
| 65 | +{ |
| 66 | + return 0; |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### Statements |
| 71 | + |
| 72 | +Angle brackets identify syntactic elements and don’t appear in real |
| 73 | +statements |
| 74 | + |
| 75 | +```c |
| 76 | +<expression> ; //semicolon indicates end of a simple statement |
| 77 | +break; //quits the tightest loop or switch immediately |
| 78 | +continue; //jumps to next loop test, skipping rest of loop body |
| 79 | +return x; //quits this function, returns x as value |
| 80 | +if (<condition>) <stmt>! //stmt executed if cond true (nonzero) |
| 81 | +if (<condition>) <stmt> else <stmt> // two-way condition |
| 82 | +while (<condition>) <stmt> //repeatedly execute stmt only if condition true |
| 83 | +do <stmt> while (<condition>); //note the semicolon, executes at least once |
| 84 | +for (<init>; <condition>; <step>) { <statements> } |
| 85 | +``` |
| 86 | +
|
| 87 | +### Includes |
| 88 | +
|
| 89 | +The homework requires you to include several header files with the needed functions: |
| 90 | +
|
| 91 | +#### I/O (`#include <stdio.h>`) |
| 92 | +Default input comes from “stdin”; output goes to “stdout”; errors to “stderr”. |
| 93 | +Standard input and output routines are declared in `stdio.h`: `#include <stdio.h>` |
| 94 | +* `scanf(p,...)` - reads ... args using format p (below); |
| 95 | +* `printf(p, ...)` - write ... args using format p (below); pass args as is fprintf(f,p,...) |
| 96 | +
|
| 97 | +Format specifiers: |
| 98 | +`%c` - character |
| 99 | +`%d` - decimal integer |
| 100 | +`%s` - string |
| 101 | +`%f` - float |
| 102 | +
|
| 103 | +#### MEMORY (`#include <stdlib.h>`) |
| 104 | +* `malloc(n)` - allocates `n` bytes of memory; (for type T: `p = (T*)malloc`) |
0 commit comments