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!

2 comments:

  1. where can i download this?

    ReplyDelete
  2. you can refer to my later post, there is a link to download turbo c

    ReplyDelete