Tuesday, September 7, 2010

Saturday, September 4, 2010

Turbo C Programming Day 1b: Basic input, output and variables

      Let's continue.


Clear Screen

      Notice here, we add the new line clrscr();
    This is important especially in Turbo C so that you'd get a clean output. What it does is simply clear the output screen before writing to it. This is especially useful when you had run a program previously, that outputs something to screen. And if you run your current program, your  current output will not be concatenated on the previous content of the screen.


     This is one such example. You have run the same program previously so the output screen has the values "Hello World". Now if you run the program again, if you don't clear the screen before writing to out, you'd get "Hello WorldHello World". 

     But if you perform clrscr() before writing to screen in this particular example, you'd get


     So that's it for clrscr(). In other modern programming languages, this isn't needed as the output buffer is already automatically taken care of.

      In this modified code listing, notice that we now have a variable declaration.

    A variable is simply a placeholder for a value in a program. Each variable holds a value that persists throughout the whole lifespan of the program executing, that can be updated and extracted as needed. 

Variables


     In this example, we declare a variable and call it 'a', and it's datatype is 'int' or integer. We then understand that the variable 'a' must contain whole numbers and can also perform arithmetic with it.

     Notice that we assign a value to it, 5, and that's without quotations because a value with single quotations ' ' is a character, while double quotations " " signifies a string.

     On the printf command which is slightly different from the previous examples show that we would like to output a %d, or an integer, and this integer gets its value from 'a', the second parameter of printf.



Executing the above code will have the output as shown above.

Character variables


     Suppose we change the data type of 'a' into 'char'. This time, this tells the compiler that 'a' is a character and it's values assigned to it are enclosed in ' '. In this example, we assign the character 'x' to variable 'a'.

     We execute printf with %c, which tells the system that what is outputted is character type.


Here is the output.

String Variables


     What if we want to store inside variables some words, or phrases or sentences? If our variable is of data type character, it will only hold 1 character! So how do we accomplish this? In modern programming languages, there is already a new data type called string, which stores values that contains more than 1 characters. 

     But in Turbo C, either we use a character array, or a pointer to a character variable. An indication that a variable is a pointer is the asterisk that precedes its variable name, as shown above, *name. We will have an in-depth discussion about pointers in the future articles but for now, let's focus on storing a string on a character data type.

     Notice that we assign "sky" which is enclosed by double quotations to name. "sky" is a string. 

     And when we print out with printf, we use %s, for string.


The output of course is as shown above.

Keyboard Input into Variable



      Take a look at the modified code above. There's a new command, gets(). What does it do? 

     It gets a value inputted into a keyboard by the user. And assigns that value to the parameter in gets. In this example, that value is placed in variable "name". 

     The code above is pretty self explanatory, the program asks for your name, you enter your name, and after entering, you hit the "Enter" key, then the program greets you!


Then you type your name,



After you type your name, hit 'Enter'.



That's it!

Variable input for calculation

     
     Another example of the usefulness of a user input. The program code shown above simply asks for two values from the user, the first operand and the second operand, which it adds, and shows the result to the screen.

     Notice that instead of gets, we use scanf with first parameter %d to tell it that we want to save to &a, and &b, an integer. Notice also the ampersand(&) beside variables a and b. This tells us that we want to write to the memory location of 'a' and 'b' instead, the values entered by the user.


Here's the sample output.

Feel free in trying out different uses of user input and calculations as well as screen display!

Stay tuned!

Friday, September 3, 2010

Turbo C Programming Day 1a: Basic input, output and variables

Hi all. So this will be the start of a series of tutorials as a crash course for Turbo C Programming. 
As students probably know, Turbo C is one of the first programming languages taught at college.

It's interface looks primitive compared to modern IDEs such as Visual Studio, but still, Turbo C can be a powerful programming language, and a fundamental knowledge on computer programming in general begins here.



Assuming you already have Turbo C installed, let's continue. First, as shown in screenshot above, we need to set first the directories so the compiler knows where to find the files it needs.

Assuming you install your Turbo C in "C:\TC", so your directories settings should be same as above. Never forget this, as this is a common issue many newbies encounter.



Now let's begin coding. Shown above is a code listing for a very simple program for basic output, printing to screen the words "Hello World".
#include   <= this one tells the compiler to include the code listing found in the file stdio.h, which in general contains basic routines for standard input and output functions, and this includes the statement "printf".

main()  <= like in every programming language, every program must have a "main()" routine or function, which is the starting or entry point of the program. Notice that every function must be enclosed in { and }.

printf("Hello World");   <= notice that every line of code, except for comments, within a function must be ended by semicolon(;).

The statement "printf" is an example of a method, a routine that accepts a parameter in between its parentheses and does an action about it. In this case, the parameter is "Hello World", and the action is printing it on screen.

getch();  <= is simply a method or command to tell the system to wait for user keyboard input before terminating. Without this, we wouldn't be able to see "Hello World" outputted to screen as the program is immediately terminated after printing in split second.


To actually make the program run, after saving(Shortcut F2), we must compile it first by choosing the option above.


After compilation, we run the program by choosing the menu option above.


And here is our first output, hooray! We are now able to print words, sentences, or even paragraphs onto a computer screen!

P.S: Printing "Hello World" to output stream is already a programming tradition when it is your first time trying out a programming language.

If you have questions, please feel free to ask, or leave a comment. Thanks! Stay tuned!