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

Pre Defined Functions in C

The document provides an overview of functions in C programming, detailing their definitions, advantages, and types, including predefined and user-defined functions. It covers various string functions, character functions, and mathematical functions, providing examples and syntax for each. Additionally, it highlights the importance of libraries such as <string.h> and <math.h> for utilizing these functions.

Uploaded by

NIKHIL GUPTA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Pre Defined Functions in C

The document provides an overview of functions in C programming, detailing their definitions, advantages, and types, including predefined and user-defined functions. It covers various string functions, character functions, and mathematical functions, providing examples and syntax for each. Additionally, it highlights the importance of libraries such as <string.h> and <math.h> for utilizing these functions.

Uploaded by

NIKHIL GUPTA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Function

Function is a block of code that has a name and used to perform some
specific task. The function contains the set of programming statements
enclosed by curly bracket { }. A function can be called multiple times, so it
is reusable.

Using the function we can divide a large program into small blocks known
as function.
Function Advantages

 By using functions, we can avoid rewriting same logic/code again and


again in a program.
 We can call C functions any number of times in a program and from any
place in a program.
 We can track a large C program easily when it is divided into multiple
functions.
 Reusability is the main achievement of C functions.
 However, Function calling is always a overhead in a C program.
Function Aspects

There are three aspects of a C function .

Function declaration A function must be declared globally in a c program to tell the


compiler about the function name, function parameters, and return type.

Function call Function can be called from anywhere in the program. The parameter
list must not differ in function calling and function declaration. We must pass the
same number of functions as it is declared in the function declaration.

Function definition It contains the actual statements which are to be executed. It is


the most important aspect to which the control comes when the function is called.
Here, we must notice that only one value can be returned from the function.
Function Types

There are two types of function in C programming

 Predefined Function ( Library Function )


 User Defined Function
Library Function

Standard Library Functions are basically the inbuilt functions in the C


compiler that makes things easy for the programmer.

Library functions are built-in functions that are grouped together and
placed in a common location called library.

Example: stdio.h is a header files that contains some standard functions


such as printf(), scanf(), gets(), puts() etc.
String Function

Strings are an array of characters. We can perform several operation on


string to manipulate the strings. Such as finding length of string,
comparing two string, displaying formatted output etc. using string
function.

All the string functions are available in string.h header file.


String Function strlen()

String function strlen() is used to find the length of given string. It does
not count null (‘\0’) character. It returns number of character of the string.

Example: If you want to find the length of “Computer” it returns 7.


strlen() Program - 1

#include <stdio.h>
OUTPUT
#include <string.h>
Enter string : Computer
int main()
String length is : 8
{
char str[25];
printf("Enter string : ");
scanf("%s",&str);
printf(“String length is : %d",strlen(str));
}
strlen() Program - 2

#include <stdio.h>
#include <string.h> OUTPUT
int main()
Enter string : C Fun
{
String length is : 5
char str[25];
int i=0;
printf("Enter string : ");
gets(str);
while(str[i]!='\0')
{
i=i+1;
}
printf(“String length = %d”,i);
}
String Function strrev()

String function strrev() is used to reverse a string.

Example: If you want to reverse string it shows “Hello” as “olleH”.


strrev() Program - 1

#include <stdio.h>
OUTPUT
#include <string.h>
Enter string : CU
int main()
Reverse string is : UC
{
char str[25];
printf("Enter string : ");
scanf("%s",&str);
printf(“Reverse string is : %s",strrev(str));
}
String Function strcpy()

String function strcpy() is used to copy source value to destination


variable.

Example: If you want to copy one string to another the syntax is.

Syntax: strcpy (destination, source);


strcpy() Program - 1

#include <stdio.h>
OUTPUT
#include <string.h>
Enter string : Hello
int main()
Value of str2 is : Hello
{
char str1[25], str2[25];
printf("Enter string : ");
scanf("%s",&str1);
strcpy( str2, str1);
printf(“Value of str2 is : %s",str2);
}
String Function strncpy()

String function strncpy() is known as bounded string copy it is used to


copy n (specified) number of character source value to destination
variable.

Example: If you want to copy one string to another with specified number
of character the syntax is.

Syntax: strcpy (destination, source, n);


strncpy() Program - 1

#include <stdio.h>
OUTPUT
#include <string.h>
Value of str2 is : C Pro
int main()
{
char str1[25]=“C Program, str2[25];
int n = 5;
strncpy( str2, str1, n);
printf(“Value of str2 is : %s",str2);
}
strncpy() Program - 2

#include <stdio.h>
#include <string.h> OUTPUT
int main()
{ Enter string : Hello C
char str1[25, str2[25]; Enter no of char : 5
int n; Value of str2 is : Hello
printf(“Enter string : “);
gets(str1);
printf(“Enter no. of character to be copy : “);
scanf(“%d”,&n);
strncpy( str2, str1, n);
printf(“Value of str2 is : %s",str2);
}
String Function strcat()

String function strcat() function concatenates two strings and result is


returned to source string.

Example: If you want to concatenate one string to another strcat is used.

Syntax: strcat (source, new_string);


strcat() Program - 1

#include <stdio.h>
OUTPUT
#include <string.h>
Enter string : C Program
int main()
Value of str2 is : C Programming
{
char str1[25];
printf(“Enter string : “);
gets(str1);
strcat( str1, “ming”);
printf(“Value of str1 is : %s",str1);
}
strcat() Program - 2

#include <stdio.h>
OUTPUT
#include <string.h>
Full name is : Peter Smith
int main()
{
char fname[25]=“Peter”, lname[25]=“Smith”;
strcat(fname,” “);
strcat(fname, lname);
printf(“Full name is : %s",fname);
}
String Function strcmp()

String function strcmp() function is used to compare two strings if strings


are identical then it returns 0 otherwise it returns non zero values. It is
case sensitive it means it checks small and capital letter.

Example: “Hello” and hello is not same.

Syntax: strcmp (String1, String2);


strcmp() Program - 1

#include <stdio.h>
#include <string.h> OUTPUT
int main()
{ Strings are not equal
char str1[25]="Hello";
char str2[25]="hello";
int n=strcmp(str1,str2);
if(n==0)
printf("Both strings are equal");
else
printf("Strings are not equal");
return 0;
}
String Function stricmp()

String function stricmp() function is used to compare two strings if strings


are identical then it returns 0 otherwise it returns non zero values. If we
use stricmp it means it ignores small and capital letter it will check
spelling only.

Example: “Hello” and hello is the same.

Syntax: stricmp (String1, String2);


strcimp() Program - 1

#include <stdio.h>
#include <string.h> OUTPUT
int main()
{ Strings are equal
char str1[25]="Hello";
char str2[25]="hello";
int n=stricmp(str1,str2);
if(n==0)
printf("Both strings are equal");
else
printf("Strings are not equal");
return 0;
}
String Function strncmp()

String function strncmp() function is used to compare two strings at


specified number of character with case sensitivity, if strings are identical
then it returns 0 otherwise it returns non zero values.

Syntax: strncmp (String1, String2, n);


strcnmp() Program - 1

#include <stdio.h>
#include <string.h> OUTPUT
int main()
{ Strings are not equal
char str1[25]=“Wonderful";
char str2[25]=“Wonderer";
int n=strncmp(str1,str2,7);
if(n==0)
printf("Both strings are equal");
else
printf("Strings are not equal");
return 0;
}
String Function strlwr()

String function strlwr() returns given string in lowercase.

Syntax: strlwr (String1);


strlwr() Program - 1

#include <stdio.h>
OUTPUT
#include <string.h>
Str1 = Hello
int main()
Str2 = hello
{
char str1[25]=“Hello";
printf(“Str1 = %s\n“,str1);
printf(“Lowercase = %s\n“,strlwr(str1));
return 0;
}
String Function strupr()

String function strupr() returns given string in uppercase letter.

Syntax: strupr (String1);


strupr() Program - 1

#include <stdio.h>
OUTPUT
#include <string.h>
Str1 = Hello
int main()
Uppercase = HELLO
{
char str1[25]=“Hello";
printf(“Str1 = %s\n“,str1);
printf(“Uppercase = %s\n“,strupr(str1));
return 0;
}
Character Function

All data is entered into computers as characters, which includes letters,


digits and various special symbols.

The character-handling library includes several functions that perform


useful tests and manipulations of character data.

Character functions need ctype.h header file to be included in the


program. Different character functions provided by C Language are:
Character Function isalpha()

isalpha() function in C language checks whether given character is


alphabet or not.

The isalpha function returns a nonzero value if c is alphabetic and returns


zero if c is not alphabetic.

Syntax: int isalpha(int c);


isalpha() Program - 1

#include <stdio.h> OUTPUT


#include <ctype.h> Enter any character : A
int main() Enter character is alphabetic
{
char ch;
printf("Enter any character : ");
scanf("%c",&ch);
if(isalpha(ch))
printf("Entered character is alphabetic\n");
else
printf("Entered character is not alphabetic\n");
return 0;
}
Character Function isalnum()

isalnum() function in C language checks whether given character is


alphanumeric or not.

The isalnum function returns a nonzero value if c is alphanumeric and


returns zero if c is not alphanumeric.

Syntax: int isalnum(int c);


isalnum() Program - 1

#include <stdio.h> OUTPUT


#include <ctype.h> Enter any character : A or 4
Enter character is alphanumeric
int main()
{
char ch;
printf("Enter any character : ");
scanf("%c", &ch);
if ( isalnum ( ch ) )
printf("Entered character is alphanumeric\n");
else
printf("Entered character is not alphanumeric\n");
return 0;
}
Character Function isdigit()

isdigit() function in C language checks whether given character is digit or


not.

The isdigit function returns a nonzero value if c is digit and returns zero
if c is not a digit.

Syntax: int isdigit(int c);


isdigit() Program - 1

#include <stdio.h> OUTPUT


#include <ctype.h> Enter any character : 4
Enter character is digit
int main()
{
char ch;
printf("Enter any character : ");
scanf("%c", &ch);
if ( isdigit ( ch ) )
printf ( "Entered character is digit\n" ) ;
else
printf ( "Entered character is not digit\n" ) ;
return 0;
}
Character Function islower()

islower() function in C language checks whether given character is


lowercase or not.

The islower function returns a nonzero value if c is lowercase letter and


returns zero if c is not a lowercase letter.

Syntax: int islower(int c);


islower() Program - 1

#include <stdio.h> OUTPUT


#include <ctype.h> Enter any character : a
Lower case
int main()
{
char ch;
printf("Enter any character : ");
scanf("%c", &ch);
if(islower(ch))
printf(“Lower case\n" ) ;
else
printf(“Not a lower case\n" ) ;
return 0;
}
Character Function islupper()

isupper() function in C language checks whether given character is


lowercase or not.

The isupper function returns a nonzero value if c is uppercase letter and


returns zero if c is not a uppercase letter.

Syntax: int isupper(int c);


isupper() Program - 1

#include <stdio.h> OUTPUT


#include <ctype.h> Enter any character : A
Upper case
int main()
{
char ch;
printf("Enter any character : ");
scanf("%c", &ch);
if(isupper(ch))
printf(“Upper case\n" ) ;
else
printf(“Not an upper case\n" ) ;
return 0;
}
Character Function tolower()

tolower() function in C language checks whether given character is


alphabetic and convert it to lowercase.

The tolower function returns c as a lowercase letter. If c is already


lowercase, the tolower function returns c unchanged.

Syntax: int tolower(int c);


tolower() Program - 1

#include <stdio.h> OUTPUT


#include <ctype.h> Enter any character : A
Lower case : a
int main()
{
char ch;
printf("Enter any character : ");
scanf("%c", &ch);
if(isalpha(ch))
printf(“Lower case : %c\n“,tolower(ch)) ;
else
printf(“Not an alphabetic") ;
return 0;
}
Character Function toupper()

toupper() function in C language checks whether given character is


alphabetic and convert it to lowercase.

The toupper function returns c as a lowercase letter. If c is already


lowercase, the toupper function returns c unchanged.

Syntax: int toupper(int c);


toupper() Program - 1

#include <stdio.h> OUTPUT


#include <ctype.h> Enter any character : d
Upper case : D
int main()
{
char ch;
printf("Enter any character : ");
scanf("%c", &ch);
if(isalpha(ch))
printf(“Upper case : %c\n“,toupper(ch)) ;
else
printf(“Not an alphabetic") ;
return 0;
}
Mathematical Function

C Programming allows us to perform mathematical operations through the


functions defined in <math.h> header file. The <math.h> header file
contains various methods for performing mathematical operations.

Some mathematical functions are:

a) abs (number)
b) ceil (number)
c) floor (number)
d) pow (d1,d2)
e) sqrt (number)
Mathematical Function

a) abs (number) – Returns the absolute value of given number.

b) ceil (number) – Rounds up the given number. It returns the integer

value which is greater than or equal to given number.

c) floor (number) – rounds down the given number. It returns the integer

value which is less than or equal to given number.

d) pow (d1,d2) – Returns the power of given number.

e) sqrt (number) – Returns the square root of given number.


Math Program - 1

#include <stdio.h>
#include <ctype.h> OUTPUT
int main()
{ 12
printf("%d",abs(-12)); 4.00
printf("\n%.2f",ceil(3.6)); 4.00
printf("\n%.2f",ceil(3.3)); 3.00
printf("\n%.2f",floor(3.6)); 3.00
printf("\n%.2f",floor(3.3)); 4.00
printf("\n%.2f",sqrt(16)); 2.00
printf("\n%.2f",sqrt(7)); 16.00
printf("\n%.2f",pow(2,4)); 27.00
printf("\n%.2f",pow(3,3));
return 0;
}

You might also like