0% found this document useful (0 votes)
11 views

TurboC Chp3

TurboC_Chp3

Uploaded by

theikdi
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

TurboC Chp3

TurboC_Chp3

Uploaded by

theikdi
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

Chapter 3

Standard Header File & their Functions

3.1 Basic Input & Output Header File (iostream.h)

cout - data output object


Example
cout<<"How old are you?";
cout<<"Hello students";
cout<<age;

cin - data input object


Example
cin>>age;
cout<<price;

Control String
\a bell \v vertical tab
\b backspace \\ backslash
\f formfeed \' single quote
\n newline \" double quote
\r carriage return \? question mark
\t horizonal tab \0 null

Output format width <iomanip.h>


setw(int) - width of the integer number
setprecision(int) - width of the decimal fraction

Type casting
(data type)variable - convert data type to another data type
Example
int a,b;
float c;
c=(float)a/b;
Example 3.1
/* example 3.1*/
//display statement on the screen
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"\a\a\a\t\t\tHello student\n";
cout<<"\a\a\a\t\t\tGood Morning!\n";
getch();
}

Example 3.2

/* example 3.2(a)*/ /* example 3.2(b)*/


//add the value of two variables //add the value of two variables
//using the assignment statement //using initialisation
#include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
clrscr(); clrscr();
int a,b,c; //variable declaration int a=5,b=10,c; //initialisation
a=5; //assignment statement c=a+b;
b=10; cout<<c;
c=a+b;; getch();
cout<<c; }
getch();
}
/* example 3.2(c)*/ /* example 3.2(d)*/
//add the value of two variables //add the value of two variables
//input from keyboard //display the output format
#include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
clrscr(); clrscr();
int a,b,c; //variable declaration int a,b,c; //variable declaration
cout<<"Enter the value of a and b: "; cout<<"Enter the value of a and b:"<<endl;
cin>>a>>b; cin>>a;
c=a+b; cin>>b;
cout<<a<<"+"<<b<<"="<<c; c=a+b;
getch(); cout<<"a= "<<a<<endl;
} cout<<"b= "<<b<<endl;
cout<<"a+b= "<<c;
getch();
}
Example 3.3
/* example 3.3(a)*/ /* example 3.3(b)*/
//convert the output variable //display the output format
#include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
void main() #include<iomanip.h>
{ void main()
clrscr(); {
int a,b; //variable declaration clrscr();
float c; int a,b; //variable declaration
cout<<"Enter the value of a and b: "; float c;
cin>>a>>b; cout<<"Enter the value of a and b: ";
c=(float)a/b; cin>>a>>b;
cout<<"a = "<<a<<endl; c=(float)a/b;
cout<<"b = "<<b<<endl; cout<<"a = "<<a<<endl;
cout<< "a/b = "<<c; cout<<"b = "<<b<<endl;
getch(); cout<<"a/b = "<<setw(5)<<setprecision(4)<<c;
} getch();
}
/* example 3.3(c)*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
float a,b,c; //variable declaration
cout<<"Enter the value of a and b: ";
cin>>a>>b;
c=a/b;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"a/b = "<<setw(5)<<setprecision(4)<<c;
getch();
}
/* example 3.4*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=10;
cout<<a<<endl;
++a;
cout<<a<<endl;
a++;
cout<<a<<endl;
cout<<++a<<endl;
cout<<a<<endl;
cout<<a++<<endl;
cout<<a<<endl;
cout<<--a<<endl;
cout<<a<<endl;
cout<<a--<<endl;
cout<<a<<endl;
getch();
}

3.2 Standard Input & Output Header File (stdio.h)

printf(control string, argument list);


- sends formatted output to standard input device.
Example
printf("How old are you?");
printf("Hello! student");
printf("%d",age);

scanf(control string, address of variable);


- stores the formatted input at an address passed as an
argument.
Example
scanf("%d",&age);
scanf("%f",&price);
getchar() - read a character from the standard input device.
Example
c=getchar();

putchar() - write a character to the standard output device.


Example
putchar(c);
getc() - read a character from the standard input device.
Example
getc(c,stdin);
putc() - write a character to the standard output device.
Example
putc(c);
gets() - read a string from the standard input device.
Example
gets(name);

puts() - write a string to the standard output device.


Example
puts("Good Morning!");

Common Conversion Specification


Conversion printf( ) scanf( )
character The argument is printed characters are converted to
%c as a character a character
%d as a decimal integer a decimal
%D as a long decimal integer a long decimal integer
%e scientific notation not used
%f as a floating point number a floating point number
%lf not used a floating point number
(double)
%s as a string a string
%o as an unsigned octal an unsigned octal
%x as an unsigned hexa an unsigned hexa
%u as an unsigned decimal an unsigned decimal
3.3 Console Input & Output Header File (conio.h)
clrscr() - clear in the current window
gotoxy(int x, int y) - move to cursor position
window(int left, int top, int right, int bottom) - define the new window
textattri(int new attr) - both foreground and background colors in a single cell
textcolor(int newcolor)- new color characters in text mode
textbackground(int newcolor) - new text background color
getch() - get a character from console but does not echo to
the screen
getche() - get a character from console but echo to the screen
cprintf(control string, argument list) - formatted output to the text
window on the screen

Example
window(1,1,80,25);
window(10,10,80,30);
textbackground(BLUE);
textcolor(LIGHTCYAN);
textcolor(7);
textbackground(2);
Value Constant foregroun backgrou
d nd
0 BLACK YES YES
1 BLUE YES YES
2 GREEN YES YES
3 CYAN YES YES
4 RED YES YES
5 MAGENTA YES YES
6 BROWN YES YES
7 LIGHTGRAY YES YES
8 DARKGRAY YES NO
9 LIGHTBLUE YES NO
10 LIGHTGREEN YES NO
11 LIGHTCYAN YES NO
12 LIGHTRED YES NO
13 LIGHTMAGENTA YES NO
14 YELLOW YES NO
15 WHITE YES NO

To make the characters blink in a call to textcolor, you add 128 to the
foreground color. The predefined constant BLINK exists for this purpose; for
example,
textcolor(CYAN + BLINK);

Example 3.5 Example 3.6


/* example 3.5*/ /* example 3.6*/
#include<conio.h> #include<conio.h>
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
clrscr(); clrscr();
int a,b,c; //variable declaration int a,b; //variable declaration
printf("Enter the value of a: "); float c;
scanf("%d",&a); printf("Enter the value of a: ");
printf("Enter the value of b: "); scanf("%d",&a);
scanf("%d",&b); printf("Enter the value of b: ");
c=a+b; scanf("%d",&b);
printf("a=%d\n",a); c=(float)a/b;
printf("b=%d\n",b); printf("a=%d\n",a);
printf("a+b=%d",c); printf("b=%d\n",b);
getch(); printf("a/b=%10.2f",c);
} getch();
}

Example 3.7 Example 3.8


#include <conio.h> #include <conio.h>
int main(void) int main(void)
{ {
int i, j; int i;
clrscr(); clrscr();
for (i=0; i<9; i++) for (i=0; i<9; i++)
{ {
for (j=0; j<80; j++) textattr(i + ((i+1) << 4));
cprintf("C"); cprintf("This is a test\r\n");
cprintf("\r\n"); }
textcolor(i+1); return 0;
textbackground(i); }
}
return 0;
}

3.4 Standard Library Header File (stdlib.h)


abs(int var) - get the absolute value of an integer
atof(const cbar *s) - convert a string to a floating point
atoi(const char *s) - convert a string to an integer
atol(const char *s) - convert a string to long int.
div(int,int) - divides two integers and return both the quotient
and the remainder
x=div(10,3); x.quot=3; x.rem=1;
ltoa(long int) - convert long int to string
itoa(long int) - convert integer to string
random(num) - returns a random number between 0 to num-1
randomize() - initializes the random number generator with a
random value
min(int,int) - returns the smaller of two values
max(int,int) - returns the larger of two values

Example 3.9
/* div & ldiv example */
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
div_t x; //declaration
ldiv_t lx; //declaration
int main(void)
{
clrscr();
x = div(10,3);
printf("10 div 3 = %d remainder %d\n",
x.quot, x.rem);
lx = ldiv(100000L, 30000L);
printf("100000 div 30000 = %ld remainder %ld\n", lx.quot, lx.rem);
return 0;
}

3.5 Character Type Header File (ctype.h)


toupper(int char)- convert character to upper case
tolower(int char) - convert character to lower case
toascii(int char) - convert character to ASCII code.

Example 3.10 Example 3.11


#include <string.h> #include <stdio.h>
#include <stdio.h> #include <ctype.h>
#include <ctype.h> #include <conio.h>
#include <conio.h>
int main(void) void main()
{ {
clrscr(); clrscr();
int length, i; int number, result;
char *string = "this is a string"; char ch;
length = strlen(string); number = 511;
for (i=0; i<length; i++) result = toascii(number);
{ printf("\t%d\t%d\n", number, result);
string[i] = toupper(string[i]); ch='A';
} result = toascii(ch);
printf("%s\n",string); printf("\t%c\t%d\n", ch, result);
/* convert lowercase and string is reverse order */ getch();
for (i=0; i<length; i++) }
{
string[i] = tolower(string[i]);
}
printf("%s\n",string);
return 0;
}

3.6 Mathematical Header File (math.h)


sin(value) - compute the sine of the input value
cos(value) - compute the cosine of the input value
tan(value) - compute the tangent of the input value
sqrt(value) - calculates the positive square root of the input value
log(value) - calcutes the natural logarithm of the input value
exp(value) - calculates the exponential function, e**x.
pow(base,exp) - calculates base to the power exp, x**y.

Exercises
1. Write a program to display the following statement on the screen.
[tab]Hello, my name's Sanda.
[tab] [tab] [tab]What's your name?, please.
2. Write a program to find the sum, product and average of three integer numbers and display the
results on the screen.
3. Write a program to find the area of triangle and display the result on the screen.
(area=0.5*base*height)

You might also like