Friday, November 23, 2007

Chapter 1, Lesson 2

Welcome to Beginners Programming.

The first time I wrote a program was just to display “hello world!” on the screen. It was not a PC though, I cannot remember the model of that gadget but surely the label was “ATARI”. During that time, computer games was like squares and circles, it was that time of “Space Invaders” or at least it was. Those were the days, computer games today changed tremendously.

Now let’s try to make a simple program which displays “Hello World” on the screen.

Figure 1:

Flowchart: (please see lesson1 for basic flowchart symbols)


Above the graphical representation of a program which prints “Hello World!” on the screen. Now we are going to translate the flowchart using Pascal, C and C++ programming languages.

Pascal Code

C Code

C++ Code


Program HelloWorld;
uses crt;
begin
writeln("Hello World!");
end.


#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}


#include <iostream.h>
int main(void)
{
cout
<< "Hello World!\n";
return 0;
}


The flowchart will serve as your blueprint, your guide developing a program. But as I have mentioned earlier, our world will be evolving within the C language. By the way, we will be using Borland C/C++ IDE, a DOS based program. If you do not have Borland C/C++ IDE you can download it from http://www.simonhuggins.com/courses/progbegin/c/download/index.htm

The Code:

#Include <stdio.h>

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

Note: C language is case sensitive meaning, capital letter N is not the same as small letter n

Dissection:

#Include

Tells the processor to treat the contents of the file stdio.h as if the contents had appeared in the source program at the point where the directive appears

stdio.h

Which stands for "standard input/output header", is the header in the C standard library that contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations

int main(void)

It is a special purpose function in C programs. When the program is then run or executed, the runtime environment will first call the main function otherwise it will be like some other function in the program.

{

The open curly brace indicates the beginning of the definition of the main function

printf(“Hello World!\n”);

This line calls (executes the code for) a function named printf, which is declared in the included header stdio.h and supplied from a system library. The \n is an escape sequence that C translates to the newline character, which on output signifies the end of the current line. The return value of the printf function is of type int, but it is silently discarded since it is not used by the caller. (A more careful program might test the return value to determine whether or not the printf function succeeded.) The semicolon ; terminates the statement, and the following blank line is simply ignored.

*from wikipedia

return 0;

This line terminates the execution of the main function and causes it to return the integer value 0, which is interpreted by the run-time system as an exit code (indicating successful execution).

}

The open curly brace indicates the end of the definition of the main function

Ok, so now are you ready to run the program? Running the program is just easy, like MSWord, the shortcut key for save is

CTRL+S (save document). In our case CTRL+F9 (execute the program)

You will notice that your program will just flicker or you will not notice it at all. “what then? Nothing happened…”, well something did happen, the IDE successfully executed your program.


“Really?”




If you want to see the output, press CTRL+F5.

Figure 2:

Output:


And you have your output. CONGRATULATIONS! You created your first program.

You can add as may printf as you want in your code. Go ahead try it.. explore…


printf(“this is a C program\n”);
printf(“Im gonna love this\n”);
printf(“its really fun\n”);
printf(“thank you very much\n”);

Well that’s it for now. I hope I have gotten your feet wet.

Next lesson in beginners programming, we will be learning how to display “hello world!” somewhere on the screen.



-End of lesson 2 Beginners Programming

No comments: