3. 3.1. Built-in Functions
3.1.1. Math functions
3.1.2. Console I/O functions
3.1.3. Standard I/O functions
3.1.4. Character Oriented Functions
4. 3.1. What is Built-in Functions in C
or
What is in-built Functions in C
or
What is library functions in C
Each built-in function in C performs specific operation
We can use of these built-in functions to get the pre-defined
output instead of writing our own code for those outputs.
Ex: scanf(), printf(), sin(), cos(), getch(),strcat(),strcmp()
5. 3.1.1. Math Functions : #include <math.h>
Math operations Meanings
sin(x) Sine of (x)
cos(x) Cosine of (x)
tan (x) Tangent of (x)
pow(x,y) X to the power of y
sqrt(x) Square root of x
8. 1.17.Formatted Input
Formatted input to an input data that has been arranged in a
particular format
Syntax:
scanf(“Format Specifier”, &var1, &var2, …., &varn);
Ex:
scanf(”%d %d” , &a,&b)
10. 1.getch(): to read a character from the keyboard
Ex: x=getch();
2.putch(): to display a character on the monitor
ex: printf(“%c”, putch(x));
3.gets() : to read a string of characters including white spaces from
the keyboard
Ex: gets(name);
4.puts(): to display string of characters including white spaces on
the monitor Ex: puts(name);
3.1.3. Character oriented Input Output functions
12. 3.2. User –defined functions
A function is a self-contained block of code that
perform a particular task.
syntax
void function_name(argument list)
{
local variable declarations;
executable statements-1;
executable statements-2;
……………………
……………………
}
13. 3.2. User –defined functions (cont..)
void function_name(argument list)
{
local variable declarations;
executable statements-1;
executable statements-2;
……………………
……………………
}
Where
function_name user defined name
Argument list surrounded by parenthesis
contains valid variable names separated by commas
Ex:
void square(int x)
{
int res;
res = x*x;
printf(“Square is “%d”, res);
}
14. 3.1.2. Find square of given number using user
defined function
#include<stdio.h>
#include<conio.h>
void square(int x)
{
int res;
res = x*x;
printf(“Square is “%d”, res);
}
void main()
{
int s;
scanf(“%d”, &s);
square(s);
}
15. 3.2.1. User –defined functions with return values
return type function_name(argument list)
{
local variable declarations;
executable statements-1;
executable statements-2;
……………………
……………………
return(value);
}
Where
return type data type of return value
function_name user defined name
Return(value) Taking value from user
defined function to main function
Ex
int addition(int x, int y)
{
int sum;
sum = x + y;
return(sum);
}
16. Ex.no.13. Find square of given number using user
defined function with return type
#include<stdio.h>
#include<conio.h>
int square(int x)
{
int res;
res = x*x;
return res;
}
void main()
{
int s,res;
scanf(“%d”, &s);
res= square(s);
printf(“square of a given number is %d” , res);
}
Calling fn
Called fn
17. 3.3. Function Prototype
Definition: A function prototype is a declaration of a function that
specifies the function’s name & type (data type & return type) but
omits the function body
Ex: returntype function_name (arg1, arg2, ……);
#include <stdio.h}
#include<conio.h>
int add(int x); function prototype for add
void main()
{
……………………..
}
int add( int x)
{…………………… }
18. #include<stdio.h>
#include<conio.h>
int square(int x) ;
void main()
{
int s,res;
scanf(“%d”, &s);
res= square(s);
printf(“square of a given number is %d” , res);
}
int square(int x)
{
int res;
res = x*x;
return res;
}
Called fn
Prototype
Calling fn
3.3. Function Prototype (cont..)
19. 3.4. Recursion
If a program allows you to call a function inside the same function
Recursion is the process of repeating items in a self-similar way
void main()
{
printf(“This is an example of recursion “);
main();
}
Execution is terminated abruptly ;otherwise the execution will
continue indefinitely
Output: This is an example of recursion
This is an example of
recursion
This is an example of
recursion
This is an example of
recursion
This is an examp
27. Ex. No. 15. Write a C program to print Book details using
array of structure
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int regno;
char branch[10];
} ;
void main()
{
struct student stud[3];
for (i=1; i<=5; i++)
{
scanf(“%s”, &stud[i].name);
scanf(“%d”, & stud[i].regno);
scanf(“%s”,&stud[i].branch);
}
for(i=1; i<=5; i++)
{
printf(“%s”, stud[i].name);
printf(“%d”, stud[i].regno);
28. CS1106- Programming Languages- Assignment No. 1
1. Write a C program to find sum of n numbers:
2. Write a C program to find factorial of a given number
3. Write a C program to find sum of digits of a number:
4. Write a C Program to Reverse the given number:
5. Write a C Program to find number is Armstrong number or not
6. Write a C Program to check the number is palindrome or not
7. Write a C to print numbers divisible by 5 not by 10:
8. Write a C Program to print fibonacci series.
9. Write a C program to Check the given number is prime or not
10. Write a C program to Print first n prime numbers
Deadline for submission
on 18.12.2023
All variables should use your names
29. 3.6. Union
union tag name
{
datatype member variable1;
datatype member variable 2;
……………..
………………
} ;
void main()
{ union tagname union_name;
}
Ex:
union student
{
char name[20];
int regno;
char branch[10];
} ;
void main()
{
union student stud;
}
30. 3.6. Union
Unions are a concept borrowed from
structure and therefore follow same
syntax as structure
Major distinction between them in
terms of storage
In structure each member has its own
storage location, where as all the
members of a union use the same
memory location