SlideShare a Scribd company logo
CS 1106
PROGRAMMING
LANGUAGES
Prepared By
Dr. Prabakaran Narayanan
Functions,
Structure
&
Union
Unit -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
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()
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
3.1.1. Math Functions : #include <math.h>
Ex.no.12 . Math.h operations
3.1.2. Console Input Output functions
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)
3.1.2. Console Input Output functions
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
CS 1106 - UNIT -3 notes for students to learn
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;
……………………
……………………
}
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);
}
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);
}
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);
}
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
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)
{…………………… }
#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..)
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
3.4. Recursion (cont..)
void fact()
{
printf(“Welcome”);
fact();
}
void main()
{
fact();
}
Output: welcome
welcome
welcome
welcome
3.5. Structure
Definition:
It is used to create a collection of data
items of different data types using a
single name
3.5.1. Structure Declaration
struct tag name
{
datatype member variable1;
datatype member variable 2;
……………..
………………
} ;
void main()
{ struct tagname structure_name;
}
Ex:
struct student
{
char name[20];
int regno;
char branch[10];
} ;
void main()
{
struct student stud;
}
3.5.2. Accessing Member variable
 To access the member variable in structure
syntax:
struct_name . member variable;
Reading inputs through keyboard:
scanf(“%s”, &stud.name);
scanf(“%d”, & stud.regno);
scanf(“%s”,&stud.branch);
Ex:
struct student
{
char name[20];
int regno;
char branch[10];
} ;
void main()
{
struct student stud;
}
3.5.3. Structure Initialization
Syntax:
struct tag name st_name={value1, value2,…. }
Ex:
Struct student stud={“Mansoor ”, 213836, “Civil”};
name
regno
branch
b.
Ex:
struct student
{
char name[20];
int regno;
char branch[10];
} ;
void main()
{
struct student stud;
}
Ex. No. 14. Write a C program to print
student details using structure
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int regno;
char branch[10];
} ;
void main()
{
struct student stud;
scanf(“%s”, &stud.name);
scanf(“%d”, & stud.regno);
scanf(“%s”,&stud.branch);
printf(“%s”, stud.name);
printf(“%d”, stud.regno);
printf(“%s”, ”,stud.branch);
}
3.5.3. Array of
Structure
title
page
price
stud[1].
title
page
price
stud[2].
price
page
title
stud[3].
Ex:
struct student
{
char name[20];
int regno;
char branch[10];
} ;
void main()
{
struct student
stud[3];
}
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);
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
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;
}
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
CS 1106 - UNIT -3 notes for students to learn
End of unit-3

More Related Content

PPTX
USER DEFINE FUNCTION AND STRUCTURE AND UNION
PPT
Introduction to Basic C programming 01
PPTX
C language
PPT
C programming
PPTX
Chapter 1_C Fundamentals_HS_Tech Yourself C.pptx
PPT
Fucntions & Pointers in C
PDF
Principals of Programming in CModule -5.pdfModule-3.pdf
USER DEFINE FUNCTION AND STRUCTURE AND UNION
Introduction to Basic C programming 01
C language
C programming
Chapter 1_C Fundamentals_HS_Tech Yourself C.pptx
Fucntions & Pointers in C
Principals of Programming in CModule -5.pdfModule-3.pdf

Similar to CS 1106 - UNIT -3 notes for students to learn (20)

PPTX
C programming(part 3)
PPTX
Functions in c
PPTX
Programming_in_C_language_Unit5.pptx course ATOT
PDF
Function lecture
PPTX
Data structures and algorithms lab1
PPTX
SND_FPL_ First year EnggSPPU UNIT-V.pptx
PPTX
programming in C and Datastructures deepdive
PPT
Functions and pointers_unit_4
PDF
programming in C & Data structures an easy approach
PPTX
Programming in C
PPTX
Programming in C
PPS
C programming session 01
PPTX
Function in c
PPTX
Functions
PPT
eee2-day4-structures engineering college
PPTX
Funtions of c programming. the functions of c helps to clarify all the tops
PDF
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
PDF
Programming Fundamentals Functions in C and types
PPTX
C language
PPTX
C language 3
C programming(part 3)
Functions in c
Programming_in_C_language_Unit5.pptx course ATOT
Function lecture
Data structures and algorithms lab1
SND_FPL_ First year EnggSPPU UNIT-V.pptx
programming in C and Datastructures deepdive
Functions and pointers_unit_4
programming in C & Data structures an easy approach
Programming in C
Programming in C
C programming session 01
Function in c
Functions
eee2-day4-structures engineering college
Funtions of c programming. the functions of c helps to clarify all the tops
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
Programming Fundamentals Functions in C and types
C language
C language 3
Ad

More from captainricardus (7)

PPTX
MY STRING AND ARRAY PROGRAM for all student
PPTX
SESSION 5 The fifth part of a lecture 5.pptx
PPTX
SESSIONSESSION 4 The fourth part of a lecture 3.pptx 4.pptx
PPTX
SESSION 3 The third part of a lecture 3.pptx
PPTX
SESSION 2 The second part of a lecture no 2.pptx
PPTX
SESSION 1: The first part of a lecture no 1.pptx
PPTX
7. Network Communication and Application.pptx
MY STRING AND ARRAY PROGRAM for all student
SESSION 5 The fifth part of a lecture 5.pptx
SESSIONSESSION 4 The fourth part of a lecture 3.pptx 4.pptx
SESSION 3 The third part of a lecture 3.pptx
SESSION 2 The second part of a lecture no 2.pptx
SESSION 1: The first part of a lecture no 1.pptx
7. Network Communication and Application.pptx
Ad

Recently uploaded (20)

PPTX
retention in jsjsksksksnbsndjddjdnFPD.pptx
PPT
lectureusjsjdhdsjjshdshshddhdhddhhd1.ppt
PPTX
Copy of 16 Timeline & Flowchart Templates – HubSpot.pptx
PPTX
Topic 5 Presentation 5 Lesson 5 Corporate Fin
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
DOCX
Factor Analysis Word Document Presentation
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PDF
REAL ILLUMINATI AGENT IN KAMPALA UGANDA CALL ON+256765750853/0705037305
PDF
annual-report-2024-2025 original latest.
PPTX
Managing Community Partner Relationships
PPT
ISS -ESG Data flows What is ESG and HowHow
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
New ISO 27001_2022 standard and the changes
PDF
Optimise Shopper Experiences with a Strong Data Estate.pdf
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PDF
Systems Analysis and Design, 12th Edition by Scott Tilley Test Bank.pdf
PDF
[EN] Industrial Machine Downtime Prediction
PPTX
Database Infoormation System (DBIS).pptx
PPTX
STERILIZATION AND DISINFECTION-1.ppthhhbx
PPTX
Introduction to Inferential Statistics.pptx
retention in jsjsksksksnbsndjddjdnFPD.pptx
lectureusjsjdhdsjjshdshshddhdhddhhd1.ppt
Copy of 16 Timeline & Flowchart Templates – HubSpot.pptx
Topic 5 Presentation 5 Lesson 5 Corporate Fin
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
Factor Analysis Word Document Presentation
IBA_Chapter_11_Slides_Final_Accessible.pptx
REAL ILLUMINATI AGENT IN KAMPALA UGANDA CALL ON+256765750853/0705037305
annual-report-2024-2025 original latest.
Managing Community Partner Relationships
ISS -ESG Data flows What is ESG and HowHow
Acceptance and paychological effects of mandatory extra coach I classes.pptx
New ISO 27001_2022 standard and the changes
Optimise Shopper Experiences with a Strong Data Estate.pdf
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
Systems Analysis and Design, 12th Edition by Scott Tilley Test Bank.pdf
[EN] Industrial Machine Downtime Prediction
Database Infoormation System (DBIS).pptx
STERILIZATION AND DISINFECTION-1.ppthhhbx
Introduction to Inferential Statistics.pptx

CS 1106 - UNIT -3 notes for students to learn

  • 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
  • 6. 3.1.1. Math Functions : #include <math.h> Ex.no.12 . Math.h operations
  • 7. 3.1.2. Console Input Output functions
  • 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)
  • 9. 3.1.2. Console Input Output functions
  • 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
  • 20. 3.4. Recursion (cont..) void fact() { printf(“Welcome”); fact(); } void main() { fact(); } Output: welcome welcome welcome welcome
  • 21. 3.5. Structure Definition: It is used to create a collection of data items of different data types using a single name
  • 22. 3.5.1. Structure Declaration struct tag name { datatype member variable1; datatype member variable 2; …………….. ……………… } ; void main() { struct tagname structure_name; } Ex: struct student { char name[20]; int regno; char branch[10]; } ; void main() { struct student stud; }
  • 23. 3.5.2. Accessing Member variable  To access the member variable in structure syntax: struct_name . member variable; Reading inputs through keyboard: scanf(“%s”, &stud.name); scanf(“%d”, & stud.regno); scanf(“%s”,&stud.branch); Ex: struct student { char name[20]; int regno; char branch[10]; } ; void main() { struct student stud; }
  • 24. 3.5.3. Structure Initialization Syntax: struct tag name st_name={value1, value2,…. } Ex: Struct student stud={“Mansoor ”, 213836, “Civil”}; name regno branch b. Ex: struct student { char name[20]; int regno; char branch[10]; } ; void main() { struct student stud; }
  • 25. Ex. No. 14. Write a C program to print student details using structure #include<stdio.h> #include<conio.h> struct student { char name[20]; int regno; char branch[10]; } ; void main() { struct student stud; scanf(“%s”, &stud.name); scanf(“%d”, & stud.regno); scanf(“%s”,&stud.branch); printf(“%s”, stud.name); printf(“%d”, stud.regno); printf(“%s”, ”,stud.branch); }
  • 26. 3.5.3. Array of Structure title page price stud[1]. title page price stud[2]. price page title stud[3]. Ex: struct student { char name[20]; int regno; char branch[10]; } ; void main() { struct student stud[3]; }
  • 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