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

PROGRAMMING in C UNIT-4 notes

The document provides an overview of C99 extensions and standard library functions in the C programming language, detailing various header files and their functionalities. It includes explanations of common operations such as input/output, string manipulation, mathematical calculations, and sorting algorithms like bubble sort, insertion sort, and selection sort. Additionally, it covers algorithms for finding factorials, Fibonacci series, linear search, and binary search.

Uploaded by

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

PROGRAMMING in C UNIT-4 notes

The document provides an overview of C99 extensions and standard library functions in the C programming language, detailing various header files and their functionalities. It includes explanations of common operations such as input/output, string manipulation, mathematical calculations, and sorting algorithms like bubble sort, insertion sort, and selection sort. Additionally, it covers algorithms for finding factorials, Fibonacci series, linear search, and binary search.

Uploaded by

saksham2018 Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

CSE, MAIT

 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.
CSE, MAIT
HEADER FILE MEANING ELUCIDATION

Standard input-output
<stdio.h> Used to perform input and output operations like scanf() and printf().
header

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. CSE, MAIT
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.

CSE, MAIT
 <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.

CSE, MAIT
 <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.
CSE, MAIT
 <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.

CSE, MAIT
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.

CSE, MAIT
 <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().

CSE, MAIT
 <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().

CSE, MAIT
 <unistd.h>
 The <unistd.h> header defines miscellaneous symbolic
constants and types, and declares miscellaneous
functions.

CSE, MAIT
 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

CSE, MAIT
 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 CSE, MAIT
 LINEAR SEARCH:
 Linear search is a very simple search algorithm. In this type of
search, a sequential search is made over all items one by one.
Every item is checked and if a match is found then that particular
item is returned, otherwise the search continues till the end of the
data collection.

 Linear Search ( Array A, Value x)


 Step 1: Set i to 1
 Step 2: if i > n then go to step 7
 Step 3: if A[i] = x then go to step 6
 Step 4: Set i to i + 1
 Step 5: Go to Step 2
 Step 6: Print Element x Found at index i and go to step 8
 Step 7: Print element not found
 Step 8: Exit
CSE, MAIT
CSE, MAIT
 BINARY SEARCH
 Search a sorted array by repeatedly dividing the search interval in
half. Begin with an interval covering the whole array. If the value
of the search key is less than the item in the middle of the
interval, narrow the interval to the lower half. Otherwise, narrow
it to the upper half. Repeatedly check until the value is found or
the interval is empty.

 ALGORITHM:
 Step 1 : Find the middle element of array. using ,
 middle = initial_value + end_value / 2 ;
 Step 2 : If middle = element, return ‘element found’ and index.
 Step 3 : if middle > element, call the function with end_value =
middle - 1 .
 Step 4 : if middle < element, call the function with start_value =
middle + 1 .
 Step 5 : exit.

CSE, MAIT
CSE, MAIT
 BUBBLE SORT
 Bubble Sort in C is a sorting algorithm where we repeatedly
iterate through the array and swap adjacent elements that are
unordered. We repeat this until the array is sorted.
 As an example, for the array mentioned above - [5, 1, 4, 2, 3] we
can see that 5 should not be on the left of 1 and so, we swap them
to get: [1, 5, 4, 2, 3]. Next, we see that 5 should again not be on
the left of 4. We swap 5 and 4 to get [1, 4, 5, 2, 3]. We repeat this
for 5 and 2 and subsequently for 5 and 3 to get [1, 4, 2, 3, 5].
 Step 1: Repeat Steps 2 and 3 for i=1 to 10
 Step 2: Set j=1
 Step 3: Repeat while j<=n
 (A) if a[i] < a[j]
 Then interchange a[i] and a[j]
 [End of if]
 (B) Set j = j+1
 [End of Inner Loop]
 [End of Step 1 Outer Loop]
 Step 4: Exit
CSE, MAIT
CSE, MAIT
 INSERTION SORT
 Insertion Sort is a sorting algorithm where the array is sorted by
taking one element at a time. The principle behind insertion sort
is to take one element, iterate through the sorted array & find its
correct position in the sorted array. Insertion Sort works in a
similar manner as we arrange a deck of cards.

 Step 1 − If the element is the first one, it is already sorted.


 Step 2 – Move to next element
 Step 3 − Compare the current element with all elements in the
sorted array
 Step 4 – If the element in the sorted array is smaller than the
current element, iterate to the next element. Otherwise, shift all
the greater element in the array by one position towards the right
 Step 5 − Insert the value at the correct position
 Step 6 − Repeat until the complete list is sorted

CSE, MAIT
CSE, MAIT
 SELECTION SORT
 Selection sort is another algorithm that is used for sorting.
This sorting algorithm iterates through the array and finds
the smallest number in the array and swaps it with the first
element if it is smaller than the first element. Next, it goes
on to the second element and so on until all elements are
sorted.

 Step 1 − Set min to the first location


 Step 2 − Search the minimum element in the array
 Step 3 – swap the first location with the minimum value in
the array
 Step 4 – assign the second element as min.
 Step 5 − Repeat the process until we get a sorted array.

CSE, MAIT
CSE, MAIT
 FIND THE SQUARE OF A NUMBER
 Step 1 → Define value n to find square root of
 Step 2 → Define variable i and set it to 1 (For integer
part)
 Step 3 → Define variable p and set it to 0.00001 (For
fraction part)
 Step 4 → While i*i is less than n, increment i
 Step 5 → Step 4 should produce the integer part so far
 Step 6 → While i*i is less than n, add p to i
 Step 7 → Now i has the square root value of n

CSE, MAIT
 ARRAY ORDER REVERSAL
 In this algorithm, subarrays are created and reversed to
perform the rotation of the array. Subarrays are created,
rotated individually and then joined together and reversed
back to get the rotated array.Input : array arr[] , positions
that are needed to be rotated r , length of array n.

 Step 1: Split the array into two sub arrays of 0 - (d-1) and d
- (n-1) size, a1 [d] and a2[n-d];
 Step 2: Reverse both arrays using the reverse method.
 Step 3: Join a1 and a2 back to get an array of original size.
 Step 4: Reverse this joined array to get the rotated array.
 Step 5: Print the array using the standard output method.

CSE, MAIT
 REVERSAL OF A STRING
 Step 1: Define a string and another empty string to store the reversed string.
 Step 2: Now, iterate the string from last and store a character from back into variable revString.
 Step 3: At the end of the loop, revString will hold the reverse of the string.

 Example:
 #include <stdio.h>
 #include <string.h>
 int main()
 { char string[] = "Dream big"; //Stores the reverse of given string
 char reversedStr[strlen(string)];
 int j = 0; //Iterate through the string from last and add each character to variable reversedStr
 for(int i = strlen(string)-1; i >= 0; i--){
 reversedStr[j++] = string[i];
 }
 reversedStr[j] = '\0';
 printf("Original string: %s", string); //Displays the reverse of given string
 printf("\nReverse of given string: %s", reversedStr);
 return 0;
 }

CSE, MAIT

You might also like