< C Programming
Completion status: Almost complete, but you can help make it more thorough.

Objective

  • Understand and differentiate between hardware and software.
  • Understand how the computer executes software.
  • Understand the process of compiling a file.
  • Gain a small taste of C.

Lesson

Hardware & Software

The Personal Computer (PC)
1. Monitor
2. Motherboard
3. CPU (Microprocessor)
4. Main memory (RAM)
5. Expansion cards
6. Power supply unit
7. Optical disc drive
8. Hard disk drive (HDD)
9. Keyboard.
10. Mouse

A computer's system unit, also commonly known as the computer case, contains a CPU (Central Processing Unit) while it usually has a monitor, keyboard, and a mouse connected to it. The CPU does all of the the processing and is an important part of the computer. Without it, the computer wouldn't even be able to run. The monitor displays graphics on a screen, which is easy for the user to see. The monitor acts as visual output. The keyboard and the mouse act as user input. The computer uses this input for many things and you'll most likely get some kind of response because of it.

The computer uses all of this equipment to do its job. This equipment, called hardware, isn't the only thing that makes a computer run. With all of the hardware something else is still needed to bring the computer to life. The stuff that actually makes the computer do something is called software. Unlike hardware, software cannot be manipulated directly by the user. Usually, the user needs to use the computer to interact with the software.

The CPU processes the software and executes it appropriately. Software is made up of a lot of instructions which the CPU executes. We don't need to go in depth about the instructions, since they vary between CPU's. The important thing is that we know that there are instructions.

It is now understood that, if we want the computer to perform a task, we must first get the instructions first. How? We simply write them down and then turn them into executable code that the CPU can use. Just remember this, a computer program is a set of instructions (a "recipe") that defines the task to be performed.

Program Execution

Machine code is the computer's true language. Some would call it unreadable by human standards. However, it is digital poetry to the trained. Back when computer science was still in its infancy, all programs were written in machine language. Before screens and printers existed, the programmer laboriously entered into the computer the machine code for the program and then fed each instruction via some external device, such as punch cards or switches. This task of writing programs was extremely slow and prone to errors.

When a program is executed, the computer reads each instruction and performs the task associated with that instruction. Since each computer can vary in the type and amount of instructions it has, we won't focus too much on this topic. The important thing to know is C's ability to be cross-platform when creating code. This portability was the key to C's success.

Compiling

Machine code is basically a set of instructions that the computer can execute. Computers use binary as the format for storing information. Each number can either be a 1 or a 0; which could be represented as on and off. Thankfully, a new method of writing programs has replaced machine code; this is called assembly. Although this is pretty consistent to writing binary, assembly still gives the program some edge, leading to less wasted time. Still, coding in assembly was slow and error prone. Higher-level programming languages started to appear. These made it easier for the programmer to write programs, efficiently and quickly. Some of these languages convert their code to assembly, which will come in handy later in the course. C, unlike a lot of new programming languages, converts its code to assembly where a new executable file is created.

A Taste of C

Here is the classic Hello World program written in C.

#include <stdio.h>

int main(void)
{
    printf("Hello, World!\n");
    return 0;
}

Most, if not all, C programs have the #include macro, the most common being #include <stdio.h>. What this macro really does is tell the compiler, "Hey! Before you compile my program, just copy and paste the contents of stdio.h where I am!". This is used to include the contents of standard C libraries.

In C, code is cut up into little blocks of code called functions. Unless declared otherwise, the function main will be called first when the program starts. Don't worry about the words int and void, they are special sizes that will be discussed later. The most important thing to worry about is the curly brackets ({}); a function starts and ends with them. The printf() function is arguably the most used function in C. It is used to display formatted text.

At the heart of our program is an instruction to write "Hello, World!" (followed by a newline character "\n") to standard out, which is usually the console. So what exactly is happening here? First, we call, or invoke, the function printf(). We put the arguments we wish to pass to printf() within the parenthesis. In the code above, we only pass a single argument, the string "Hello, World!\n". What's a string? Don't worry, that and more will be explained in the following lessons.

On the next line we have return 0. When the computer encounters return 0, it immediately exits the function, meaning nothing past it will be executed, and returns the integer 0 to the caller of the function (in this case, the OS). When a non-zero integer is returned to the OS, it is usually a sign that an error has occurred somewhere in the program.

You may have noticed the semi-colon (;) at the end of printf("Hello, World!\n") and return 0. These lines of code are called statements, and in C, statements are always terminated with a ;. If you do not terminate your statements, the computer will not know where the statement ends and the next one begins.

Try writing the source code above to a file. Save the file (remember to save it with the extension .c if you're using a plain-text editor), compile it, and run it. You've created your first C program! Congratulations on your step forward into the world of C.

Assignments

  • Name one piece of hardware that acts as user input.
  • Name one piece of hardware that acts as user output.
  • What is a computer program made up of?
  • What is the computer's true language?
  • What is the lowest-level, platform-dependent language called?
  • Is C compiled or assembled?
  • Were you able to compile the example in this lesson? If so, what happened?

This article is issued from Wikiversity. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.