C/Introduction

< C

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)<br /?> 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 execute it appropriately. Software is made up of a lot of instruction which the CPU executes. We don't need to go in depth about the instructions, since they varies 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 feeding each instruction in via some external device, for example punch cards or switches. This task of writing programs were extremely slow and error prone.

The executable is executed instruction after instruction by the computer, which will then determined what to do based on the 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 is key to C's success.

Compiling

Machine code is basically a set of instructions that the computer can execute. Today, digital computers use binary as the format for storing information. This means each number can either be 1 or 0; which could be represented as on and off. Thankfully, a new method replaced machine code; this is called assembly. Although this is pretty consistent to writing binary, assembly still gave the program some edge, leading to less wasted time. Still, coding in assembly was still slow and error prone. Finally, programming languages started to appear. These made it easier for the programmer to create programs, while doing it on a time efficient schedule. Some of these languages convert their code to assembly, which will come in handy later in the course. Today, programming languages dominant assembly and programs aren't even written in binary anymore! 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 traditional 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 is 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, the code displays "Hello World!" (followed by a newline character "\n"). First, we call the function printf as such printf();. Within the parentheses, we put the arguments to be passed to printf, in this case there is only one argument, the string "Hello World!\n". But keep in mind that some functions, like printf, can take multiple arguments as needed. You may have noticed the semi-colon (;) at the end of printf. In C, a newline begins after a semi-colon, so it's important that you add one to the end of each line.

If you followed the instructions from the previous lesson and you're able to compile this file, 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?

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

This article is issued from Wikiversity - version of the Saturday, December 12, 2015. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.