Hello, welcome to this version of knowledge sharing thanks to blogger for hosting the entire application. Today is a little tricky in that we are just going to cover the basics of the C programming Language.
Brief History
The C programming language is a popular and widely used programming language for creating computer programs. Programmers around the world embrace C because it gives maximum control and efficiency to the programmer.
If you are a programmer, or if you are interested in becoming a programmer, there are a couple of benefits you gain from learning C.
- You shall be able to read and write code for a large number of platforms including everything from micro-controllers to the most advanced scientific systems can be written in C, and many modern operating systems are written in C.
- You can also have a free pass to the famous object oriented version of C well known for it's dominance in the Game Industry. Object oriented C++ language becomes much easier with C++ as an extension of C, and it is nearly impossible to learn C++ without learning C first.
C is a computer programming language meaning that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use and has been around for quite a long time hence winning widespread acceptance because of the fact that it gives programmers maximum control and efficiency.
C is an easy language to learn. It is a bit more cryptic in its style than some other languages, but you get beyond that fairly quickly.
C is a compiled language meaning that once you write your C program, you must run it through a C compiler such as (GNU gcc or Visual C++, Borland C++ among others) to turn your program into an executable that the computer can run (execute). The C program is the human-readable form, while the executable that comes out of the compiler is the machine-readable and executable form. What this means is that to write and run a C program, you must have access to a C compiler. If you are using a UNIX machine (for example, if you are writing CGI scripts in C on your host's UNIX computer, or if you are a student working on a lab's UNIX machine), the C compiler is available for free. It is called either "cc" or "gcc" and is available on the command line. If you are working at home on a Windows machine, you are going to need to download a free C compiler or purchase a commercial compiler. A widely used commercial compiler is Microsoft's Visual C++ environment (it compiles both C and C++ programs). Unfortunately, this program costs several hundred dollars. If you do not have hundreds of dollars to spend on a commercial compiler, then you can use one of the free compilers available on the Web.
We will start at the beginning with an extremely simple C program and build up from there. I will assume that you are using the UNIX command line and gcc as your environment for these examples; if you are not, all of the code will still work fine
Let me not bore you with more history but rather get started! Read
History of C for more.
Let's be polite and start by saluting the world! Type the following program into your favorite editor:
#include < stdio.h>
void main()
{
ello World\n");
}
printf("\n
H
Save the code in the file hello.c, then compile from your terminal by typing:
gcc hello.c -o objectfile
This creates an executable file a.out, which is then executed simply by typing its name. The result is that the characters `` Hello World'' are printed out, preceded by an empty line.
OK, lets step through the code bit by bit.
- A C program contains functions and variables. The functions specify the tasks to be performed by the program.
- We inform the compiler to include a header file called stdio.h to allow us use the system's input and output functions.
- Then we declare a main function to handle all the the main processing. All C programs start executing from the main function and there can only be one main function. anything you want to execute should be included in the main function.
- The result of compiling this source file is an executable called objectfile prefixed by the -o telling the compiler to name the executable that name otherwise, if not specified, the default name a.out is chosen instead.
- To run the executable, simply type ./objectfile. notice the dot (.) before the forward slash. (Specific to Unix like systems). You should see something like
Meaning, you have successfully run your first C program. Congs upon that.
The printf line prints the message ``Hello World'' on ``stdout'' (the output stream corresponding to the X-terminal window in which you run the code); ``\n'' prints a ``new line'' character, which brings the cursor onto the next line. By construction, printf never inserts this character on its own: the following program would produce the same result:
#include < stdio.h>
void main()
{
printf("Hello
printf("\n");
Douglas");
printf("\n");
}
Try leaving out the ``\n'' lines and see what happens. You'll find that it shall print and exit from the same line. so the ``\n'' simply allows you to print a statement and terminate from the next line. It's a more decent way of printing (outputting).
Next we shall be advancing more advanced Topics. See you then