Last active
June 2, 2017 10:38
-
-
Save knotech/39d446016d859b61a0dd1d9377fa51c7 to your computer and use it in GitHub Desktop.
Lesson 3 For 65534.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| // Sup, sorry about missing this weeks lesson. As I explained in the email, I came up with a | |
| // hella good idea, and had to nuke all my hard drives. Rearrange data. Basically, If I get inspired | |
| // you can knock on my door and I might not notice. | |
| // So for this week. | |
| /* <- This symbol starts a comment. | |
| That means that if you compile, and run your program with: | |
| ~$ gcc your_program_name.c | |
| ~$ ls | |
| a.out your_program_name.c | |
| ~$ ./a.out | |
| This block won't be included in the compiled output of the program. | |
| Basically these blocks are where we are going to make notes about | |
| the code we write, and jokes to keep any other programmer's that | |
| have to work on your code later sane while they try to decipher what your code does. | |
| */ // This symbol: "*/" ends a comment... | |
| // <- oh yeah and this is a comment too. | |
| // These are single line comments. | |
| /* These are multi-line comments */ | |
| #include <stdlib.h> // But /* multi-line comments */ can be used for single line comments too. | |
| // Anything between them is included in the compilation. | |
| // C does not care about where you place statements. You could literally write a C program in one line with no spaces. | |
| /* ... and anyone who had to work on it later would curse your name. */ | |
| #include <unistd.h> // This is where we get | |
| // Kind of like this program... Which you are going to work on. | |
| // our programs last week were: | |
| #include <math.h> // This gives us fabs, which returns the absolute value of a number. | |
| /* | |
| #include <stdio.h> | |
| main() { | |
| printf("Some string.\n"); | |
| return(0); | |
| } | |
| */ | |
| // And the other one | |
| /* | |
| #include <stdio.h> | |
| void main() { | |
| char user_input; | |
| char buffer[64]; | |
| printf("Prompt user for text: \n"); | |
| scanf(%s, &user_input); | |
| printf(%s Some other text, &user_input); | |
| return; | |
| } | |
| */ | |
| int main() { | |
| char visit_site[1]; | |
| printf("The key difference I was going to point out was that the first program does not state a type that main function is going to return.\n"); | |
| printf("The main data types we'll find ourselves using are:\n"); | |
| printf("void\n"); // Nothing, null, nada, zilch... This one is special. | |
| printf("char\n"); // A single character, like the first word in the sentence of this comment. | |
| printf("int\n"); // A whole number, we'll do an exercise where we determine if we this accepts integers. | |
| printf("float\n"); // A 'floating point' number, like decimals, like money, or 2.718281828459045 | |
| printf("double\n"); // A double percision float. Like 3.1415926535897932718281828459045 | |
| printf("For a full list of C data types visit https://en.wikipedia.org/wiki/C_data_types \n\n\n\n"); | |
| printf("Do you want to see the list right now? [y/n]"); | |
| scanf("%1s", visit_site); | |
| /* | |
| // Remember when we got that error? ***Stack Smashing Detected*** | |
| // Take a look at the difference between this and the scanf function from our 2nd program. | |
| // And try to feed more input than we alotted for the "visit_site" variable. | |
| // This is the easiest (or at least the most straight forward) way to limit the amount of | |
| // input that a function reads into a buffer. | |
| // a safer, more flexible, or just 'fancier' way is to use the function "fgets". | |
| // You can read the manual page about it by typing `~$ man fgets` | |
| // I'll show you why allowing user input that can write more data into a buffer | |
| // than you have allocated is dangerous, important to keep in mind, and super interesting. | |
| // we'll also have to start learning about memory address space to understand it. | |
| // It's gonna be like, ... a lot... like, really a lot... of information. | |
| // and we'll start looking assembly. I realized after | |
| // I left that all that shit in the assembly was the program loading the library from the | |
| // #include <stdio.h> statement. | |
| // We'll write a couple short programs and look at the assembly. | |
| // Read up on "Makefiles" if you get 10 minutes, because we are going to write one. | |
| // Oh and all of these '//' we're totally unecessary. | |
| See... | |
| */ | |
| if (visit_site == 'y' || visit_site = 'Y') { | |
| system("firefox https://en.wikipedia.org/wiki/C_data_types"); | |
| } elif (visit_site == 'n' || visit_site == 'N') { | |
| printf("Okay, well, you can actually find all of the documentation here anyways.\n"); | |
| system("echo -e " \033[33;5mm~$ man ctype \033[0m"); | |
| for (int i=-5; i<0; i++) { | |
| printf(%d, fabs(i)); | |
| system("sleep 1"); | |
| } | |
| if (fork == 0) { | |
| // We'll talk about forks and what are called child processes, but we have a lot of other shit to cover first. | |
| // But seriously 2 hours a week isn't enough to learn. If you want to get good, fast, 2 hours a day is kind of the | |
| // baseline. | |
| execl("man ctype"); | |
| return -1; | |
| } | |
| return(0); | |
| } else { | |
| printf("Run this again and actually just hit y or n or even use a word that starts with a y or an n.\n"); | |
| printf("And I haven't tested this. I just wrote it. \nSo if you did hit y or n... \nand it didn't work... \n\n\nFigure out why.\n"); | |
| return 1; | |
| } | |
| } | |
| /* | |
| Since you're doing this to learn C++ Here's the first program we wrote in C++ | |
| #include <iostream> | |
| int main() | |
| { | |
| std::cout << "¿De quién es este mundo?\n"; | |
| std::cout << "El mundo es tuyo.\n"; | |
| return 0; | |
| } | |
| copy this into a file and compile it with g++ instead of gcc. | |
| Just so you can see how the work we are doing here will translate. | |
| In 2 more weeks, if we can cover the rest of the basics, we'll start building a game. | |
| Don't get too excited tough, it will be 2-D, and be closer to Pac-Man than Call of Duty. | |
| But you get to learn about how program flow works for games, and we'll start to look at | |
| what's called 'managing state'. And using an IDE, instead of a text editor. | |
| Hope your sister is doing well man. | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment