1. UNIT 4-HEADER FILES IN C
A header file is a file with extension .h which contains C function
declarations and macro definitions to be shared between several source
files.
Typesof Header files
Sy stem header files: It iscomes withcompiler.
User headerfiles: It is written by programmer.
You request to use a header file in your program by including it with the
C preprocessing directive #include, like you have seen inclusion
of stdio.hheader file, which comes along with your compiler.
Including a header file is equal to copying the content of the header file
but we do not do it because it will be error-prone and it is not a good
idea to copy the content of a header file in the source files, especially if
we have multiple source files in a program.
A simple practice in C or C++ programs is that we keep all the constants,
macros, system wide global variables, and function prototypes in the
header files and include that header file wherever it is required.
1. math.h header file in C:-
Function De s cr iption Exam ple
sqrt(x) square root of x
sqrt(4.0) is 2.0
sqrt(10.0) is 3.162278
exp(x) exponential (ex
)
exp(1.0) is 2.718282
exp(4.0) is 54.598150
log(x) natural logarithm of x (base e)
log(2.0) is 0.693147
log(4.0) is 1.386294
log10(x) logarithm of x (base 10)
log10(10.0) is 1.0
log10(100.0) is 2.0
2. Function De s cr iption Exam ple
fabs(x) absolute value of x
fabs(2.0) is 2.0
fabs(-2.0) is 2.0
ceil(x) rounds x to smallest integer not less than x
ceil(9.2) is 10.0
ceil(-9.2) is -9.0
floor(x) rounds x to largest integer not greater than x
floor(9.2) is 9.0
floor(-9.2) is -10.0
pow (x,y) x raised to pow er y (xy
) pow (2,2) is 4.0
fmod(x) remainder of x/y as floating-point number fmod(13.657, 2.333) is 1.992
sin(x) sine of x (x in radian) sin(0.0) is 0.0
cos(x) cosine of x (x in radian) cos(0.0) is 1.0
tan(x) tangent of x (x in radian) tan(0.0) is 0.0
Examples:-
SUBMIT
#include <stdio.h>
#include <math.h>
int main()
{
4. 10
11
12
13
14
15
16
17
18
if(isalnum(ch))
printf("%c is an alphanumeric character.n",ch);
else
printf("%c is not an alphanumeric character.n",ch);
return 0;
}
First run:
Enter a character: H
H is an alphanumeric character.
Second run:
Enter a character: %
% is not an alphanumeric character.
2) isalpha()
This function checks whether character is alphabet or not.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* C example program of isalpha().*/
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
if(isalpha(ch))
printf("%c is an alphabet.n",ch);
else
printf("%c is not an alphabet.n",ch);
return 0;
}
First run:
Enter a character: Y
Y is an alphabet.
Second run:
Enter a character: 9
9 is not an alphabet.
3) isdigit()
This function checks whether character is digit or not.
1
2
/* C example program of isdigit().*/
#include<stdio.h>
5. 3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<ctype.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
if(isdigit(ch))
printf("%c is a digit.n",ch);
else
printf("%c is not a digit.n",ch);
return 0;
}
First run:
Enter a character: Y
Y is not a digit.
Second run:
Enter a character: 9
9 is a digit.
4) isspace()
This function checks whether character is space or not.
5) isupper()
This function checks whether character is an uppercase character or not.
6) islower()
This function checks whether character is a lowercase character or not.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* C example program of isspace(), isupper(), islower() .*/
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
if(isupper(ch))
printf("%c is an uppercase character.n",ch);
else if(islower(ch))
printf("%c is an lowercase character.n",ch);
else if(isspace(ch))
printf("%c is space.n",ch);
else
6. 17
18
19
20
21
22
printf("%c is none from uppercase, lowercase and
space.n",ch);
return 0;
}
First run:
Enter a character: T
T is an uppercase character.
Second run:
Enter a character: t
t is an lowercase character.
Third run:
Enter a character: main
m is an lowercase character.
Fourth run:
Enter a character:
is space.
Fifth run:
Enter a character: *
* is none from uppercase, lowercase and space.
7) ispunct()
This function checks whether character is a punctuation character or not.
Punctuation characters are , . : ; ` @ # $ % ^ & * ( ) < > [ ] / { } ! | ~ - _ + ? = ' "
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* C example program of ispunct() .*/
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
if(ispunct(ch))
printf("%c is a punctuation character.n",ch);
else
printf("%c is not a punctuation character.n",ch);
return 0;
}
First run:
Enter a character: !
! is a punctuation character.
Second run:
Enter a character: ,
7. , is a punctuation character.
Third run:
Enter a character: +
+ is a punctuation character.
8) isprint()
This function checks whether character is printable or not.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* C example program of ispunct() .*/
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
if(isprint(ch))
printf("%c is a printable character.n",ch);
else
printf("%c is not a printable character.n",ch);
return 0;
}
Enter a character: x
x is a printable character.
9) toupper()
This function returns character in upper case.
10) tolower()
This function returns character in lower case.
1
2
3
4
5
6
7
8
9
10
11
/* C example program of toupper() and tolower() .*/
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
printf("Upper case: %c, Lower case:
%cn",toupper(ch),tolower(ch));
8. 12
13
14
15
return 0;
}
First run:
Enter a character: w
Upper case: W, Lower case: w
Second run:
Enter a character: 9
Upper case: 9, Lower case: 9
3. stdio.hheaderfile libraryfunctionsinC:-
1. perror() function of stdio.h in C.
In this article, we are going to learn about the perror() function of stdio.h header file in C programming language,
and use it prints a descriptive error message to stderr.
2. puts() and putchar() functions of stdio.h in C.
In this article, we are going to learn about the putchar() and puts() function of stdio.h header file in C
programming language and use it put string and characters on console.
3. gets() function of stdio.h in C.
In this article, we are going to learn about the gets() function of stdio.h header file in C programming language,
and use it get string and then print it on console.
4. putc() function of stdio.h in C.
In this article, we are going to learn about the putc() function of stdio.h header file in C programming language,
and use it to put characters inside a file with the help of file pointer.
5. fgets() function of stdio.h in C.
In this article, we are going to learn about the fgets() function of stdio.h header file in C programming language
and use it while getting the characters from file pointer.
6. fscanf() function of stdio.h in C.
In this article, we are going to learn about the fscanf() function of stdio.h header file in C programming language
and use it to scan rewind variables.
Explainationswithexamples:-
1) perror() function in C:-
This function is very useful as it prints the error message on the screen. All we have to do is if you have a situation
where we caught an error; call this function with a parameter of string.
The String which will be printed first, a colon, a space then the error message.
Example:
perror("here is the error");
will print →
here is the error : ERROR MESSAGE.
stdio.h - perror() function Example in C
#include <stdio.h>
int main ()
9. {
// defining the file pointer
FILE *f;
// rename file name
rename("abc.txt", "abcd.txt");
// open file
f = fopen("file.txt", "r");
// condition if error then print error
if( f == NULL ) {
perror("Error in Code is: ");
return(-1);
}
// close the file
fclose(f);
return(0);
}
Output
2) puts( ) and putchar( ) functions:-
The function puts() is used to print strings while putchar() function is used to print character as their names
specifies.
These functions are from the stdio.h class doing the jobs related to strings.
Example:
puts("I AM A STRING");
Output:
I AM A STRING
putchar("a");
Output:
a
You can also use printf() which havemore options then these two functions.
#include <stdio.h>
#include <string.h>
int main ()
{
10. // initializing the variables
char a[15];
char b[15];
char c;
// coming string in a and b
strcpy(a, "includehelp");
strcpy(b, "ihelp");
// put a and b
puts(a);
puts(b);
// printing characters
for(c = 'Z' ; c >= 'A' ; c--)
{
putchar(c);
}
return(0);
}
Output
3) gets( ) in C:-
The function gets() is used to scan a string from the user without any n or till the n character. This function is
from the string.h class doing the jobs related to strings.
Example:
gets(str) ;
User input: "I AM A STRING"
Output:
I AM A STRING
You can also use scanf() which shavemore options then this function.
stdio.h - gets() function Example in C
11. #include <stdio.h>
int main ()
{
//initializing the type of variables
char str[50];
char c;
//message for user
printf("Please enter a string : ");
gets(str);
//printing the result
printf("Your String: %snn", str);
//message for user
printf("Please enter a character: ");
c = getchar();
//printing the result
printf("Your Character: ");
putchar(c);
return(0);
}
Output
4) putc( ) in C:-
If you are handling files in your application then coping one file into other or from the same file will something
you will encounter. Therefore, using the function putc() will help you doing this.
The Function is use to write a single character in file. TheFunction needs only two parameters first a character and
second is the file itself.
Example:
putc('a', FILE);
12. //Here, FILE is the file variable.
Output:
It will write 'a' on the file at the current
position of the cursor.
Examplesalreadydone inclass.
5) fgets( ) inC:-
If you are handling files in your application then reading from a file is something that you cannot ignore.
Therefore, the function fgets() will help you doing this.
The Function is use to read a string from a file. The Function needs only three parameters, first a character array,
second is the number of character you want to read and the third is the file itself.
Example:
fgets (str, 60, F);
//Here, str is a character array, F is the file variable.
It will read 60 characters from the file from the current position of the cursor.
stdio.h - fgets() function Example in C
#include <stdio.h>
int main ()
{
//initializing the file pointer
//and type of variables
FILE *F;
char str[60];
//open file abc in read mode
F = fopen("abc.txt" , "r");
if(F == NULL) {
perror("Error is:");
return(-1);
}
if( fgets (str, 60, F)!=NULL ) {
//printing the output on console
puts(str);
}
fclose(F);
return(0);
}
Output
13. 6) fscanf( ) in C:-
This function is just like scanf() function, but instead of standard input, this read data from the file. Using this
function is simple if you already knew about the scanf() function.
This, fscanf() function requires one more parameter then the scanf() function and that parameter is the File
object. Rest of the argument are just as the scanf() function.
Syntax:
fscanf(F, "%s", a);
Here, F is the file object, a is the character array and "%s" string denoting the input value must be string.
This will read a string from the file from the current position of the cursor. You can do same with integers etc.
4. stdlib.h library functions in C:-
LIST OF INBUILT C FUNCTIONS IN STDLIB.H FILE:
Function Description
malloc()
This function is used to allocate space in memory during the execution ofthe
program.
calloc()
This function is also like malloc () function. But calloc () initializes the
allocated
memory to zero. But, malloc() doesn’t
realloc()
This function modifies the allocated memory size by malloc () and calloc ()
functions to new size
free()
This function frees the allocated memory by malloc (), calloc (), realloc ()
functions
and returns the memory to the system.
abs()
This function returns the absolute value ofan integer . The absolute value ofa
number is always positive. Only integer values are supported in C.
14. div() This function performs division operation
abort() It terminates the C program
exit() This function terminates the programand does not return any value
system() This function is used to execute commands outside the C program.
atoi() Converts string to int
atol() Converts string to long
atof() Converts string to float
strtod() Converts string to double
strtol() Converts string to long
getenv() This function gets the current value ofthe environment variable
setenv() This function sets the value for environment variable
putenv() This function modifies the value for environment variable
perror()
This function displays most recent error that happened during library function
call.
rand() This function returns the randominteger numbers
delay() This function Suspends the execution ofthe programfor particular time
a) atof():-
Converts the string str to a decimal. If there are any white-space characters at the beginning of the string str then
they are skipped. For example, in the string " 123", white-space characters present at the beginning of the
string will be skipped and the conversion will start from the character '1'. The conversion stops if there is a
character which is not a number. For example, in the string "123a12", the conversion will stop after converting
"123" as soon as it encounters the non-numeric character 'a'.
If the string str doesn't form any valid floating-point number then 0.0 is returned.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char str[10] = "3.14";
printf("The value is %fn",atof(str));
return 0;
15. }
Output
The value is 3.140000
b) atoi( ) in C:-
Converts the string str to an integer. If there are any white-space characters at the beginning of the string str then
they are skipped. For example, in the string " 123", white-space characters present at the beginning of the string
will be skipped and the conversion will start from the character '1'. The conversion stops if there is a character
which is not a number. For example, int the string, "123a12" the conversion will stop after converting "123" as
soon as it encounters the non-numeric character 'a'.
If the string str doesn't form any valid integer number then 0 is returned.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char str[10] = "55";
printf("The value is %dn",atoi(str));
return 0;
}
Output
The value is 55
5. process.h library functions in C:-
process.h’ is a header file which includes macros and declarations. These are especially u sed during work with thread
and processes. There is no standard for process.h functions. They depend on compiler whichwe use.
1. Some of the standard member functions of process.h header files are,
2. __________________________________________________________
3. Function Name Description
4. execle - It loads & executes new child process by
5. placing it in memory previously occupied by
6. the parent process.
7. spawnv - Parameters are passed as an array of pointers.
8. It loads & executes new child process.
9. getpid - It returns the process identifier.
10. execlp - It loads & executes a new child process by
11. placing it in memory previously occupied by
12. the parent process.
EXAMPLE:-
1. /* Program to demonstrate process.h header file working.
2. Creation Date : 06 Nov 2010 07:43:10 PM
3. Author : www.technoexam.com [Technowell, Sangli] */
4.
5. #include <stdio.h>
6. #include <conio.h>
7. #include <process.h>
8.
9. int main(void)
10. {
16. 11. clrscr();
12. // under DOS PSP segment
13. printf("nt Program's process identification");
14. printf(" number is : %X",getpid());
15. getch();
16. return 0;
17. }
Ou tput :
1. Program's process identification number (PID) number is : 8E01_
7. Conio.h header file in C:-
LIST OF INBUILT C FUNCTIONS IN CONIO.H FILE:
Functions Description
clrscr() This function is used to clear the output screen.
getch() It reads character from keyboard
getche() It reads character from keyboard and echoes to o/p screen
textcolor() This function is used to change the text color
textbackground() This function is used to change text background
List of solved programs of 'conio.h' header file
1. clrscr() and delline() functions of conio.h in C.
In this article, we are going to learn about the two very useful functions clrscr() and delline() of conio.h header file
in C programming language.
2. getch() and getche() functions of conio.h in C.
In this article, we are going to learn about the pre-defined functions getch() and getche() of conio.h header file
and use them with the help of their examples.
3. gotoxy() and kbhit() functions of conio.h in C.
In this article, we are going to learn about the two rare pre-defined functions (gotoxy() and kbhit()) of conio.h
header file.
4. textcolor() and textbackground() functions of conio.h in C.
In this article, we are going to learn about the textbackground() and textcolor() functions of conio.h header file
and use them to change colors.
5. wherex()and wherey() functions of conio.h in C.
In this article, we are going to learn about the use of wherex() and wherey() functions of conio.h header file for
getting the position of cursor.
Some of these functions are given as below:-
1. Clrscr( ) and delline( ):
#include <conio.h>
int main()
{
// message on screen
printf("To clear the screen press any key from keyboard.");
getch();
17. // to clear screen
clrscr();
// message after clearing the screen
printf("The previous screen is now cleared.n");
printf("To get exit from the code just press any key.");
getch();
return 0;
}
Output
conio.h - delline() function Example in C:- deletes a line:-
When you are working on the console, you should always keep it clean and sober. The first thing we should do is
to clear the whole console when the program starts at the beginning. To accomplish this task all you need to do
is use clrscr() function at starting.
What clrscr() function does? It removes everything from the console and we get a clean console window ready
to use.
When you do not want to clear the whole screen? May be, we only want to delete a single line or multiple lines.
The Function delline() delete the current cursor line from the console. (Check the example given below for more
clarification).
We can say delline() is an abbreviation for Delete Line. This can be very useful when you want to bring
something up, because all you will need to do is raise your cursor up and delete those lines. Which in turn brings
up the matter up.
#include <stdio.h>
//to use delline() function
#include <conio.h>
int main()
{
// single line message
printf("To delete this line just press any key from keyboard.");
getch();
// calling function
delline();
// print the message after the code run successfully
printf("The line is deleted now.");
18. getch();
return 0;
}
Ouput
2. getch() function is used, to take a single byte of input from a user. We don’t care about the
input anyway, all we want is, that user should get some time to read or the window should wait
till the user wants it to be there. When the user presses any key on the keyboard the function
ends and the program ends.
The getche() function is almost similar but the little wider as it take any character or alphanumeric input. But the
different thing it does, is it prints out the input that the user has given.
Program already done in class.
4. textcolor() and textbackground() functions of conio.h in C.
In this article, we are going to learn about the textbackground() and textcolor() functions of conio.h
header file and use them to change colors.
These are very helpful functions to change the color or the background of the text. How this works is first we got
to set the color of the text and then print something on the console?
To use the textbackground()function all you need to do is before printing any text call this function with a
parameter defining the color in capital letters. That will be enough to change the background color of the text.
Now, if you want your text to blink then while calling the textcolor() function pass the color and also
say BLINK. This will like this: textcolor(BLUE+BLINK).
These are the function of the conio.h fileand must be included in your program.
conio.h - textbackground() function Example in C
#include <stdio.h>
//to use 'textbackground'
#include <conio.h>
int main()
{
// setting the color of background
textbackground(GREEN);
19. // message
cprintf("Change the background color to green");
getch();
return 0;
}
Output
conio.h - textcolor() function Example in C
#include <stdio.h>
//to use 'textcolor()'
#include <conio.h>
int main()
{
// set the color of text
textcolor(BLUE);
// message
cprintf("Color of text is BLUEnn");
// set blinking color
textcolor(GREEN+BLINK);
// message
cprintf("nThis is BLINKING text");
getch();
return 0;
}
Output
1.