Notes on Library Functions | Grade 12 > Computer Science > Programming in C | KULLABS.COM

• Note
• Things to remember

INTRODUCTION

In C language, a number of library functions are used. They carry out various commonly used operations or calculations. Some functions return a data item to their access point and others indicate whether a condition is true or false. Some other functions carry out special operations on data items.

Library functions that are functionally similar and usually grouped together as object programs (compiled) in separate library files. These library files are supplied as a part of each C compiler.

A library function is accessed simply by writing the function name, followed by a list of arguments that represent information being passed to the function.

The arguments must be enclosed in parentheses and separated by commas. These arguments can be constants, variable names, etc. Some important functions are given below.

List of Library Functions

 Function Type Purpose abs(i) ceil(d) cos(d) cosh(d) exp(d) fabs(d) floor(d) fmod(d1,d2) getchar() log(d) pow(d1,d2) printf(…) putchar(c) rand() sin(d) sqrt(d) srand(u) scanf(…) tan(d) toascii(c) tolower(c) toupper(c) int double double double double double double double int double double int int int double double void int double int int int Return the absolute value of i. Round up the next integer value Return the cosine of d. Return the hyperbolic cosine of d. Raise e to the power d. Return the absolute value of d. Round down the next integer value Return the remainder Enter a char from the standard input device Return the natural logarithm of d. Return d1 raised to the power d2. Send data items to the standard output device Send a character to the standard output device Return a random positive integer Return the sine of d. Return the square root of d. Initialize the random number generator. Enter data items from a standard input device. Return the tangent of d. Convert value of an argument to ASCII. Convert the letter to lowercase. Convert the letter to uppercase.

#### Data Input/output

The most common library functions responsible for input and output operations in C are: getchar (),

Putchar (), scanf (), printf (), gets (), and puts (). The getchar () and putchar () are the functions used to accept a single character from the keyboard and display on the monitor. The scanf () and printf () functions are flexible.

They can accept values, characters, and strings and use suitable format specifier that can be displayed. Knowing the structure of these functions one can write many C programs in a simple form. Let us examine each of them.

The getchar () Function

Single character can be entered into the computer using the C library function getchar (). This is a part of the standard I/O library. It returns a single character from the standard input device (keyboard). The function does not require any arguments but empty parentheses must follow the keyword getchar.

Syntax:

Character variable = getchar ();

While using getchar () with file handling functions, if an end-of-file condition is encountered with reading a character with the getchar function, the value of the symbolic constant EOF will automatically be returned.

# Write a program to input a character form the keyboard and print.

 #include main() { char c; printf(“\n Enter a character: “); c = getchar (); printf(“\n The character input is :”); putchar (c); }

The putchar () Function

The putchar () function is used to display single characters. It is a part of standard C I/O library. It transmits a single character to the standard output device (monitor). The character being transmitted will normally be represented as a character-type variable. It must be expressed as an argument to the function, enclosed in parentheses, following the word put char.

Syntax:

Putchar ( character variable);

# Write a program to test and verify putchar () function.

 #include main() { char c; printf (“\n Enter a character of your own:”); c = getchar (); printf(“\n The character that you have entered is:”); putchar (c); }

While executing the program, if you enter a word, the first character is only displayed. This is due to the fact that single character is passed to the character-type variable c.

String Input/output Functions

The above discussed functions can print the characters only. You can use a scanf() function to read a line of text but during printing, the characters before the white space are only printed. Let us examine the following program.

# Write a program to accept a string of 80 characters long and print it.

 #include #include main() { char string [80]; printf(“\n Enter a string:”); scanf(“%s”,&string); printf(“\n The string accepted is %s”, string); }

While executing the above program if you enter a word of 80 characters long without any spaces, it will be printed. But if you supply space before at any place, only the first word will be printed.

The gets() and puts() Function

The gets() and puts() functions are used to input complete sentences with comma, semicolon, etc. and that can be printed on the screen.

Example

 #include main() { char ch [80]; gets (ch); …….. ……… }

The above segment of the program has defined ch variable with 80 characters as an array. The gets() function contains the same variable in the parentheses.

Here, puts() is an output function which lets to display the output on a suitable device. This function is generally used to display the output that has been accepted from the keyboard.

Write a program in C to accept a sentence with comma and semicolon and print on the screen.

 #include void main () { char c[ 80]; printf(“\n Enter a sentence:”); gets (c); printf(“\n The entered sentences is:\n”); puts (c); }

We can use an alternative form of entering a line of text using the following convention.

# Write a program to input a line of text without using gets () and puts () function.

 #include #include main () { char line [80]; printf(“\n Enter a line of text:”); scanf(“% [^\n”, line); clrscr (); printf(“\n You have entered the line of text as follows”); printf(“\n\n %s”, line); }

General Input: The scanf() Function

The C library function scanf() is used for entering the data from the standard input device such as a keyboard. This function can be used to enter any combination of numerical values, single characters, and strings.

Syntax:

scanf(“control string”, arg1, arg2, ………, argn);

Here, the control string (also called format string) includes the group of characters with one group of characters for each input data. Each character group begins with a % sign. A single character group consists of the % sign followed by a conversion character that indicates the data type. Different types of data items and conversion character are given in the below table.

 Conversion Character Meaning %c %d %e %f %g %h %i %o %s %u %x [……..] Data item is a single character Data item is a decimal integer Data item is a floating point value (exponent, scientific notation) Data item is a floating point value Data item is a floating point value Data item is a short integer Data item is a decimal, hexadecimal or octal number Data item is an octal integer Data item is a string followed by a whitespace character (the null character \0 will automatically be added at the end) Data item is unsigned decimal integer Data item is a hexadecimal integer Data item is a string which may include whitespace characters.

Example: Test for input of a decimal number.

 #include main () { int i; printf(“\n Enter an integer:”); scanf(“%d”,&i); printf(“\n The input decimal value %d”, i); }

General Output: The printf () Function

The output data can be given to the monitor from the computer using the printf() function. This function is useful for printing any numerical values, single characters, and strings. It is similar to the scanf() function but it is used to display the data on the suitable standard output device. The printf() function moves the data from computer’s memory to the output device.

Syntax

Print(“control string”, arg1, arg2, ……., argn);

The control string is also known as a format string. The format string contains text, commas and a format specifier such as %c, %d, %f, etc. which receive the data from the arguments respectively.

Example: Test of a format string and format specifier.

 #include main () { int i; printf(“\n Enter an integer:”); scanf(“%d”,&i); printf(“\n The input decimal value %d”,i); }

Let us examine the line:

printf(“\n The input decimal value %d”,i);

This line contains %d within the double quotes which is format specifier and receives the value from i.

References:

Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. -154-161.

Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd

1. A library function is accessed simply by writing the function name, followed by a list of arguments that represent information being passed to the function.
2. The most common library functions responsible for input and output operations in C are: getchar (),Putchar (), scanf (), printf (), gets (), and puts ().
3. Single character can be entered into the computer using the C library function getchar (). This is a part of the standard I/O library.
4. The putchar () function is used to display single characters. It is a part of standard C I/O library. It transmits a single character to standard output device (monitor).
5. The gets() and puts() functions are used to input complete sentences with comma, semicolon, etc. and that can be printed on the screen.
6. The control string is also known as a format string. The format string contains text, commas and a format specifier such as %c, %d, %f, etc. which receive the data from the arguments respectively.
.

0%

## ASK ANY QUESTION ON Library Functions

No discussion on this note yet. Be first to comment on this note