Presentation is loading. Please wait.

Presentation is loading. Please wait.

Formatted and Unformatted Input/Output Functions

Similar presentations


Presentation on theme: "Formatted and Unformatted Input/Output Functions"— Presentation transcript:

1 Formatted and Unformatted Input/Output Functions

2 Outline Unformatted Input/Output functions getchar() putchar() getch()
gets() puts()

3 Unformatted Functions
C has three types of I/O functions: Character I/O String I/O File I/O

4 getchar() This function reads a character-type data from standard input. It reads one character at a time till the user presses the enter key. Example: char c; c = getchar(); Syntax Variable-name = getchar();

5 #include<stdio.h> void main() { char c; printf(“enter a character”); c=getchar(); printf(“c = %c ”,c); } Enter a character k c = k

6 putchar() This function prints one character on the screen at a time which is read by standard input. Example: char c= ‘c’; putchar (c); Syntax putchar( variable name);

7 #include<stdio.h> void main() { char ch; printf(“enter a character: ”); scanf(“%c”, ch); putchar(ch); } enter a character: r r

8 getch() & getche() These functions read any alphanumeric character from the standard input device The character entered is not displayed by the getch() function until enter is pressed The getche() accepts and displays the character. The getch() accepts but does not display the character. Syntax getche();

9 #include<stdio.h> void main() { printf(“Enter two alphabets:”); getche(); getch(); }
Enter two alphabets a

10 putch() This function prints any alphanumeric character taken by the standard input device Example: #include<stdio.h> void main() { char ch; printf(“Press any key to continue”); ch = getch(); printf(“ you pressed:”); putch(ch); } Press any key to continue You pressed : e

11 gets() String I/O This function is used for accepting any string until enter key is pressed (string will be covered later) Syntax char str[length of string in number]; gets(str);

12 #include<stdio.h> void main() { char ch[30]; printf(“Enter the string:”); gets(ch); printf(“Entered string: %s”, ch); } Enter the string: Use of data! Entered string: Use of data!

13 puts() This function prints the string or character array. It is opposite to gets() Syntax char str[length of string in number]; gets(str); puts(str);

14 #include<stdio.h> void main() { char ch[30]; printf(“Enter the string:”); gets(ch); puts(“Entered string:”); puts(ch); } Enter the string: puts is in use Entered string: puts is in use

15 Math Library functions

16 Outline Math library functions

17 Functions in Mathematics
“A relationship between two variables, typically x and y, is called a function, if there is a rule that assigns to each value of x one and only one value of y.” So, for example, if we have a function f(x) = x + 1 then we know that f(-2.5) = -2.5 + 1 -1.5 f(-2) -2 -1 f(-1) f(0) +1 f(+1) +2 f(+2) +3 f(+2.5) +2.5 +3.5

18 Functions in Mathematics
Likewise, if we have a function a(y) = | y | then we know that a(-2.5) = | -2.5 +2.5 a(-2) -2 +2 a(-1) -1 +1 a(0) a(+1) a(+2) a(+2.5)

19 Function Argument f(x) = x + 1 a(y) = | y |
We refer to the thing inside the parentheses immediately after the name of the function as the argument (also known as the parameter) of the function. In the examples above: the argument of the function named f is x; the argument of the function named a is y.

20 Absolute Value Function in C
The abs function calculates the absolute value of its argument. It’s the C analogue of the mathematical function a(y) = | y | (the absolute value function) a= abs(y);

21 Absolute Value Function in C
fabs(-2.5) returns 2.5 abs(-2) 2 abs(-1) 1 abs(0) abs(1) abs(2) fabs(2.5)

22 Absolute Value Function in C #3
We say “abs of -2 evaluates to 2” or “abs of -2 returns 2.” Note that the function named abs calculates the absolute value of an int argument, and fabs calculates the absolute value of a float argument.

23 Function Call in Programming
In programming, the use of a function in an expression is referred to as a call. We say that the statement printf("%d\n", abs(-2)); invokes or calls the function abs; the statement passes an argument of -2 to the function; the function abs returns a value of 2.

24 Math Function vs Programming Function
An important distinction between a function in mathematics and a function in programming: a function in mathematics is simply a definition (“this name means that expression”), while a function in programming is an action (“this name means execute that sequence of statements”).

25 C Standard Library Every implementation of C comes with a standard library of predefined functions. Note that, in programming, a library is a collection of functions. The functions that are common to all versions of C are known as the C Standard Library.

26 Math Library Functions
perform common mathematical calculations #include <math.h> Format for calling maths functions functionName( argument ); If multiple arguments, use comma-separated list Example: printf( "%.2f", sqrt( ) ); Calls function sqrt, which returns the square root of its argument All math functions return data type double Arguments may be constants, variables, or expressions

27 Math Library Functions
Explain functions(SQRT, FABS,CEIL, FLOOR,POW) in detail

28 Math Library Functions: pow()
The power function, pow(x,y), calculates xy; that is, the value of pow(x,y) = xy. pow(2,3)= 2³ = 8.0 and pow(2.5,3) = The function pow is of the type double or that the function pow returns a value of the type double. x and y are called the parameters (or arguments) of the function pow. Function pow has two parameters.

29 sqrt() The square root function, sqrt(x),
calculates the non-negative square root of x for x >= 0.0 sqrt(2.25) is 1.5 sqrt(25) is 5.0 The function sqrt is of the type double and has only one parameter.

30 fabs() fabs calculates the absolute value of a float argument.
fabs(2.25) is 2.25 fabs(-25.0) is 25.0 The function fabs is of the type double and has only one parameter.

31 floor() The floor function, floor, calculates the largest whole number that is not greater than x. floor(48.79) is 48.0 floor(48.03) is 48.0 floor(47.79) is 47.0 The function floor is of the type double and has only one parameter.

32 ceil() The ceil function, ceil, calculates the smallest whole number that is not less than x. ceil(48.79) is 49 ceil(48.03) is 49 ceil(47.79) is 48 The function ceil is of the type double and has only one parameter.

33

34 Programming: Argument Type
Programming has concepts that are analogous to the mathematical domain and range: argument type and return type. For a given function in C, the argument type – which corresponds to the domain in mathematics – is the data type that C expects for an argument of that function. For example: the argument type of abs is int; the argument type of fabs is float.

35 Argument Type Mismatch
An argument type mismatch is when you pass an argument of a particular data type to a function that expects a different data type. Some implementations of C WON’T check for you whether the data type of the argument you pass is correct. If you pass the wrong data type, you can get a bogus answer. This problem is more likely to come up when you pass a float where the function expects an int. In the reverse case, typically C simply promotes the int to a float.

36 #include <stdio.h>
#include <math.h> int main () { /* main */ const float pi = ; printf("2.0 = %f\n", 2.0); printf("pi = %f\n", pi); printf("cos(pi) = %f\n", cos(pi)); printf("sin(pi) = %f\n", sin(pi)); printf("sqrt(2.0) = %f\n", sqrt(2.0)); printf("sqrt(2.0) / 2 = %f\n", sqrt(2.0) / 2); } /* main */

37 Function Use Example 2.0 = 2.000000 pi = 3.141593 cos(pi) = -1.000000
sin(pi) = sqrt(2.0) = sqrt(2.0) / 2 =

38 Recursive function

39 Recursion Vs Iteration
Examples of recursion Finding factorial of a number Finding Fibonacci series up to nth term Recursion Vs Iteration

40 Recursion Recursive functions Functions that call themselves
Can only solve a base case Divide a problem up into What it can do What it cannot do What it cannot do resembles original problem The function launches a new copy of itself (recursion step) to solve what it cannot do Eventually base case gets solved Gets plugged in, works its way up and solves whole problem

41 Recursion example (factorial)
Factorial of a number in mathematics 5! = 5 * 4 * 3 * 2 * 1 Another method we have studied is For 5!, we write 5! = 5 * 4! Then for 4!, 4! = 4 * 3! Then for 3!, 3! = 3 * 2! Then for 2!, 2! = 2 * 1! Then for 1!, 1! = 1 * 0! And if its comes to 0, 0!=1 Solve base case (1! = 0! = 1) 1 2 6 24 120 Values returned

42 Recursion example (factorial)

43 Recursion example (factorial code)
This function calculates factorial of first 10 numbers

44 Recursion example (factorial code)
This function calculates factorial of first 10 numbers 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 10! = output

45 Recursion example (fibonacci)
What is Fibonacci series: …?? 0, 1, 1, 2, 3, 5, 8... Each number is the sum of the previous two Can be solved recursively: fib( n ) = fib( n - 1 ) + fib( n – 2 ) Code for the fibonacci function long fibonacci( long n ) { if (n == 0 || n == 1) // base case return n; else return fibonacci( n - 1)+fibonacci( n – 2 ); }

46 Recursion example (fibonacci)
Set of recursive calls to fibonacci() function f( 3 ) f( 1 ) f( 2 ) f( 0 ) return 1 return 0 return +

47 Recursion example (fibonacci code)
This function calculates fibonacci number of any given position

48 Recursion example (fibonacci code)
This function calculates fibonacci number of any given position Enter an integer: 0 Fibonacci( 0 ) = 0 or Enter an integer: 1 Fibonacci( 1 ) = 1 Enter an integer: 20 Fibonacci( 20 ) = 6765 output

49 Recursion vs. Iteration
Repetition Iteration: explicit loop(for,while) Recursion: repeated function calls Termination Iteration: loop condition fails Recursion: base case reached Both can have infinite loops Balance Choice between performance (iteration) and good software engineering (recursion)

50 Rules for recursive function
In recursion, it is essential to call a function itself Only the user defined function can be involved in the recursion. Library function cannot be involved in recursion because their source code cannot be viewed A recursive function can be invoked by itself or by other function. To stop recursive function, it is necessary to base recursion on some condition, and proper termination statement such as exit() or return The user defined function main() can be invoked recursively.


Download ppt "Formatted and Unformatted Input/Output Functions"

Similar presentations


Ads by Google