0% found this document useful (0 votes)
6 views58 pages

Module -3

The document provides an overview of function prototypes, definitions, and calls in C programming, along with string and array initialization techniques. It also discusses various C string functions, math functions, recursion, pointers, and their arithmetic operations. Additionally, it highlights the advantages and disadvantages of pointers and includes examples of programs that utilize these concepts.

Uploaded by

sunitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views58 pages

Module -3

The document provides an overview of function prototypes, definitions, and calls in C programming, along with string and array initialization techniques. It also discusses various C string functions, math functions, recursion, pointers, and their arithmetic operations. Additionally, it highlights the advantages and disadvantages of pointers and includes examples of programs that utilize these concepts.

Uploaded by

sunitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

Module -3

Function Prototype

• A function prototype declares a function


before its actual definition.
• It helps the compiler understand function
return type and parameters.
• Syntax:
• return_type function_name(parameter_list);
• Ex: int add(int ,int);
Function Definition

• A function definition contains the actual


implementation of the function.
• It consists of:
– Return type : any basic data type and user defined type
– Function name
– Parameters (if any) : to be declared along with data type
– Function body
• Example:
• int multiply(int a, int b) { return a * b;}
Function Call

• A function is called within main() or another


function.
• Call format:
• function_name(arguments);
• int result = multiply(4, 2);
Example
#include <stdio.h>
// Function prototype
int add(int, int);

void main()
{
int result = add(5, 3);
// Function call
printf("Sum: %d\n", result);
return 0;
}
// Function definitionin
add(int a, int b)
{
return a + b;
}
String 1D array initialization
• Assigning a String Literal without Size
– char str[] = “jnnce”
• Assigning a String Literal with Size
– Char str[30]=“JNNCESHIMOGA”
• Assigning Character by Character with Size
– Char str[10] = {‘j’,’n’,’n’,’c’,’e’}
• Assigning Character by Character with out
Size
• Char str[] = {‘j’,’n’,’n’,’c’,’e’}
2D array initialization
#include <stdio.h>

int main() {
char arr[3][10] = {
“hjnnce",
“shimoga",
“nes"
};

for (int i = 0; i < 3; i++) {


printf("%s\n", arr[i]);
}

return 0;
}
C string functions
strlen(const char *str)
: Returns the length of the string
Excluding the null character
strcpy
• strcpy(char *dest, const char *src)
• Copies the string src to dest
strcat
• strcat(char *dest, const char *src)
• Appends the string src to the end of dest
strcmp
• strcmp(const char *str1, const char *str2)
• Compares two strings
• Return 0 if equal
• Return >=1 if str1>str2
• Returns <=-1 if str2>str1
strchr
Strchr: Finds the first occurrence of character c in str
Syntax : strchr(const char *str, int c)

Example:
const char str[] = "Hello, jnnce";
char ch = ‘n';
char *ptr = strchr(str, ch);

}
strstr
strstr(const char *text, const char *patt)
Finds the first occurrence of the substring patt in text.
Example
char str[] = “welcome to jnnce";
char substr[] = “come’; / char substr[]=“sunitha”
char *result;
result = strstr(str, substr);
if (result != NULL)
{
printf("Substring found: '%s'\n", result);
}
else
{
printf("Substring not found.\n");
}
strtok()

• The strtok() function is used to split a string into tokens


based on specified delimiters
int main() {
char s[] = "Hello, sunitha, GP!";
char *t = strtok(s, ", ");
while (t != NULL)
{ printf("%s\n", t);
t = strtok(NULL, ", ");
} return 0;
}
Math functions
• In C programming, math functions are
provided by the standard library math.h
• Basic Arithmetic Functions:
• Trigonometric Functions
• Inverse Trigonometric Functions:
• Exponential and Logarithmic Functions:
• Hyperbolic Functions
sqrt
• double sqrt(double x): Computes the square
root of x
• double num = 9.0
• printf("Square root of %.2f is %.2f\n", num,
sqrt(num));
ceil
• double ceil (double x)
• The C library function double ceil (double x)
returns the smallest integer value greater than
or equal to x.
• val1 = 1.6;
• printf("value1 = %.1lf\n", ceil(val1)); returns 2
floor
• double floor(double x)
• The C library function double floor(double x)
returns the largest integer value less than or
equal to x.
• val1 = 1.6;
• printf("value1 = %.1lf\n", ceil(val1)); returns 1
fabs
• double fabs(double x)
• The C library function double fabs(double x)
returns the absolute value of x.
• b = -344;
• printf("The absolute value of %d is %lf\n", b,
fabs(b));
• Returns 344
fmod
• double fmod(double x, double y)
• The C library function double fmod(double x,
double y) returns the remainder of x divided by y.
• a = 8.2;
• b = 5.7;
• printf("Remainder of %f / %f is %lf\n", a, b,
• fmod(a, b));
• Remainder of 8.200000 / 5.700000 is 2.500000
pow
double pow(double x, double y)
The C library function double pow(double x,
double y) returns x raised to the power of y

printf("Value 8.0 ^ 3 = %lf\n", pow(8.0, 3));


Value 8.0 ^ 3 = 512.000000
exp
double exp(double x)
The C library function double exp(double x)
returns the value of e raised to the xth power.
double x = 0;
printf("The exponential value of %lf is %lf\n", x,
exp(x));
• The exponential value of 0.000000 is 1.000000
cos/sin
double cos(double x)
The C library function double cos(double x) returns
the cosine of a radian angle x.
double angle = 45.0;
double radians = angle * (3.14/ 180.0);
printf("Sine of %.2f degrees is %.2f\n", angle, sin(radians));
printf("Cosine of %.2f degrees is %.2f\n", angle,
cos(radians));
Sine of 45.00 degrees is 0.71
Cosine of 45.00 degrees is 0.71
Function arguments in C – Call by value and Call by reference

• In C programming value can pass to a function in


two ways.
1. Call by value
2. Call by reference
Call by value is the
default mechanism
to pass arguments to
a function
In Call by value Changes made to the formal
parameters does not affect the actual parameter
Call by reference

• It uses pointers to pass reference of an actual


parameter to formal parameter
• Changes made to the formal parameter
immediately reflects to actual parameter
Call by reference
Recursion
• A function calls itself to solve a problem is
called recursion
• Recursion should have
– Base Case: The base case is crucial to prevent
infinite recursion
– Recursive Case: The function calls itself with n - 1
until it reaches the base case
• Termination: There should be a base case to
terminate the recursion
Example
Example2
Recursive binary Search
pointers
• In C, pointers are variables that store the
memory address of another variable.
types
• main types of pointers:
• Integer Pointers: Point to integer values.
• Array Pointers: Reference the initial element of an array.
• Structure Pointers: Point to user-defined data types
(structures).
• Function Pointers: Allow calling functions indirectly by
storing their addresses.
• Double Pointers: Pointers that point to another pointer.
• NULL Pointers: Pointers that do not point to any valid
memory location.
Pointers Arithmetic

Increment/Decrement of a
Pointer
Addition of integer to a
pointer
Subtraction of integer to a
pointer
Subtracting two pointers of
the same type
Comparison of pointers
Increment / decrement
Addition /subtraction of integer to a pointer
Pointer comparison
Pointers and arrays
• The memory address of the first element is
the same as the name of the array
Pointers and arrays
Array of pointers
• a pointer array is a homogeneous collection of
indexed pointer variables that are references
to a memory location.
• pointer_type *array_name [array_size];
• pointer_type: Type of data the pointer is
pointing to.
• array_name: Name of the array of pointers.
• array_size: Size of the array of pointers.
example
pointer
• A pointer is a variable that stores
the memory address of another
variable.
• 2 important operators used while
handling pointers are
– Dereferencing operator(*) : Used to
access the value
– Address operator(&) : used to access
the address of a variable
Pointer description
pointer
Advantages Dis-advantages
1. Pointers are used for dynamic 1. Uninitialized pointers
memory allocation and
might cause a
deallocation
segmentation fault.
2. Pointers are useful for
accessing memory 2. Memory corruption can
locations occur
3. Pointers are used to form 3. Pointers are a little bit
complex data structures such
complex to understand.
as linked lists, graphs, trees,
etc.
4. Pointers reduce the length of
the program and its execution
time as well
Declaring and initializing pointers
Example call by value
Call by Reference
Finding the sum and avg of array elements
using pointer
Programs
1. finding sum upto n
2. Factorial of a number
3. Checking number is odd or even
4. Finding the area ( 3.14*r*r) or circumference of a circle
(2 * 3.14 * r)
5. Adding two numbers
6. Checking given number is prime or not
7. Finding sum and average of array elements using
pointers
8. Swapping of two numbers using pointers

You might also like