If you’re someone who plans to go through this site to gain knowledge about everything programming, there are a few basic components that play a big part in a computer’s ability to execute programs. Including common terms and some fancy programming lingo.
As for the programming examples, please think of them as “Psuedo-Code” instead of just code in the language of C. The importance of this section is NOT to learn how to program in C, it’s to learn how to conceptualize programming.
Many programming languages and software development scenarios use the following concepts, and it’s important to understand them. Throughout a programmers’ career/life, it is likely that they won’t stick to the same language, but will probably use the same concepts over and over. Even as I write this, I haven’t touched C in probably 5+ years, but the same programming concepts I learned still apply to my more up-to-date languages. It’s easier to learn a concept first, then apply it to a language, instead of the other way around.
Here is a summary of this page:
- What is a Central Processing Unit (CPU)?
- What is Main Memory?
- What is External Memory?
- What is a Operating System?
- What is a Compiler?
- What are Application Programs?
- What is a Process?
- What is Thread?
- What is Systems Software?
- What is a Integrated Development Environment (IDE)?
- Putting it All Together / Let’s Start Programming!
- What are Comments in Programming?
- What are Directives / Imports in Programming?
- What is a Main / Executive function in Programming?
- What is Printing / Output?
- Short Note about Main and Sequential Execution
- What are Variables in Programming?
- How to Use a Variable in Programming?
- Example Program Using Variables
What is a Central Processing Unit (CPU)?
A Central Processing unit is the “brains” of the computer that actually execute all the instructions the computer runs. This is the lowest level of processing that can happen in a program, think of 1’s and 0’s when it comes to binary; the CPU interprets those numbers.
What is Main Memory?
Main Memory is where all running programs and the data they use are stored. This is only active when the computer is physically on. This includes Random Access Memory (RAM)
What is External Memory?
External Memory is a more permanent memory, such as a hard-drive, SD card, sim card, CD, etc. In order for any programs stored on these medium to run, they have to be transferred into main memory.
What is a Operating System?
An Operating System provides an interface between the machine and the user. Allocates the computer’s resources. Some operating systems include Windows, Mac OS, and Linux, with sub-systems of each, such as Windows 10, Mac OSX, CentOS, and Ubuntu.
What is a Compiler?
A Compiler is basically “translator” which converts a program written in a high-level computer language (C, Java, Python, etc.) into machine language, a set of 0s and 1s understood by the computer.
What are Application Programs?
Application Programs are programs intended to be used by end-users on a computer.
What is a Process?
A Process is a program in execution.
What is a Thread?
A Thread is a unit of execution in a process.
What is Systems Software?
Consists of a set of programs that support the operation of a computer system and help, the programmer, to simplify the programming process and create an environment to run application software efficiently.
Note: Eventually I will be creating another entire section on this topic
What is a Integrated Development Environment (IDE)
This is the tool that you will be using to program. It usually contains a source/code editor, building tools, and a debugger. An example of this can be as simple as Notepad++ all the way to Visual Studios. Read more on the IDE Wikipedia Page
Putting it all together
When you boot up your computer, the first major thing that occurs, is that the operating system gets loaded into main memory.
This program (Windows, Mac OS, Linux, etc.) is responsible for providing the user with a nice interface, and responding to the requests of the user.
For example, if the user double clicks on the Chrome icon, this invokes the computer to find the Chrome program stored on the hard drive and load it into main memory. Any data this program needs to execute must also be loaded into main memory. All executable programs are stored in machine language, which is understood by the machine.
Let’s Start Programming!…kinda
Example Code of a C Program:
/* Coding Timplates
The simplest program you will ever write
3/19/2020
A small program to give the illusion that programming is easy
Language Used = C
*/
#include <stdio.h>
int main(void) {
printf("Help I started programming and I am lost already!\n");
return 0;
}
The above is a basic example of code. This code, when compiled, makes up a program. Yes, this create’s an executable when compiled, such as a *.exe file. So all your programs on your computer, Microsoft Word, Notepad, Calculator, Paint, even your games like Solitaire, contain lines and lines of code, in various languages, wow. Don’t let all this power go to your head.
From our example above, each line/piece of this code means something different, so we’re going to go through each one. Keep in mind that these elements can be made up of different things going between different languages, but let’s try to keep this simple.
What are Comments in Programming?
The section between /* and */ is called a comment, and more specifically a block comment. These are normally in an entirely different color when it comes to programming (usually grey or green) on your IDE. If your IDE doesn’t show different colors for comments compared to other code then I would recommend using a different one because it’s a long way down from here.
The purpose of a comment is to help the reader identify key information about the program. The first comment in a program (the header comment) should identify the author of the program, the date the program was written, as well as a brief description of the program. It’s important to note that comments are something the compiler normally ignores. We will talk about other types of comments later.
What are Directives / Imports in Programming?
#include <stdio.h>
This line is a directive to include a standard C header file. There are some pre-written functions in C that we commonly use. When we include this file, we are allowed to use all of these functions. More specifically, these functions allow us to print information to the screen and read in information from the user. Nearly all of programs written in C will this line. Many languages use directives for importing other features to use in your code. For example, in the Java programming language instead of #include, you would use import. See more examples in the Java section.
What is a Main / Executive Function in Programming?
int main(void) {
This line signifies the beginning of the function main. All code in C resides in functions. You can think of functions as all being separate little programs. main is a special function and it’s the only function that automatically EXECUTES. A program may be made upon many functions, but the computer only DIRECTLY executes main. In order for the computer to execute any other function, main must give instructions to call those functions. We will go over more details on functions later on.
What is Printing / Output?
printf("Help I started programming and I am lost already!\n");
This line calls a function printf which resides in stdio.h. The way this function works is that it prints out any string contained in between two sets of double quotes. However, it does not print all the characters above as seen to the screen. There are a few characters which have special codes, called escape sequences. These characters all start with a backslash: \. The code \n stands for a newline character. That simply tells the computer to advance the cursor to print information to the screen to the left hand side of the new line. This line will simply print:
Help I started programming and I am lost already!
to the screen and advance the cursor to the next line so that the next piece of information printed will start printing directly below the H instead of right after the !.
return 0;
Each non-void function must return a value of the proper type. For most functions except for main, this return value is meaningful. But for the function main, this value is of little significance and is only included so that the program has proper syntax.
}
This bracket simply signifies the end of the function main.
A few things to take away from functions (that are not main).
Let’s use this function as an example:
public int square(int value) {
return value * value;
}
- A function is called a method synonymously
- A function is made up of a few things, let’s reference each using the example above:
- public : member-access, which means basically “who has access to use this function?”
- int : return type, which is the kind of data you’ll be returning
- square : the name of the function
- int (again) : the parameter type, which is the kind of data you’ll be passing to the function
- value : a parameter to the function. When calling the function you would input a value (such as 3, which is an int)
- return : This is what you’re returning from the function
- brackets { } : This encloses what you plan on doing inside of the function, the “body” of the function
- A function ‘call‘ is when you use that function
- For example, to use the function above you would do:
- square(3);
- This would return 9
We will go over functions more later on, with better examples.
Short Note about Main and Sequential Execution
As the beginning of these code guides go, all the programs will only comprise of one function: main. When your compiled program executes, it will simply execute each instruction in main in order. One of the key concepts that you should take away from programming is understanding sequential execution. Namely, each program specifies exact instructions for the computer to execute in a particular order without ambiguity. When writing programs, you must take this into account. You must be precise about the statements you ask the computer to execute and exactly which order you want them to execute. It’s much like reading a book, you wouldn’t start reading in the middle and then jump around, right?
What are Variables in Programming?
Computers are powerful because they can manipulate data. However, the computer must have a standardized manner of storing information. Primitive data types native to the C language aid this standardization. In particular, in order to store data in a C computer program, you must store it one of the primitive data types given by C. Of these data types, the most common ones you will use are:
int, char, float, double
A type specifies what will be stored. I can also assure you that “data type specifications” are common between many languages, including C++, C#, Java, Javascript, and pretty much anything except Python, where the rules are made up and the points don’t matter.
For example, an int stores an integer in between -231 and 231-1. Typically, most applications don’t compute integers outside of this range, so you can mostly think of an int as storing an integer.
A char stores a single character. A character includes any key on the keyboard, along with a few “special” characters that are not necessarily printable. Incidentally, all characters are stored inside the computer as integers in between 0 and 255. The numeric value of a character is known as its ascii value. We’ll talk more about these later. Strings, which we will often use, technically are not primitive data types, but are rather several char variables strung together.
float and double store decimal numbers to varying precision. floats store decimal numbers to about 6 or 7 digits of precision, while doubles store decimal numbers to about 13 digits of precision.
How to Use a Variable in Programming?
In order to use a variable in a program, you must first declare the variable. Once you declare it, typically, you should initialize the value of the variable. These two things can be done in one step. Here is an example of a variable declaration only:
int kilometers_in_mile;
After this line is executed, the computer allocates space for an integer variable. The name given to that space is kilometers_in_mile. Pictorially, we have something as follows:
kilometers_in_mile
Based on this line of code, the variable (the box) may store any value. We can initialize the value as follows:
kilometers_in_mile = 0;
This tells the computer to take the value on the right of the =, and store it in the variable to the left of the =.
Note that all statements in C end in a semicolon. (Occasionally, you will see situations that seem to contradict this rule, but in fact, in those situations, the structure in question turns out not to be a single statement.)
The picture after this statement is as follows:
kilometers_in_mile
| 0 |
Once a variable has a value, it can be used in statements for computational purposes. First we will show another sample program that uses variables, then we will discuss common practices with variables, assignment statements and arithmetic expressions. (These are the separate components of the following program.)
Example Program Using Variables
/* Coding Timplates
Another Simple Program
3/19/2020
Computes the number of feet in a mile */
#include <stdio.h>
int main(void) {
int feet_in_mile, yards_in_mile;
int feet_in_yard;
yards_in_mile = 1760;
feet_in_yard = 3;
feet_in_mile = yards_in_mile*feet_in_yard;
printf("Mile = %d Feet.\n", feet_in_mile);
return 0;
}
The first two lines (9 and 10) of the main function declare all the variables. All variables inside of a function must be declared at the beginning of the function. Thus, after these two lines, our pictures looks like:
feet_in_mile yards_in_mile feet_in_yard
Note, these boxes may NOT be contiguous in the computer’s memory. I have only drawn them this way because it’s easier for me to do so.
The next two lines initialize the variables yards_in_mile and feet_in_yard. Here is the picture after these two lines are executed:
feet_in_mile yards_in_mile feet_in_yard
| 1760 | 3 |
The next line is an assignment statement. (Technically speaking, so are the two previous lines we just went over.) The syntax of an assignment statement is that a variable is always on the left hand side of an equal sign, and an arithmetic expression is always on the right hand side, followed by a semicolon. The order of execution is as follows:
1) The value of the arithmetic expression on the right is determined using the current values of the variables in this expression. Note: There may also be constants in this expression.
2) This value is then stored in the variable on the left hand side.
Using this procedure, we evaluate the expression:
yards_in_mile*feet_in_yard
This evaluates to 1760*3, using the current values of each of the respective variables. Simplifying this numerical expression we get 5280. Finally, this value is stored in feet_in_mile.
The picture after this step is as follows:
feet_in_mile yards_in_mile feet_in_yard
| 5280 | 1760 | 3 |
Finally, the last line prints out some information to the screen. Up until now, we only talked about how to print characters to the screen. But now, we would like to print the value of a variable to the screen. In order to do this, we must specify the format of the variable. %d is the format for an integer variables. A list of format codes for C can be found on this site. (Note: The code for a float is %f, and a double is %lf.) This code tells the computer what type of variable will be printed. After the ending double quote, the actual variable itself must be specified. This is done by first typing a comma, followed by the name of the variable. The printf function call must then be closed with a close parethesis.
Note: the syntax for printing varies between languages, this is just an example.
The end result of running this line the statement:
Mile = 5280 Feet.
will print to the screen.
Congratulations!
You made it through my first programming guide section. This section references many things that are taught from UCF, but have been formatted and skimmed down so they aren’t as useless as when they were taught to me.