Introduction to C
From Wikiversity
Contents |
[edit] What is a computer program, what is programming, who is a programmer?
The system unit of a computer contains a CPU (Central Processing Unit) and a monitor and keyboard (and often a mouse) are connected to the system unit. The CPU is used to do all the processing. The monitor displays the result of computation. The keyboard and/or mouse are used for input.
The CPU does all the processing. Think of it like this - the CPU fetches the instructions from the memory, fetches the data (operands), executes the instruction and then stores the output. e.g. when we give the 'ADD' instruction to the computer, the computer will fetch operand 1 and operand 2 from the memory, compute operand 1 + operand 2 using the ALU (Arithmetic Logic Unit), and then output the result.
Thus, it is understood, that if we want to perform any task on the computer, we must first write instructions for it. A computer program is a set of instructions (a "recipe") that defines the task to be performed.
All the applications that we use on a computer like Microsoft Office, Notepad, Internet Explorer, Opera are also programs. In Internet Explorer or Firefox, when we click on the Back button or press ALT+B (Option+B on the Mac), millions of instructions are executed that check if you were on a previous page before you came to the page you're currently on (error checking), and if there was, it displays this previously visited page. When an action or event takes place (you click on the Back button or press ALT/Option+B), the corresponding task is performed (go to the previous page). Thousands of programmers have written programs containing millions of instructions to develop such complicated applications.
In fact, the Windows OS is also a very very very large (millions of lines) computer program written in C. Linux has also been written in C.
Most people think that programming is only limited to learning a language like C or Java. However, programming is more about understanding the grammar of implementing algorithms in the form of program instructions. That is why programming is closely tied with algorithms. Unless you have a sound knowledge of computer algorithms, you cannot write a computer program. To write a program to perform a task, we need to first convert the task into an algorithmic process, and then try to write a program for it.
The art of writing a computer program, whether it be a Hello World or a complete operating system, is called computer programming. Those who write computer programs are programmers.
[edit] Machine languages, lower level and higher level languages
A machine language is the computer's true language. Some would call it unreadable by human standards. However, to the trained eye it is digital poetry. Back in the old days of computing, all programs were written in machine language, and (before screens or printers existed) laboriously entered into the computer by calculating the machine code for the program and then feeding each instruction in via setting a bank of switches on or off then pressing a button to tell the computer to store this instruction and wait for the next one to be entered. Because the switches used in the switch bank were called "toggle switches", such an activity was often referred to as 'toggling in' the program.
"Machine code" or "machine language" is basically a set of instructions to the computer written in a numbering system such as octal, hexadecimal, or binary. No matter which numbering system used, it is generally reduced down to binary (0s and 1s) - toggling a program into a computer such as in the type referenced above would call for binary machine code to be used, although hexadecimal plays a big part in machine language and octal has its uses too.
Nowadays, machine language is rarely used because it's too cumbersome; even simple programs are long and hard to maintain. There are many low-level languages, such as assembly language, which define "mnemonics" of machine language instructions. Instead of writing 011101 you could write HLT. Assembly language has its devoted fans but it is almost as cumbersome and terse as machine language. High level languages are languages that resemble English (or sometimes other languages, such as Spanish). You may have heard of Python and Lisp before, both of which are high level languages. So now instead of writing 01110110101110, or MOV AX, 5 we can write a = 5. Instead of writing dozens of instructions, we can simply say print "Hello" to display text on a shell/console. We could go into further discussion, but for now the subject at hand!
[edit] Compiling and editing
In order to write C source code we need a text editor(Notepad, vi, emacs, etc.) and a compiler. Compiling makes our programs go from text to executable files(remember all a computer can truly understand is machine language). You can also use an Integrated Development Enviroment, or IDE. This is software that is basically a text editor with a compiler linked to it. This makes it easier to compile and execute programs. Most of the time, you can do this with just the click of a button. IDE's usually have more features than this, more specific to helping programmers. A very popular IDE for Windows is Dev-C++. You can learn more about it here.
On UNIX to compile a file:
cc foo.c -o bar.ext
Where extension is the executable extension(.exe on Windows, and files that are chmodded a+x on GNU/Linux). bar is the desired name of the executable(myprogram), and foo is the name of the source code file. All C files end in a .c extension.
Clearer example:
cc test.c -o test
Remember, test.c must actually exist and be real C code in order to be compiled successfully. At first these concepts seem a bit difficult, but test things out in order to fully understand. Programming is learning by doing. You learn algebra by example and practice. It's the same with programming and generally anything else.
See also: What you Need before you can Learn
[edit] A Taste of C
Here is the traditional Hello World program written in C.
[edit] #include
Most C programs have #include statements, the most common being "#include <stdio.h>" What this statement really does is tell the compiler: "Hey! Before you compile my program, just copy and paste the contents of stdio.h where this statement is". This is used to include the contents of standard C libraries.
In our program we have this line which does just that:
#include <stdio.h>
[edit] Functions
In C, code is cut up into little "blocks" of code called functions. You can have a function called add_two_numbers() which would do just that. Don't worry about making functions for now though. Just know about the main() function:
int main(void) {
This fragment of code marks the beginning of the main() function, the most important function in almost every C program. The main function is the function automatically run when the program starts executing. Any and all other functions must be called directly or indirectly from main() in order to be executed. The keyword "int" indicates that the return value of main() is an integer. The keyword void inside the parentheses indicates that main takes no arguments.
The main() function can also accept command line arguments, which you will be familiar with if you've used CLI systems such as Unix or DOS, or the Windows Command Prompt. In that case, main is declared as follows. Don't worry too much about this for now; it will become relevant later.
int main(int argc, char **argv) {
[edit] printf
The printf() function is arguably the most used function in C. It is used to display formatted text to the standard output and is extremely powerful, even allowing you to make tables of data. But our use is very modest:
printf("Hello World!\n");
The heart of our program, this code displays "Hello World!" (followed by a newline character "\n") onto the standard output (usually the console). 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.
[edit] Ending Functions
Good things must end, functions included. Functions that return a value to the caller have to do so by using a return followed by an expression that evaluates to the type that must be returned. A function that does not return a value can be terminated with a plain return; statement. A closing brace terminates the definition of the function. return statements can appear anywhere in the function definition.
return 0; }
The end of our program! return 0; returns the value 0 to whatever called the main function (in this case the command line). The value 0 indicates successful completion of the program. The value 0 ties in directly with the int keyword in the declaration of main. The closing brace signals the end of the main function.
| Project: Topic:C |
| Previous: Topic:C/Before_you_start — Introduction to C — Next: Basic Output |