SlideShare a Scribd company logo
1 of 8
/* program to find area of a ring */ #include<stdio.h> int  main() { float a1,a2,a,r1,r2; printf(&quot;Enter the radius : &quot;); scanf(&quot;%f&quot;,&r1); a1 = 3.14*r1*r1; printf(&quot;Enter the radius : &quot;); scanf(&quot;%f&quot;,&r2); a2 = 3.14*r2*r2;  a = a1- a2; printf(&quot;Area of Ring : %.3f&quot;, a);  }  /* program to find area of a ring */ #include<stdio.h> float area(); int  main() { float a1,a2,a; a1 = area(); a2 = area(); a = a1- a2; printf(&quot;Area of Ring : %.3f&quot;, a);  }  float area() { float r; printf(&quot;Enter the radius : &quot;); scanf(&quot;%f&quot;, &r); return (3.14*r*r); } Modularizing and Reusing of code through Functions Calculation of area of Circle is separated into a separate module from Calculation of area of Ring and the same module can be reused for multiple times.  Function Declaration Function Definition Function Calls Repeated & Reusable blocks of code
A  Function  is an  independent, reusable module  of statements, that specified by a name. This module (sub program) can be called by it’s name to do a specific task. We can call the function, for any number of times and from anywhere in the program. The purpose of a function is to receive zero or more pieces of data, operate on them, and return at most one piece of data. A  Called Function  receives control from a  Calling Function . When the called function completes its task, it returns control to the calling function. It may or may not return a value to the caller. The function main() is called by the operating system; main() calls other functions. When main() is complete, control returns to the operating system.   int main() { int n; float p, r, si; printf(“Enter Details of Loan1:“); scanf( “%f %d %f”, &p, &n, &r); si = calcInterest (  p,  n ,  r  ); printf(“Interest : Rs. %f”, si); printf(“Enter Details of Loan2:“);  } float  calcInterest (float loan  ,  int  terms  , float iRate  ) { float interest; interest = ( loan * terms * iRate )/100; return  ( interest  ); } value of ‘r’ is copied to ‘iRate’   value of  ‘n’ is copied to terms’   value of  ‘p’ is copied to loan’  value of  ‘interest’ is  assigned to ‘si ’   Called Function Calling Function The  block is executed Process of Execution for a Function Call
int  main() { int n1, n2; printf(&quot;Enter a number : &quot;); scanf(&quot;%d&quot;,&n1); printOctal(n1); readPrintHexa();  printf(&quot;Enter a  number : &quot;); scanf(&quot;%d&quot;,&n2); printOctal(n2);  printf(“”); }  void printOctal(int n) { printf(&quot;Number in octal form : %o &quot;, n);  }  void  readPrintHexa() { int num; printf(&quot;Enter a number : &quot;);  scanf(&quot;%d&quot;,&num); printHexa(num);  printf(“”);  }  void printHexa(int n) { printf(&quot;Number in Hexa-Decimal form : %x &quot;,n);  }  1 2 3 4 5 6 7 8 Flow of Control in  Multi-Function Program
/* Program demonstrates function calls */ #include<stdio.h> int  add  (  int n1, int n2  ) ; int main(void) { int a, b, sum; printf(“Enter two integers : ”); scanf(“%d %d”, &a, &b); sum = add (  a , b  ) ; printf(“%d +  %d = %d”, a, b, sum); return 0; } /* adds two numbers and return the sum */ int  add  (  int  x ,  int  y  ) { int s; s = x + y; return (  s  ); } Declaration (proto type) of Function Formal Parameters Function Call Actual Arguments Definition of Function Parameter List used in the Function Return statement of the Function Return Value Return Type Function-It’s Terminology Function Name
/* using different functions */ int main() { float radius, area; printMyLine(); printf(“Usage of functions”); printYourLine(‘-’,35); radius = readRadius(); area = calcArea ( radius ); printf(“Area of Circle = %f”, area); } void  printMyLine() { int i; for(i=1; i<=35;i++) printf(“%c”,  ‘-’); printf(“”); } Function with No parameters and No return value void printYourLine(char ch, int n) { int i; for(i=1;  i<=n ;i++) printf(“%c”,  ch); printf(“”); } Function with parameters and No return value float  readRadius() { float r; printf(“Enter the radius :  “); scanf(“%f”, &r); return  ( r ); } Function with return value & No parameters float  calcArea(float r) { float  a; a = 3.14 * r * r ; return  ( a ) ; } Function with return value and parameters Categories of Functions Note: ‘void’ means “Containing nothing”
#include<stdio.h>  float length, breadth;  int main() { printf(&quot;Enter length, breadth : &quot;); scanf(&quot;%f %f&quot;,&length,&breadth); area(); perimeter(); printf(“Enter length, breadth: &quot;); scanf(&quot;%f %f&quot;,&length,&breadth); area(); perimeter(); } void perimeter() { int no = 0;  float p; no++; p = 2 *(length + breadth); printf(“Perimeter of Rectangle %d: %.2f&quot;,no,p); } void area() { static int num = 0;  float a; num++; a = (length * breadth); printf(“Area of Rectangle %d : %.2f&quot;, num, a); } Enter length, breadth : 6  4 Area of Rectangle 1 : 24.00 Perimeter of Rectangle 1 : 20.00 Enter length, breadth : 8  5 Area of Rectangle 2 : 40.00 Perimeter of Rectangle 1 : 26.00 Automatic Local Variables  Scope  : visible with in the function.  Lifetime:  re-created for every function call and destroyed automatically when function is exited.  Static Local Variables  Visible with in the function,   created only once when function is called at first time and exists between function calls. External Global Variables  Scope : Visible across multiple functions  Lifetime : exists  till the end of the program. Storage Classes – Scope & Lifetime
#include<stdio.h>  float length, breadth; static float base, height; int main() { float peri; printf(&quot;Enter length, breadth : &quot;); scanf(&quot;%f %f&quot;,&length,&breadth); rectangleArea(); peri = rectanglePerimeter(); printf(“Perimeter of Rectangle : %f“, peri); printf(“Enter base , height: &quot;); scanf(&quot;%f %f&quot;,&base,&height); triangleArea();  } void rectangleArea() { float a; a = length * breadth; printf(“Area of Rectangle : %.2f&quot;,  a); } void triangleArea() { float a; a = 0.5 * base * height ; printf(“Area of Triangle : %.2f&quot;,  a); } extern float length, breadth ; /*  extern base , height ;  --- error  */ float rectanglePerimeter() { float p; p = 2 *(length + breadth);  return ( p ); } File1.c File2.c External Global Variables  Scope : Visible to all functions across all files in the project. Lifetime : exists  till the end of the program. Static Global Variables  Scope : Visible to all functions with in the file only. Lifetime : exists  till the end of the program. Storage Classes – Scope & Lifetime
#include<stdio.h>  void showSquares(int n) { if(n == 0) return; else showSquares(n-1); printf(“%d  “, (n*n)); } int main() { showSquares(5); } A  function calling itself is  Recursion Output :  1  4  9  16  25   showSquares(5 ) showSquares(4) showSquares(3) showSquares(2) showSquares(1) addition of  function calls to  call-stack   call-stack execution of  function calls in  reverse  Preprocessor Directives #define   - Define a macro substitution #undef   - Undefines a macro #ifdef  - Test for a macro definition #ifndef  - Tests whether a macro is not    defined #include   - Specifies the files to be included #if  - Test a compile-time condition #else  - Specifies alternatives when  #if    test fails #elif   - Provides alternative test facility #endif  - Specifies the end of  #if #pragma   - Specifies certain instructions #error   - Stops compilation when an error    occurs #   - Stringizing  operator ##   - Token-pasting operator Preprocessor is a program that processes the source code before it passes through the compiler. main()

More Related Content

What's hot

What's hot (20)

C Programming
C ProgrammingC Programming
C Programming
 
Input And Output
 Input And Output Input And Output
Input And Output
 
C programs
C programsC programs
C programs
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
C Programming
C ProgrammingC Programming
C Programming
 
Program flowchart
Program flowchartProgram flowchart
Program flowchart
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
C-Language Unit-2
C-Language Unit-2C-Language Unit-2
C-Language Unit-2
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Programming in C [Module One]
Programming in C [Module One]Programming in C [Module One]
Programming in C [Module One]
 
Functions
FunctionsFunctions
Functions
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
C programms
C programmsC programms
C programms
 
C Language Unit-1
C Language Unit-1C Language Unit-1
C Language Unit-1
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 

Viewers also liked

Viewers also liked (6)

Unit 1 Presentation Will Sharp
Unit 1 Presentation  Will SharpUnit 1 Presentation  Will Sharp
Unit 1 Presentation Will Sharp
 
Les07
Les07Les07
Les07
 
Les10
Les10Les10
Les10
 
Les11
Les11Les11
Les11
 
Les13
Les13Les13
Les13
 
ICT BTEC UNIT 2 P2
ICT BTEC UNIT 2 P2ICT BTEC UNIT 2 P2
ICT BTEC UNIT 2 P2
 

Similar to Unit2 C

C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solutionAnimesh Chaturvedi
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
Input output functions
Input output functionsInput output functions
Input output functionshyderali123
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C LanguageAdnan Khan
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
C basics
C basicsC basics
C basicsMSc CST
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariTHE NORTHCAP UNIVERSITY
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)Ashishchinu
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptxGuy Komari
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Functionimtiazalijoono
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
C programming function
C  programming functionC  programming function
C programming functionargusacademy
 

Similar to Unit2 C (20)

7 functions
7  functions7  functions
7 functions
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solution
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Input output functions
Input output functionsInput output functions
Input output functions
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
String Manipulation Function and Header File Functions
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C Language
 
Function in c program
Function in c programFunction in c program
Function in c program
 
C basics
C basicsC basics
C basics
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
 
Array Cont
Array ContArray Cont
Array Cont
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
Function in c
Function in cFunction in c
Function in c
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
C programming function
C  programming functionC  programming function
C programming function
 

More from arnold 7490 (20)

Les14
Les14Les14
Les14
 
Les09
Les09Les09
Les09
 
Les06
Les06Les06
Les06
 
Les05
Les05Les05
Les05
 
Les04
Les04Les04
Les04
 
Les03
Les03Les03
Les03
 
Les02
Les02Les02
Les02
 
Les01
Les01Les01
Les01
 
Les12
Les12Les12
Les12
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
Unit 5 Java
Unit 5 JavaUnit 5 Java
Unit 5 Java
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
Unit 1 Java
Unit 1 JavaUnit 1 Java
Unit 1 Java
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
 
Unit6 C
Unit6 C Unit6 C
Unit6 C
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
Unit4 C
Unit4 C Unit4 C
Unit4 C
 

Recently uploaded

A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 

Recently uploaded (20)

A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 

Unit2 C

  • 1. /* program to find area of a ring */ #include<stdio.h> int main() { float a1,a2,a,r1,r2; printf(&quot;Enter the radius : &quot;); scanf(&quot;%f&quot;,&r1); a1 = 3.14*r1*r1; printf(&quot;Enter the radius : &quot;); scanf(&quot;%f&quot;,&r2); a2 = 3.14*r2*r2; a = a1- a2; printf(&quot;Area of Ring : %.3f&quot;, a); } /* program to find area of a ring */ #include<stdio.h> float area(); int main() { float a1,a2,a; a1 = area(); a2 = area(); a = a1- a2; printf(&quot;Area of Ring : %.3f&quot;, a); } float area() { float r; printf(&quot;Enter the radius : &quot;); scanf(&quot;%f&quot;, &r); return (3.14*r*r); } Modularizing and Reusing of code through Functions Calculation of area of Circle is separated into a separate module from Calculation of area of Ring and the same module can be reused for multiple times. Function Declaration Function Definition Function Calls Repeated & Reusable blocks of code
  • 2. A Function is an independent, reusable module of statements, that specified by a name. This module (sub program) can be called by it’s name to do a specific task. We can call the function, for any number of times and from anywhere in the program. The purpose of a function is to receive zero or more pieces of data, operate on them, and return at most one piece of data. A Called Function receives control from a Calling Function . When the called function completes its task, it returns control to the calling function. It may or may not return a value to the caller. The function main() is called by the operating system; main() calls other functions. When main() is complete, control returns to the operating system. int main() { int n; float p, r, si; printf(“Enter Details of Loan1:“); scanf( “%f %d %f”, &p, &n, &r); si = calcInterest ( p, n , r ); printf(“Interest : Rs. %f”, si); printf(“Enter Details of Loan2:“); } float calcInterest (float loan , int terms , float iRate ) { float interest; interest = ( loan * terms * iRate )/100; return ( interest ); } value of ‘r’ is copied to ‘iRate’ value of ‘n’ is copied to terms’ value of ‘p’ is copied to loan’ value of ‘interest’ is assigned to ‘si ’ Called Function Calling Function The block is executed Process of Execution for a Function Call
  • 3. int main() { int n1, n2; printf(&quot;Enter a number : &quot;); scanf(&quot;%d&quot;,&n1); printOctal(n1); readPrintHexa(); printf(&quot;Enter a number : &quot;); scanf(&quot;%d&quot;,&n2); printOctal(n2); printf(“”); } void printOctal(int n) { printf(&quot;Number in octal form : %o &quot;, n); } void readPrintHexa() { int num; printf(&quot;Enter a number : &quot;); scanf(&quot;%d&quot;,&num); printHexa(num); printf(“”); } void printHexa(int n) { printf(&quot;Number in Hexa-Decimal form : %x &quot;,n); } 1 2 3 4 5 6 7 8 Flow of Control in Multi-Function Program
  • 4. /* Program demonstrates function calls */ #include<stdio.h> int add ( int n1, int n2 ) ; int main(void) { int a, b, sum; printf(“Enter two integers : ”); scanf(“%d %d”, &a, &b); sum = add ( a , b ) ; printf(“%d + %d = %d”, a, b, sum); return 0; } /* adds two numbers and return the sum */ int add ( int x , int y ) { int s; s = x + y; return ( s ); } Declaration (proto type) of Function Formal Parameters Function Call Actual Arguments Definition of Function Parameter List used in the Function Return statement of the Function Return Value Return Type Function-It’s Terminology Function Name
  • 5. /* using different functions */ int main() { float radius, area; printMyLine(); printf(“Usage of functions”); printYourLine(‘-’,35); radius = readRadius(); area = calcArea ( radius ); printf(“Area of Circle = %f”, area); } void printMyLine() { int i; for(i=1; i<=35;i++) printf(“%c”, ‘-’); printf(“”); } Function with No parameters and No return value void printYourLine(char ch, int n) { int i; for(i=1; i<=n ;i++) printf(“%c”, ch); printf(“”); } Function with parameters and No return value float readRadius() { float r; printf(“Enter the radius : “); scanf(“%f”, &r); return ( r ); } Function with return value & No parameters float calcArea(float r) { float a; a = 3.14 * r * r ; return ( a ) ; } Function with return value and parameters Categories of Functions Note: ‘void’ means “Containing nothing”
  • 6. #include<stdio.h> float length, breadth; int main() { printf(&quot;Enter length, breadth : &quot;); scanf(&quot;%f %f&quot;,&length,&breadth); area(); perimeter(); printf(“Enter length, breadth: &quot;); scanf(&quot;%f %f&quot;,&length,&breadth); area(); perimeter(); } void perimeter() { int no = 0; float p; no++; p = 2 *(length + breadth); printf(“Perimeter of Rectangle %d: %.2f&quot;,no,p); } void area() { static int num = 0; float a; num++; a = (length * breadth); printf(“Area of Rectangle %d : %.2f&quot;, num, a); } Enter length, breadth : 6 4 Area of Rectangle 1 : 24.00 Perimeter of Rectangle 1 : 20.00 Enter length, breadth : 8 5 Area of Rectangle 2 : 40.00 Perimeter of Rectangle 1 : 26.00 Automatic Local Variables Scope : visible with in the function. Lifetime: re-created for every function call and destroyed automatically when function is exited. Static Local Variables Visible with in the function, created only once when function is called at first time and exists between function calls. External Global Variables Scope : Visible across multiple functions Lifetime : exists till the end of the program. Storage Classes – Scope & Lifetime
  • 7. #include<stdio.h> float length, breadth; static float base, height; int main() { float peri; printf(&quot;Enter length, breadth : &quot;); scanf(&quot;%f %f&quot;,&length,&breadth); rectangleArea(); peri = rectanglePerimeter(); printf(“Perimeter of Rectangle : %f“, peri); printf(“Enter base , height: &quot;); scanf(&quot;%f %f&quot;,&base,&height); triangleArea(); } void rectangleArea() { float a; a = length * breadth; printf(“Area of Rectangle : %.2f&quot;, a); } void triangleArea() { float a; a = 0.5 * base * height ; printf(“Area of Triangle : %.2f&quot;, a); } extern float length, breadth ; /* extern base , height ; --- error */ float rectanglePerimeter() { float p; p = 2 *(length + breadth); return ( p ); } File1.c File2.c External Global Variables Scope : Visible to all functions across all files in the project. Lifetime : exists till the end of the program. Static Global Variables Scope : Visible to all functions with in the file only. Lifetime : exists till the end of the program. Storage Classes – Scope & Lifetime
  • 8. #include<stdio.h> void showSquares(int n) { if(n == 0) return; else showSquares(n-1); printf(“%d “, (n*n)); } int main() { showSquares(5); } A function calling itself is Recursion Output : 1 4 9 16 25 showSquares(5 ) showSquares(4) showSquares(3) showSquares(2) showSquares(1) addition of function calls to call-stack call-stack execution of function calls in reverse Preprocessor Directives #define - Define a macro substitution #undef - Undefines a macro #ifdef - Test for a macro definition #ifndef - Tests whether a macro is not defined #include - Specifies the files to be included #if - Test a compile-time condition #else - Specifies alternatives when #if test fails #elif - Provides alternative test facility #endif - Specifies the end of #if #pragma - Specifies certain instructions #error - Stops compilation when an error occurs # - Stringizing operator ## - Token-pasting operator Preprocessor is a program that processes the source code before it passes through the compiler. main()