0% found this document useful (0 votes)
2 views12 pages

4. Inbuilt Functions and Format Specifiers (1)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 12

Inbuilt Functions and

Format Specifiers
Format Specifiers

 The format specifiers are used in C for input and


output purposes. Using this concept the compiler can
understand that what type of data is in a variable
during taking input using the scanf() function and
printing using printf() function. Here is a list of format
specifiers.
Format Specifiers
Format Specifier Type
%c Character
%d Signed integer
%e or %E Scientific notation of floats
%f Float values
%g or %G Similar as %e or %E
%l or %ld or %li Long
%lf Double
%Lf Long double
%lu Unsigned int or unsigned long
%llu Unsigned long long
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character
#include <stdio.h> Format Specifiers
main()
{
char ch = 'B';
printf("%c", ch); //printing character data
//print decimal or integer data with d and i
int x = 45, y = 90;
printf("\n%d", x);
printf("\n%i", y);
float f = 12.67;
printf("\n%f", f); //print float value
printf("\n%e", f); //print in scientific notation
int a = 67;
printf("\n%o", a); //print in octal format
printf("\n%x", a); //print in hex format
char str[] = "Hello World";
printf("\n%s", str);
printf("\n%20s", str); //shift to the right 20 characters including the string
printf("\n%-20s", str); //left align
printf("\n%20.5s", str); //shift to the right 20 characters including the string, and print
string up to 5 character
printf("\n%-20.5s", str); //shift to the right 20 characters including the string, and print
string up to 5 character

}
Functions
 In c, we can divide a large program into the basic building blocks known as function. The

function contains the set of programming statements enclosed by {}. A function can be called

multiple times to provide reusability and modularity to the C program. In other words, we

can say that the collection of functions creates a program. The function is also known

as procedure or subroutine in other programming languages.

 A function is a block of code that performs a specific task. A complex problem is often easier

to solve by dividing it into several smaller parts, each of which can be solved by itself.
Advantages of functions
 Functions support modular programming in which a large program is divided into smaller self contained parts,
each of which has a unique identifiable purpose. This helps the program easier to write, understand, debug and
maintain.

 It helps in reducing the overall size of the program as repetitive set of instructions are written only once in the
function.

 Use of functions enhances the readability of a program. A big code is always difficult to read. Breaking the
code in smaller Functions keeps the program organized, easy to understand and makes it reusable.

 A function can be used to create our own header file which can be used in any number of programs i.e. the
reusability.

 In C, a function can call itself again, this is known as recursion. Using recursion many calculations can be
done very easily.
Classification of functions

 Functions can be classified broadly in two categories: Library functions (or in-built functions) and user-defined
functions.
Library Functions
 Library functions are those functions which are already defined in C library, example printf(), scanf(), strcat()

etc. The programmers need to include appropriate header files to use these functions. These are already

declared and defined in C libraries.

 There functions are already defined in the C compilers. They are used for String handling, I/O operations, etc.

These functions are defined in the header file. To use these functions in a program particular header file in

which the function definition is present should be added.


Library Functions
 Some examples of Library Functions are

 pow() finds the power of the given number.

 sin() finds the sine of the given number.

 exp() finds the exponent of the given number.

 cos() finds the cosine of the given number.

 sqrt() finds the square root of the number.

 log() finds the logarithmic value of the number.


Program of Square root without using Inbuilt
Function */

for(i=0;i<num;i=i+j)
void main() {
{ if((i*i)>num)
float i,j; {
float num; i=i-j;
j=0.1; break;
clrscr(); }
}
scanf("%f",&num); printf(“Square root
of the number is %.2f",i);
Program of Square root with using Inbuilt Function
void main() */
{

int x,f;

scanf("%d",&x);

f=sqrt(x);

printf("%d",f);

getch();

You might also like