Friday, January 6, 2012

3. Printing and Remembering values within C

You might be feeling at home if you've read the older posts. And yes it's a good thing and I must tell that this feeling of thinking things easier as they were seen before is a good habit and will help you a lot in your journey. I too have been through the difficulties to solve problems and when they are solved, they look like as same as when the mystery behind the magic is revealed(Nothing hard to understand remains behind). You will feel good to solve different problems by yourself or with a little guidance or trying hard by challenging yourself.
And yes if you stuck at a problem and could not solve it by yourself, you might search for the code over the web. But I must assure you that the code you might see can be out of your expectations. It can be hard to read, understand and many new terms might be disclosed to you.
This is generally because the diversity in the thinking to solve a single problem and proficiency level. But I must suggest, rather to search for code, do search for the logic to solve a given problem. Or use forums and post your problem and ask for the logic. You can even use this site to get your problem solved. Once you know the logic, you only need the instructions, the words, that will tell the compiler what commands must be followed and executed.


For e.g. let us assume a real life problem, suppose Rohan age is 25 and his father is 20 years older than him and his grandfather is 42 years older than him.
Then the following code might work for you.

#include<stdio.h>
main()
{
printf("%d",35);
printf("%d",35+25);
printf("%d",35+42);
}


Firstly, the %d tells the printf that we want to print the numbers rather than a message.
try and use printf("35+25"); and see the difference by yourself.
These are called format specifiers and some most common of them can be seen in use here.

But why had we used 35 again when calculating his father's and grandfather's age. And If Rohan grows by a year then we also must have to change it wherever we have used his age. Can't we tell the compiler to remember Rohan's age once we have provided it? Yes, computer also got a memory as like us. But to use it you must tell the compiler the type of data you'll store and the name of the memory location you are going to designate with.
So here is it
 int age;
//now compiler will provide a memory for you to store an integer value and we can designate that memory with its new name i.e. age. So as we got memory, we can simply put our value in it

age=35;

So now the old code can be generalized as

#include<stdio.h>
main()
{
int age;
age=35;
printf("%d",age);
printf("%d",age+25);
printf("%d",age+42);
}


TRY: Command compiler to remember all the values i.e Rohan's age, his father's age and grandfather's age and then add 1 to all of their age(assumed a year has been passed) and then print the resulting age of all of them.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Web Host