TO UNDERSTAND about stdio.h in C.
TO LEARN ABOUT Math.h in C.
To learn about ctype.h in C.
To understand stdlib.h in c.
To learn about conio.h in c.
To learn about String.h in c.
TO LEARN ABOUT process.h in C.
String Manipulation Function and Header File Functions
1. PAPER: INTRODUCTION PROGRAMMING LANGUAGE USING C
PAPER ID: 20105
PAPER CODE: BCA 105
DR. VARUN TIWARI
(ASSOCIATE PROFESSOR)
(DEPARTMENT OF COMPUTER SCIENCE)
BOSCO TECHNICAL TRAINING SOCIETY,
DON BOSCO TECHNICAL SCHOOL, OKHLA ROAD , NEW DELHI
3. OBJECTIVES
IN THIS CHAPTER YOU WILL LEARN:
1. TO UNDERSTAND ABOUT STDIO.H IN C.
2. TO LEARN ABOUT MATH.H IN C.
3. TO LEARN ABOUT CTYPE.H IN C.
4. TO UNDERSTAND STDLIB.H IN C.
5. TO LEARN ABOUT CONIO.H IN C.
6. TO LEARN ABOUT STRING.H IN C.
7. TO LEARN ABOUT PROCESS.H IN C.
4. STDIO.H:
Function Description
printf()
It is used to print the character, string, float, integer, octal and
hexadecimal values onto the output screen.
scanf() It is used to read a character, string, numeric data from keyboard.
gets() It reads line from keyboard
puts() It writes line to output screen
fopen() fopen() is used to open a file in different mode
fclose() closes an opened file
getw() reads an integer from file
putw() writes an integer to file
5. EXAMPLE PRINTF AND SCANF
#include <stdio.h>
void main()
{
int a;
printf(“Enter any number-:");
scanf(“%d”,&a);
printf(“output is -:%d”,a);
}
6. EXAMPLE FOPEN AND FCLOSE
FOPEN FUNCTION IS USED TO OPENING A FILE IN DIFFERENT MODES AND FCLOSE FUNCTION IS USED
TO CLOSING A FILE.
#include<stdio.h>
void main(){
FILE *a;
a = fopen("file.txt", "w");//opening file
fprintf(a, "Hello how r u.n");//writing data into file
fclose(a);//closing file
}
7. EXAMPLE GETS AND PUTS
GETS() FUNCTION IS USED TO READS STRING FROM USER AND PUTS() FUNCTION IS USED TO PRINT THE STRING.
#include<stdio.h>
#include<conio.h>
void main(){
char n[25];
clrscr();
printf("enter your name: ");
gets(n);
printf("your name is: ");
puts(n);
getch(); }
8. EXAMPLE OF GETW() AND PUTW()
GETW() FUNCTION IS USED TO READ A INTEGER VALUE FROM A FILE AND PUTW() IS USED TO WRITE AN INTEGER VALUE FROM A FILE.
#include <stdio.h>
void main () {
FILE *f;
int a,n;
clrscr();
f = fopen ("bcd1.txt","w");
for(a=1;a<=10;a++) {
putw(a,f); }
fclose(f);
f = fopen ("bcd1.txt","r");
while((n=getw(f))!=EOF)
{ printf("n Output is-: %d n", n);} fclose(f); getch(); }
9. MATH.H:
Function Function Description
ceil It returns nearest integer greater than argument passed.
cos It is used to computes the cosine of the argument
exp It is used to computes the exponential raised to given power
floor Returns nearest integer lower than the argument passed.
log Computes natural logarithm
log10 Computes logarithm of base argument 10
pow Computes the number raised to given power
sin Computes sine of the argument
sqrt Computes square root of the argument
tan Computes tangent of the argument
10. EXAMPLE OF CUBE() , CEIL() , EXP() AND COS()
#include <stdio.h> #include <math.h>
#define pi 3.1415
void main()
{ double n =4.6,a=24.0,res; clrscr();
res = ceil(n);
printf("n ceiling integer of %.2f = %.2f", n, res);
a = (a * pi) / 180;
res = cos(a);
printf("n cos value of is %lf radian = %lf", a, res);
res = exp(n);
printf("n exponential of %lf = %lf", n, res); getch(); }
11. EXAMPLE OF POW, LOG, FLOOR AND LOG10
#include <stdio.h>
#include <math.h>
void main()
{ double n = 4.7,p=-9.33,res,b=3,po=4; clrscr();
res = pow(b,po);
printf("n %lf ^ %lf = %lf", b, po, res);
res = log(n);
printf("n log value is %f = %f", n, res);
res = floor(p);
printf("n floor integer of %.2f = %.2f", p, res);
res = log10(n);
printf("n log10 value is %f = %f", n, res);
getch(); }
12. Example of sin(), sqrt() and tan() in c.
#include <stdio.h>
#include <math.h>
void main()
{ double n = 4.7,sr,res;
clrscr();
res = sin(n);
printf("n sin value is -: %lf = %lf", n, res);
sr = sqrt(n);
printf("n square root of %lf = %lf", n, sr);
res = tan(n);
printf("n tan value is -: %lf = %lf", n, res); getch(); }
13. CONIO.H:
Function Function Description
clrscr() This function is used to clear the output screen.
getch()
This function is used to hold the screen until any character not press from
keyboard
textcolor() This function is used to define text color.
textbackground() This function is used to define background color of the text.
getche() It is used to get a character from the console and echoes to the screen.
14. Example of getch(),getche()
void main()
{
char c;
int p;
printf( "Press any keyn" );
c = getche();
printf( "You pressed %c(%d)n", c, c );
c = getch();
printf("Input Char Is :%c",c);
getch();
}
15. Example of clrscr(), textcolor() and textbackground()
#include<conio.h>
void main()
{ int i;
clrscr();
for(i=0; i<=15; i++)
{ textcolor(i);
textbackground(10-i);
cprintf("Bosco Technical Training Society");
cprintf("rn"); }
getch(); }
16. CTYPE.H:
Function Function Description
isalnum Tests whether a character is alphanumeric or not
isalpha Tests whether a character is alphabetic or not
iscntrl Tests whether a character is control or not
isdigit Tests whether a character is digit or not
islower Tests whether a character is lowercase or not
ispunct Tests whether a character is punctuation or not
isspace Tests whether a character is white space or not
isupper Tests whether a character is uppercase or not
tolower Converts to lowercase if the character is in uppercase
toupper Converts to uppercase if the character is in lowercase
17. Example of iscntrl() , isdigit() , isupper(),
islower()
#include <stdio.h> #include <ctype.h>
void main() { char c='n';
char p; char q,r; clrscr();
if(iscntrl(c)) printf("n u entered control value"); else printf("n not a control character");
printf("n enter any numeric value"); scanf("%c",&p);
if(isdigit(p)) printf("n entered value is digit"); else printf("n not digit");
q='a'; if(islower(q)) printf("n entered value is lower"); else printf("n not lower");
printf("n");
r='p'; if(isupper(r)) printf("n entered value is upper"); else printf("n not upper");
getch();
}
18. Example of isalnum() , isalpha() , ispunct(),
isspace()
#include <stdio.h> #include <ctype.h>
void main() { char c='c'; char p;
char q,r; clrscr();
if(isalpha(c)) printf("n u entered alphabetic value"); else printf("n Not an Alphabet");
printf("n Enter any numeric value"); scanf("%c",&p); if(isalnum(p))
printf("n Entered value is alphanumeric"); else printf("n Not alphanumeric");
q=' '; if(isspace(q)) printf("n Space is here"); else printf("n Space is not here"); printf("n");
r=':'; if(ispunct(r)) printf("n Entered value is punctuation"); else printf("n Not Punctuation");
getch(); }
19. Example of ispunct() , isspace() , isupper(), tolower() and toupper()
#include <stdio.h>
#include <ctype.h>
void main()
{ char c;
c = 'M'; res = tolower(c); printf("tolower is -: %c = %c n",
c, res);
c = 'h'; res = toupper(c); printf("toupper is-: %c = %c n", c,
res);
getch();}
20. STRING.H:
Function Function Description
strlen() computes string's length
strcpy() copies a string to another
strcat() concatenates(joins) two strings
strcmp() compares two strings
strlwr() converts string to lowercase
strupr() converts string to uppercase
21. Example of all string function
#include <stdio.h> #include <string.h>
void main() {
char a[20]="java"; char b[20]="program"; char c[20]; int res,d,g;
char s1[] = "vivek", s2[] = "viVek", s3[] = "vivek"; char q[40];
clrscr(); d=strlen(a); g=strlen(b);
printf("n length is a-: %d",d);
printf("n length is b -%d",g); strcpy(a,c); puts(c); printf("n %s",strcat(a,b));
res = strcmp(s1, s2); printf("n strcmp(s1, s2) = %dn", res); res = strcmp(s1,
s3);
printf("n strcmp(s1, s3) = %dn", res); printf("n %s",strlwr(a)); printf("n
%s",strupr(b));
getch();
}
23. Example of abort(),exit()
#include <stdio.h>
#include <stdlib.h>
void main () { FILE *f; f= fopen ("test.txt","r");
if (f == NULL) {
fputs ("error opening filen",stderr); abort(); } fclose (f); }
#include <stdio.h>
#include <stdlib.h>
void main ()
{ FILE *f; f = fopen ("test.txt","r");
if (f==NULL) { printf ("Error opening file"); exit (EXIT_FAILURE); } else
{ /* opening a file code here*/ }}
24. Example of system(),getpid()
#include <stdio.h>
#include <stdlib.h>
void main () {
system(“cls”);
system(“dir”);
printf(“n Process id of this program-: %X”,getpid());
getch();
}
25. STDLIB.H:
Function Function Description
atof Convert string to double (function )
atoi Convert string to integer (function )
atol Convert string to long integer (function )
rand Generate random number (function )
abort Abort current process (function )
Exit Terminates the program (function)
26. Example of atoi() , atol() , atof()
#include <stdio.h>
#include <stdlib.h>
void main ()
{ long int li;int i;
double n,m; double pi=3.141;
char buf[100];
printf ("enter degrees: ");
fgets (buf,100,stdin);
n = atof (buf);
m = sin (n*pi/180);
printf ("the sine of %f degrees is %fn" , n, m);
printf ("enter a long number: ");
li = atol(buf);
printf ("the value entered is %ld. its double is %ld.n",li,li*2);
i = atoi (buf);
printf ("the value entered is %d. its double is %d.n",i,i*2);
getch(); }
27. Example of abort(),exit()
#include <stdio.h>
#include <stdlib.h>
void main () { FILE *f; f= fopen ("test.txt","r");
if (f == NULL) {
fputs ("error opening filen",stderr); abort(); } fclose (f); }
#include <stdio.h>
#include <stdlib.h>
void main ()
{ FILE *f; f = fopen ("test.txt","r");
if (f==NULL) { printf ("Error opening file"); exit (EXIT_FAILURE); } else
{ /* opening a file code here*/ }}
28. Example of rand()
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main ()
{ int s,g;
srand (time(NULL));
s = rand() % 10 + 1;
do { printf ("Guess the number (1 to 10): ");
scanf ("%d",&g);
if (s<g) puts ("The secret number is lower");
else if (s>g) puts ("The secret number is higher");
} while (s!=g);
puts ("Hurrah your secret no is equal guess no");
return 0; }