Erin's CS stuff

CS stuff!

Skip the explanation and go straight to the Quick Guide.

In the beninging

Generally, the code we write in this class will run sequentially, so we should have a well-defined, consistent starting point. Enter the main function! (We'll learn about "functions" later, I promise!)

Every program in C must include a main function. While you may see some variations of the format in the wild, our programs will always start with

begin

and end with

end

All our other code (at least to start with) will go between those two parts.

As far as "behavior" (what the computer does when we run our program), this code doesn't really do anything, but it is required and can be considered the "start" of our programs. So many questions about how to code are answered with "it depends", but this is something you can just memorize!

preferred

If this is missing (or certain parts of it), you will see an error when trying to compile [1]. For example, compiling a program with:

int main{

   return 0;
}

will result in:

error

because the parentheses are missing before the curly brace on line 1. Compiler errors are pretty cryptic, but with some practice they'll start to make some sense.

error_notated

Comments

Although code is also for communicating with humans, some things are easier in our every day language than in a programming language. Luckily, we can use comments for that. Any comments in our program files get ignored by the compiler, and therefore the computer. It's a great way to leave future-you a little note about what the heck you were thinking!

There are two types of comments in C:

Single-line comments start with 2 forward slashes // and they end automatically at the end of the line.

Multi-line comments start with a forward slash then asterisk /* but only end after the reverse */ (an asterisk then forward slash).

In most cases, especially if you have a short note, single-line comments will do. In this class, you will be expected to include what we call "header comments" at the top of each program file. It's a good habit to get into and it will help the graders do their work! You can use whatever kind of comments you like, but an example of my preferred style is below.

The first things to put in every program file should be the header comments and main function:

with_comments

1 Technically, you only need

int main(){

}

but it's a best practice to include the return statement and grading will reflect that. Again, there are a couple of valid variations of this, but in this class you'll see the preferred format.