Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

LECTURE-22

PROGRAMMING IN ‘C’
C STANDARD LIBRARIES AND BASIC
ALGORITHMS
 C99 EXTENSIONS
 C99 (previously known as C9X) is an informal name for ISO/IEC
9899:1999, a past version of the C programming language
standard.
 STANDARD LIBRARY FUNCTIONS:  
 Standard Library Functions are basically the inbuilt functions in the
C compiler that makes things easy for the programmer.
 As we have already discussed, every C program has at least one
function, that is, the main() function. The main() function is also a
standard library function in C since it is inbuilt and conveys a
specific meaning to the C compiler.
 In order to access the standard library functions in C, certain header
files need to be included before writing the body of the program.
HEADER FILE MEANING ELUCIDATION

Standard input-output Used to perform input and output operations like scanf() and


<stdio.h>
header printf().

Used to perform string manipulation operations like strlen and


<string.h> String header
strcpy.

Used to perform console input and console output operations like


Console input-output
<conio.h> clrscr() to clear the screen and getch() to get the character from the
header
keyboard.

Standard library Used to perform standard utility functions like dynamic memory
<stdlib.h>
header allocation using functions such as malloc() and calloc().

Used to perform mathematical operations like sqrt() and pow() to


<math.h> Math header
obtain the square root and the power of a number respectively.

Used to perform character type functions like isaplha() and isdigit()


<ctype.h> Character type header to find whether the given character is an alphabet or a digit.
respectively.

Used to perform functions related to date and time like setdate() and
<time.h> Time header getdate() to modify the system date and get the CPU time
respectively.

Used in program assertion functions like assert() to get an integer


<assert.h> Assertion header data type as a parameter which prints stderr only if the parameter
passed is 0.
HEADER FILE MEANING ELUCIDATION

Used to perform localization functions like setlocale() and


<locale.h> Localization header localeconv() to set locale and get locale conventions
respectively.

Used to perform signal handling functions like signal() and


<signal.h> Signal header raise() to install signal handler and to raise the signal in the
program respectively.

<setjmp.h> Jump header Used to perform jump functions.

Used to perform standard argument functions like va_start


Standard argument and va_arg() to indicate the start of the variable-length
<stdarg.h>
header argument list and to fetch the arguments from the variable-
length argument list in the program respectively.
 <stdio.h>
 This is the basic header file used in almost every program written in the C language.
 It stands for standard input and standard output used to perform input-output functions,
some of which are: 
 printf()– Used to display output on the screen.
 scanf()– To take input from the user.
 getchar()– To return characters on the screen.
 putchar()– To display output as a single character on the screen.
 fgets()– To take a line as an input.
 puts()– To display a line as an output.
 fopen()– To open a file.
 fclose()– To close a file.
 <string.h>
 We have already discussed the various string manipulation functions in C in detail. 
 <stdlib.h>
 Functions such as malloc(), calloc(), realloc() and free() can be used while dealing with
dynamic memory allocation of variables. 
 It should be clear that these functions are used for dynamic memory allocation of
variables, that is in contrast to arrays that allocate memory in a static (fixed) manner.
 <math.h>
 The math header is of great significance as it is used to perform various mathematical operations such as:
 sqrt() – This function is used to find the square root of a number
 pow() – This function is used to find the power raised to that number.
 fabs() – This function is used to find the absolute value of a number.
 log() – This function is used to find the logarithm of a number.
 sin() – This function is used to find the sine value of a number.
 cos() – This function is used to find the cosine value of a number.
 tan() – This function is used to find the tangent value of a number. 
 <ctype.h>
 This function is popularly used when it comes to character handling. 
 Some of the functions associated with <ctype.h> are: 
 isalpha() – Used to check if the character is an alphabet or not.
 isdigit() – Used to check if the character is a digit or not.
 isalnum() – Used to check if the character is alphanumeric or not.
 isupper() – Used to check if the character is in uppercase or not
 islower() – Used to check if the character is in lowercase or not.
 toupper() – Used to convert the character into uppercase.
 tolower() – Used to convert the character into lowercase.
 iscntrl() – Used to check if the character is a control character or not.
 isgraph() – Used to check if the character is a graphic character or not.
 isprint() – Used to check if the character is a printable character or not
 ispunct() – Used to check if the character is a punctuation mark or not.
 isspace() – Used to check if the character is a white-space character or not.
 <conio.h>
 It is used to perform console input and console output operations like
clrscr() to clear the screen and getch() to get the character from the
keyboard. 
 Note: The header file <conio.h> is not supported in Linux. It works
fairly well on Microsoft Windows.
 <assert.h>
 The assert.h header file of the C Standard Library provides a macro
called assert which can be used to verify assumptions made by the
program and print a diagnostic message if this assumption is false.
 The defined macro assert refers to another macro NDEBUG which is
not a part of <assert.h>. If NDEBUG is defined as a macro name in the
source file, at the point where <assert.h> is included, the assert macro is
defined as follows −
 #define assert(ignore) ((void)0)
 <time.h>
 The time.h header defines four variable types, two macro and various
functions for manipulating date and time.
Sr.No. Variable & Description

1 size_t
This is the unsigned integral type and is the result of the sizeof keyword.

2 clock_t
This is a type suitable for storing the processor time.

3 time_t is
This is a type suitable for storing the calendar time.

4 struct tm
This is a structure used to hold the time and date.
 <stdarg.h>
 The stdarg.h header defines a variable type va_list and three
macros which can be used to get the arguments in a function
when the number of arguments are not known i.e. variable
number of arguments.
 A function of variable arguments is defined with the ellipsis (,...)
at the end of the parameter list.
 Library Variables
 Following is the variable type defined in the header stdarg.h –

Sr.No. Variable & Description

1 va_list
This is a type suitable for holding information needed by the three
macros va_start(), va_arg() and va_end().
 <setjmp.h>
 The setjmp.h header defines the macro setjmp(), one function
longjmp(), and one variable type jmp_buf, for bypassing the
normal function call and return discipline.
 Library Variables
 Following is the variable type defined in the header setjmp.h –

Sr.No. Variable & Description

1 jmp_buf
This is an array type used for holding information for
macro setjmp() and function longjmp().
 <unistd.h>
 The <unistd.h> header defines miscellaneous symbolic

constants and types, and declares miscellaneous


functions.
BASIC ALGORITHMS:

 FINDING FACTORIAL-
 Factorial of a positive integer n is product of all values from n to
1. For example, the factorial of 3 is (3 * 2 * 1 = 6). 
 Algorithm
 Algorithm of this program is very easy −
START
Step 1 → Take integer variable A
Step 2 → Assign value to the variable
Step 3 → From value A upto 1 multiply each digit and store
Step 4 → the final stored value is factorial of A
STOP
 FIBONACCI SERIES:
 Fibonacci Series generates subsequent number by adding two previous numbers.

Fibonacci series startsfrom two numbers − F0 & F1. The initial values of F0 & F1 can
be taken 0, 1 or 1, 1 respectively. 
Fibonacci series satisfies the following conditions −
Fn = Fn-1 + Fn-2
So a Fibonacci series can look like this −
F8 = 0 1 1 2 3 5 8 13 
or, this −
F8 = 1 1 2 3 5 8 13 21
Algorithm
Algorithm of this program is very easy − 
START
Step 1 → Take integer variable A, B, C
Step 2 → Set A = 0, B = 0
Step 3 → DISPLAY A, B
Step 4 → C = A + B
Step 5 → DISPLAY C
Step 6 → Set A = B, B = C
Step 7 → REPEAT from 4 - 6, for n times
STOP

You might also like