Definitions | |
---|---|
C | Programming language |
Command Line | Type cammands; they are executed immediately Examples: cd, nano, gcc, ... |
Text Editor | Word processor that can save as text only Examples: nano, notepad, vim, ... |
Instruction | Single statement that does something Examples: printf("Hi\n"); x = 5; |
Expression | Sequence of operations that results in a value Examples: 5 x*20 + 10/3 x |
Variable | Stores a value Examples: int x; float a; char hello_123; |
Condition | expression that evaluates to either true or false Examples: true false x < 5 x == y 10 != 3 |
Function | Block of instructions that can be called from somewhere else |
Arguments | Things passed to a function (inputs) |
Return value | What the function gives back (output) |
Array | variable that can hold multiple values |
Index | which element in the array you're talking about |
String | an array of characters (text) |
Preprocessor | gcc runs it before the compile, looking for # preprocessor directives |
Literal | anything that shows its own value. Examples: 5 'a' "hi there" |
Symbols |
# | Preprocessor directive Example #include |
+ | add |
- | subtract or negative Examples: a-b "a minus b" -c "negative c" |
/ | Divide |
* | Multiply or pointer Examples: a*b "a times b" *c "pointer c" |
% | modulo a.k.a. remainder |
= | set Example: x=5 "set x to 5" |
== | comparison Example: a==b "is a equal to b?" |
!= | not equal Example: a!=b "is a different than b?" |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
&& | and Example: a==b && c==d "a is equal to b and c is equal to d" |
|| | or |
! | not Example: !a "not a" |
& | bitwise and Don't use this until you know what it means. |
| | bitwise or Don't use this until you know what it means. |
; | semicolon at the end of each instruction Exceptions if, for, while, function definitions, preprocessor directives |
\n | newline |
%d | replace this with a integer Example: x = 5; y = 4; printf("hi %d %d there\n", x, y); hi 5 4 there |
%c | replace this with a character Example: c='a'; printf("hi %c there\n", c); hi a there |
%f | replace this with a float |
%s | replace this with a string |
Paired symbols | |
{} | curly brackets Used for: block of instructions Examples: if, for, while, function definitions |
() | parentheses Used in: expressions (remember your order of operations!) for, while, if (around the condition) around argument lists (both calls and definitions) |
<> | angle brackets Used in: #include |
[] | square brackets Used in: arrays Example: a[5] "element number 5 of the array a" |
"" | double quotes Used for: text a.k.a. string Example: "Hello world" |
'' | single quotes Used for: character Example: 'H' |
Types |
int my_var | integer, a.k.a. whole number |
char my_var | character |
int my_var[] | array of integers |
char *my_var | string |
char my_var[] | string |
char *my_var[] | array of strings |
float my_var | floating point, a.k.a. real number |
void | nothing (no type) | Functions |
srand() | sets the random seed (starting point for random number generation) Example: srand(time(NULL)); |
rand() | gets a random value Example: x = rand() % 100;  "Get a random number between 0 and 99, and put it in x." |
printf() | puts text on the screen This funciton is special because it can accept a variable number of arguments. Examples: printf("Hi!\n"); Hi! printf("Hi %d!\n", 2); Hi 2! printf("Hi %d %c %s %f!\n", 2, 'a', "Wheee", 4.562); Hi 2 a Wheee 4.562! |
time() | Gets the current time in seconds Example: printf("How many seconds? %d seconds!\n", time(NULL)); |
fgets() | Gets input from the user Note that they have to press enter before the function returns. fgets() takes 3 arguments: 1. the string to store the input into 2. the length of the string 3. the source of input (in most cases you'll use stdin (standard input)) Example: char user_input[128]; fgets(user_input, 128, stdin); |
atoi() | Turns a string into an integer Examples: int x = atoi("100"); int value = 0; if (argc > 1) { value = atoi(argv[1]); } char user_input[128]; int value; fgets(user_input, 128, stdin); value = atoi(user_input); |
strcasecmp() | String case compare. It compares two strings, ignoring case. It takes two arguments, the two strings to compare. It returns a number: 0 if they are the same, and non-zero if they are not. Examples: if (strcasecmp(user_input, "hello\n") == 0) { printf("Hello to you to!\n"); } if (strcasecmp("apple", "aPPlE") == 0) { printf("Yeah\n"); } if (strcasecmp("apple", "pear") == 0) { printf("No!\n"); } |
exit() | You can call this to exit your program at any time. It takes one integer argument. Pass it 0 if there was no error. Pass anything else if there was. |
main() | This is where you put your own code! The program always starts at the first instruction inside main's {} curly brackets. |