UNIT – 4
1. Standard Mathematical Functions (math.h)
What is math.h?
math.h is a standard header file in C language that provides various mathematical functions
like power, square root, trigonometry, logarithm, etc. These functions help perform complex
calculations easily and efficiently.
How to use:
c
CopyEdit
#include <math.h>
If you're using GCC:
nginx
CopyEdit
gcc file.c -lm
Common Functions in math.h:
Function Description Syntax Example Output
sqrt(x) Square root sqrt(double x) sqrt(16) 4
pow(x, y) x to power y pow(x, y) pow(2,3) 8
abs(x) Absolute value (int) abs(int x) abs(-10) 10
fabs(x) Absolute value (float) fabs(double x) fabs(-2.3) 2.3
ceil(x) Round up ceil(double x) ceil(4.3) 5
floor(x) Round down floor(4.7) 4
log(x) Natural log (base e) log(10) 2.302
log10(x) Log base 10 log10(1000) 3
exp(x) e raised to x exp(2) 7.389
sin(x) Sine (x in radians) sin(π/2) 1
Function Description Syntax Example Output
cos(x) Cosine cos(0) 1
tan(x) Tangent tan(π/4) 1
Example Program:
c
CopyEdit
#include <stdio.h>
#include <math.h>
int main() {
double x = 9.0;
printf("Square root of %.2f = %.2f\n", x, sqrt(x));
printf("Power: 2^3 = %.2f\n", pow(2, 3));
return 0;
}
2. Formatted and Unformatted I/O Functions
Formatted I/O
printf():
Used to display formatted output.
c
CopyEdit
int a = 5;
printf("Value = %d", a);
scanf():
Used to take formatted input.
c
CopyEdit
int x;
scanf("%d", &x);
Format Specifiers:
Specifier Data Type
%d Integer
%f Float
%c Character
%s String
%lf Double
Unformatted I/O
These handle only characters and strings, no formatting.
getchar() / putchar():
c
CopyEdit
char c;
c = getchar();
putchar(c);
gets() / puts():
c
CopyEdit
char str[50];
gets(str);
puts(str);
Note: gets() is deprecated, use fgets() instead.
3. String Manipulation Functions
Available in string.h header.
Common Functions:
Function Description Example
strlen() Finds length strlen("BCA") = 3
strcpy() Copy string strcpy(s1, s2)
strcat() Concatenate strings strcat(s1, s2)
strcmp() Compare two strings strcmp(s1, s2)
strupr() Convert to uppercase strupr(str)
strlwr() Convert to lowercase strlwr(str)
Example:
c
CopyEdit
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello", str2[] = "World";
strcat(str1, str2);
puts(str1); // Output: HelloWorld
return 0;
}
4. User-Defined Functions
What is a function?
A function is a block of code that performs a specific task. It improves modularity,
readability, and reusability.
Types of Functions:
• Library Functions → Built-in, like printf(), sqrt()
• User-defined Functions → Created by programmer
Syntax:
c
CopyEdit
return_type function_name(parameter list) {
// function body
}
Function Prototype:
c
CopyEdit
int sum(int, int);
Local vs Global Variables:
• Local: Inside function, valid only in that function
• Global: Declared outside, accessible everywhere
Parameter Passing:
• Call by Value: Pass copy → Original unaffected
• Call by Reference: Pass address → Changes reflect
Recursion:
A function calling itself repeatedly with a base condition.
c
CopyEdit
int factorial(int n) {
if(n==0) return 1;
else return n * factorial(n-1);
}
5. Arrays in C
What is an Array?
An array is a collection of same type of elements, stored in contiguous memory locations.
c
CopyEdit
int a[5] = {1,2,3,4,5};
Accessing:
c
CopyEdit
a[0] = 10;
printf("%d", a[0]);
Types of Arrays:
1. 1-D Array – List
2. 2-D Array – Matrix
3. Multidimensional Array – Table/3D etc.
Array Initialization:
c
CopyEdit
int a[5] = {10, 20, 30, 40, 50};
Passing Array to Function:
c
CopyEdit
void print(int arr[], int size) {
for(int i=0; i<size; i++)
printf("%d ", arr[i]);
}
6. Strings in C
Strings are arrays of characters ending with \0.
c
CopyEdit
char name[10] = "Anu";
Input/Output:
c
CopyEdit
char s[20];
gets(s); // input
puts(s); // output
7. Pointers in C
What is a Pointer?
A pointer is a variable that stores the memory address of another variable.
c
CopyEdit
int x = 10;
int *ptr = &x;
Dereferencing:
c
CopyEdit
printf("%d", *ptr); // 10
Use Cases:
• Dynamic memory allocation
• Array traversal
• Functions argument by reference
• Linked Lists and Data Structures
Array with Pointers:
c
CopyEdit
int arr[] = {1, 2, 3};
int *p = arr;
for(int i=0; i<3; i++)
printf("%d ", *(p+i));
Pointers with Functions:
c
CopyEdit
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}