Sample programs

 • We are going to discuss some simple programs in turbo C before going to graphics program that is terminal in graphic mode.

• So simple programs in text mode.

1) Program to print Hello world on different line.

#include<stdio.h>

#include<conio.h>

int main()

{

    int c= 5;

    for(int i=0;i<12; i=i+2)

    {

    gotoxy(c,i);

    printf("Hello world!");

    c=c+2;

    }

getch();

return 0;

}

Explanation:

• we have also discussed prior about text mode in Turbo C let's understand it in short.


• So this is the image representation of text mode where:

1) screen is divided in Rows and Columns.

2) we have 80 rows and 25 columns

3) Here first coordinate is of column and another is row example: (80,25)

• 80 is column and 25 is row only in turbo C text mode it's reverse as it is normally.

4)  program we did above is of Controlling the cursor with the help of function gotoxy( )  it's a function used to send the cursor to the specified coordinates in the active window. 

5) Here the first coordinate is the column (x) and the second coordinate is the row (y) as we discussed. Here it accepts relative coordinates to the current active window

Example:  gotoxy(40,12);

It sends the cursor to the 40th column and 12th row.

• Most of the text mode graphic functions are available in conio.h


2) Same program as above but with text in different color.

#include<stdio.h>

#include<conio.h>

int main()

{

  int c= 5, i ;

 for( i=0;i<12; i=i+2)

{

 gotoxy(c,i);

textcolor(i);

cprintf("Hello world!");

c=c+2;

}

getch();

return 0;

}

Explanation: 

1) textcolor() is the function used to set the foreground (text) color in the active window. We can use either numeric value or symbolic name to set the text color.

2) numeric value starts from 0 to 15 for foreground colour.

3) Generally the printf() statement prints the text at the active cursor position only with the default foreground color white. Where as cprintf() helps to print the text with the active window color.


3) Program to Set background color in same program.

#include<stdio.h>

#include<conio.h>

int main()

{

   int c= 5, i ;

    for( i=0;i<12; i=i+2)

{

    gotoxy(c,i);

    textcolor(i);

    textbackground(5);

   cprintf("Hello world!");


c=c+2;

}

getch();

return 0;

}

Explanation:

1) textbackground() is the function used to set the background color to the active window. It accepts either numeric color value or symbolic name as argument.

2) Numeric value starts from 0 to 7.


4) program to Print a string with changing colors.

#include<stdio.h>

#include<conio.h>

#include<dos.h>

int main()

{

   for(int i=0; i<=15; i++)

{

         gotoxy(35,15);

        textcolor(i);

       cprintf("Hello World");

        delay(1000);

}

getch();

return 0;

}

Explanation: 

1) delay() in Turbo C:

It is the function defined in dos.h, used to delay the execution for specified number of milliseconds.


• Hope you have got many other ideas to use this function in many different ways in text mode.

 

*You can leave a comment if you have doubt in it.



Comments

Popular posts from this blog

Functions of graphics

Line Basic Concepts

Shadow Mask technique