Summary of my journey so far in learning a programming language

Computer Language: “So, you see over here we have a horse and wagon.”

Me: (*peers over to where the computer language is pointing too*)

Me: “What are you talking about? I don’t see a horse and wagon.

Computer Language: “It is right there.”

Me: “There is not a horse and wagon over there.”

Computer Language: “You are not looking hard enough, it is seriously right over there.”

Computer Language: (*gestures over towards the same direction it has been gesturing to but now with more frustration, hostility and force*)

Me: “Um, all I see over there is a horse that seems to be hitched up to what appears to be….a car?”

Computer Language: “Yes, as I said, a horse and wagon. Use your imagination.”

Me: “That is not a horse and wagon. That is a horse that has been hitched up to a car.”

Computer Language: “Well a horse hooked up to a car is the exact same thing as a horse and wagon.”

Me: “What on earth are you talking about?”

Computer Language: “Well, the horse is hooked up to the car the same way as it would be to a wagon and you can sit on roof of the car the same way as you could on a wagon so therefore that makes it pretty much the exact same thing….duh!”

Computer Language: *rolls its eyes at me and smiles at me smugly.*

Me: “Let me get this straight, you are telling me that you hooked a horse up to a car and you are now reasoning that because you are able to hitch it to the car and that you can sit on top of the car that now logically it has all the features and functions of a wagon.

Computer Language: “Yes, of course! Why wouldn’t it now operate the exact same way?”

Me: “You’re truly insane.”

C Programming: Why do we need to ‘return 0’ in our main() function?

When the operating system runs a program in C, it passes control of the computer over to that program. This is like the captain of a huge ocean liner handing you the wheel. Aside from any fears that may induce, the key point is that the operating system needs to know where inside your program the control needs to be passed. In the case of a C language program, it’s the main() function that the operating system is looking for. The set of parentheses after a C language function name is used to contain any arguments for the function — stuff for the function to digest.

Why do we use return 0;?

In C and C++ programs the main function is of type int and therefore it should return an integer value. The return value of the main function is considered the “Exit Status” of the application. When we return 0 at the end ( while using int as the return type) it tells the OS that the program is functional and is without any error(except logical error), as main function is called by OS and it returns 0 to the OS. On most operating systems returning 0 is a success status like saying “The program worked fine”.

Main returns are useful for debugging, program analysis and lots of stuff when you get serious with your programming.

On C++ compilers that use C99 and above, if you skip return statement in your main function, compiler would add a return 0 or return EXIT_SUCCESS for you. It varies from compiler to compiler but it’s important to keep in mind that main is the only function where omitting return is allowed unless that functions return type is void.

In summary, it’s good practice to use it since it reports any errors back to the environment in which it was run. Wherever the return statement is encountered, it marks that the function stops there.

What is the difference between int main() and int main(void)?

In C++, there is no difference, both are same.

Both definitions work in C also, but the second definition with void is considered technically better as it clearly specifies that main can only be called without any parameter.

In C, if a function signature doesn’t specify any argument, it means that the function can be called with any number of parameters or without any parameters.

Why do we always have to use int with our main() function?

The return value of main is to be passed to the operating system (any operating system) in a single, consistent way. The information that the operating system needs to know is “did the program terminate successfully, or was there an error?” Returning an integer is the easiest way to do it on the OS level.

The key point to understand is that main – unlike other functions in any program – is not part of a protocol defined by the programmer, but the protocol used to interface with the host (OS).

What about void main()?

The keyword just before the function name is the return type of the function. If a function has void as its return type then you can’t return a value, so you can’t have return 0; inside that function.

void main() should not be used as it is not a good practice. C void main is permitted only if the compiler specifically supports it. Many compilers don’t complain very loudly if you write void main.

And what about argc & argv?

The main() function uses its parentheses to contain any information typed after the program name at the command prompt. You might think that the command line is a relic of the past but even today when the world runs on graphical operating systems internally all graphical operating systems still reference the command line and a c program can read these command line options and arguments.

Command line arguments are arguments that you can give to certain programs before they run. They are placed after the command to run an executable.

So how do we tell our program that it should be expecting command line arguments? We just have to include a few arguments when main() is declared.

When you don’t plan on your program accepting any command line arguments, you can leave the main() function’s parentheses empty.

Using them looks like this: int main()

But when arguments are used in your code, they must be declared.

Using them looks like this: int main(int argc, char *argv[])

argc is the argument count value. It’s an integer that ranges from 1 through however many items were typed after the program name at the command prompt.

*argv[] is an array of char pointers. You can think of it instead as an array of strings, which is how it can be used in your code.

The main() function receives information about the command-line argument directly from the operating system. The command line is evaluated, and arguments are tallied and referenced. The tally appears as argc, and the references are stored in the argv[] array.


Minecraft Programming: How to Use Minecraft Python API and Spigot

From the book:
Learn to Program with Minecraft by Craig Richardson

  1.  Open Spigot by clicking Start_Server.
  2. Open Minecraft and connect to the Spigot server by selecting Minecraft Python World from mulitplayer menu.
  3. Hit ESC on your keyboard to free your cursor from the Minecraft window, and then open a Python shell in IDLE. Make sure you are running Python 3!

You’ll need to have these three pieces of software open whenever your write programs that interact with Minecraft.

Then enter the following into your shell:

from mcip.minecraft import Minecraft

mc = Minecraft.create()

Minecraft Cheatsheet

How do I fly?

If you press the space bar once you will jump. Double-tap the space bar and you will fly.

How do I move?

To move forward, press the “w” key.
To move backward, press the “s” key.
To strafe, or move sideways, to the left, press the “a.”
To strafe to the right, the default key is “d.”
To jump, press the spacebar.