Wednesday 21 December 2011

What are Pointers?

A pointer provides a way of accessing a variable without referring to the variable directly.
The mechanism used for this is the address of variable. The address act as an intermediate between the variable  and the program accessing it.
Let's have a look at the following example:


#include<stdio.h>
#include<conio.h>
void main()
{
   int *p, q;   //pointer variable always initialize with an *
     q = 100;   //assigning value to q 
     p = &q;    //inserting the address of q into p
       printf("%d",*p);  //see the description below
   getch();     //exit on key press                                                         
}

In the above program, the value of q (i.e. 100) is shown through the pointer variable p (i.e. *p). You can easily see in the picture above. First, store 100 the q variable and then the address of q (i.e. 1312) is given to pointer variable p and at last the value of q is print through pointer variable (*p).

If you still have any problem or confusion, you can contact me... 

Saturday 17 December 2011

How to display a colorful animated string/phrase


#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>      // for delay() function


void main(void)
{
    int driver=DETECT, mode;
    int i;    //for loop
    initgraph(&driver, &mode, "c:\\TC\\bgi" );
    textcolor(BLACK);
    for(i=0;i<=400;i++)
    {
    clrscr();
    setcolor(i/15);     //set text/string color
    outtextxy(20+i,150,"**C Language Codes**");  //see the description below
    delay(30);       //slow down the loop
    }
    getch();
    closegraph();
}


What is outtextxy(); ?


It is a built-in function defined in the header file "graphics.h". This function used to display graphical string. The general syntax is as follows:
outtextxy(x-coordinate, y-coordinate, "String to display"); 


In the above program, outtextxy() function is put into for loop to make the text animated. As you can see that variable i is written with the x-coordinate whereas, the y-coordinate remains constant which means the text will move towards left to right on the screen.


If you still have any question, you can ask me freely... 



Draw a colorful Circle


#include<graphics.h>
#include<conio.h>
#include<stdio.h>

void main(void)
{
    int driver=DETECT, mode;
    int xc1=320, yc1=240;


       initgraph(&driver, &mode, "c:\\TC\\bgi" );
         setcolor(7);                     //edge color
         setfillstyle(SOLID_FILL,GREEN);  // fill inside the circle
         circle(xc1,yc1,100);             // create a circle
         floodfill(xc1,yc1,7);            // flood color
     getch();
    closegraph();
}


Output:
















If you have any question, you can ask me freely...