Unit-5 Ip Long
Unit-5 Ip Long
Essay Questions-7M
1. What are various types of parameters passing techniques in ‘C’ programming?
Explain with an example program.
Ans.
In C programming, there are two primary
types of parameter passing techniques:
1. Call by Value:In Call by Value, the actual value of the argument is passed to the function.
Any changes made to the argument within the function do not affect the original variable.
Example Program:
#include <stdio.h>
void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
printf("Inside swap function: x = %d, y = %d\n", x, y);
}
int main()
{
int a = 10;
int b = 20;
printf("Before swap:a = %d,b =%d\n", a,b);
swap(a, b);
printf("After swap: a = %d, b = %d\n",a, b);
return 0;
}
Output:
Before swap: a = 10, b = 20
Inside swap function: x = 20, y = 10
After swap: a = 10, b = 20
As you can see, the swap function did not change the original values of `a` and `b`.
2. Call by Reference:In Call by Reference, the address of the argument is passed to the
function. Any changes made to the argument within the function affect the original variable.
Example Program:
#include <stdio.h>
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
printf("Inside swap function: x = %d, y =
%d\n", *x, *y);
}
int main()
{
int a = 10,b = 20;
printf("Before swap: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
Output:
Before swap: a = 10, b = 20
Inside swap function: x = 20, y = 10
After swap: a = 20, b = 10
As you can see, the swap function changed the original values of `a` and `b`.
Call by Value:In Call by Value, the actual value of the argument is passed to the function.
Any changes made to the argument within the function do not affect the original variable.
PROGRAM:
#include <stdio.h>
void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
printf("Inside swap function: x = %d, y = %d\n", x, y);
}
int main()
{
int a = 10;
int b = 20;
printf("Before swap: a = %d, b = %d\n", a, b);
swap(a, b);
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
Output:
Before swap: a = 10, b = 20
Inside swap function: x = 20, y = 10
After swap: a = 10, b = 20
As you can see, the swap function did not change the original values of `a` and `b`.
Call by Reference:In Call by Reference, the address of the argument is passed to the
function. Any changes made to the argument within the function affect the original variable.
PROGRAM:
#include <stdio.h>
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
printf("Inside swap function: x = %d, y = %d\n", *x, *y);
}
int main()
{
int a = 10;
int b = 20;
printf("Before swap: a = %d, b = %d\n", a, b);
swap(&a, &b);
(ii) fscanf(): fscanf() is a function that reads formatted input from a file. It is similar to
`scanf()`, but instead of reading from the standard input (usually the keyboard), it reads from
a file.
Syntax: fscanf(file_pointer, format_string, argument1, argument2, ...);
Example:
FILE *file = fopen("example.txt", "r");
int x;
char name[20];
fscanf(file, "%d %s", &x, name);
printf("x = %d, name = %s\n", x, name);
fclose(file);
This will read an integer and a string from the file "example.txt" and print them to the
console.
(iv) fgets(): fgets() is a function that reads a line of text from a file. It is similar to `gets()`, but
instead of reading from the standard input (usually the keyboard), it reads from a file.
Syntax: fgets(string, size, file_pointer);
Example:
FILE *file = fopen("example.txt", "r");
char line[100];
fgets(line, 100, file);
printf("%s", line);
fclose(file);
This will read a line of text from the file "example.txt" and print it to the console.
(v) fputs(): fputs() is a function that writes a string to a file. It is similar to `puts()`, but instead
of writing to the standard output (usually the screen), it writes to a file.
Syntax: fputs(string, file_pointer);
Example:
FILE *file = fopen("example.txt", "w");
char *str = "Hello, World!";
fputs(str, file);
fclose(file);
This will write the string "Hello, World!" to the file "example.txt".
6. What is recursion? Write a program to find the factorial of a given number using
recursion.
Ans
Recursion is a programming technique where a function calls itself repeatedly until it reaches
a base case that stops the recursion. In other words, a function solves a problem by
breaking it down into smaller sub-problems of the same type, which are then solved by the
same function, until the solution to the original problem is found.
1. Base case: A recursive function has a base case that is a trivial case that can be solved
directly.
2. Recursive call: The function calls itself with a smaller input or a modified version of the
original input.
3. Solution: The function combines the solutions to the sub-problems to solve the original
problem.
PROGRAM:
// a program to find the factorial of a given number using recursion//
#include <stdio.h>
int factorial(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
else
{
return n * factorial(n-1);
}
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0)
{
printf("Error! Factorial is not defined for negative numbers.\n");
}
else
{
printf("Factorial of %d = %d\n", num, factorial(num));
}
return 0;
}
Output:
Enter a number: 5
Factorial of 5 = 120
Input Functions:
1. scanf(): Reads input from the standard input (usually the keyboard).
2. fscanf(): Reads input from a file.
3. getchar(): Reads a single character from the standard input.
4. gets(): Reads a string from the standard input.
Output Functions:
1. printf(): Writes output to the standard output (usually the screen).
2. fprintf(): Writes output to a file.
3. putchar(): Writes a single character to the standard output.
4. puts(): Writes a string to the standard output.
PROGRAM:
// program that demonstrates passing pointers to functions//
#include <stdio.h>
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x = 10;
int y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
Output:
Before swap: x = 10, y = 20
After swap: x = 20, y = 10
In this example, the `swap` function takes two integer pointers as parameters. The
function swaps the values of the integers by dereferencing the pointers. In the `main`
function, we pass the addresses of `x` and `y` to the `swap` function using the unary `&`
operator. After the swap, the values of `x` and `y` are printed to the console.
Types of streams:
1. Input streams: Streams that provide input data, such as reading from a file.
2. Output streams: Streams that accept output data, such as writing to the console.
3. Binary streams: Streams that handle binary data, such as image files.
4. Text streams: Streams that handle text data, such as reading from a text file.
SYNTAX:return-type function-name(parameter-list);
EXAMPLE :int add(int, int);
This prototype declares a function named `add` that takes two `int` parameters and
returns an `int` value.
(ii) Function Call : A function call is a statement that invokes a function, passing arguments
to it. When a function is called, the control is transferred to the function, and the function's
code is executed.
SYNTAX:function-name(argument-list);
EXAMPLE:
result = add(10, 20);
This statement calls the `add` function, passing `10` and `20` as arguments, and assigns the
returned value to the variable `result`.
(iii) Returning from Function:Returning from a function refers to the process of exiting the
function and returning control to the calling function. When a function returns, it can also
pass a value back to the calling function.
Output Functions:
printf(): Prints formatted output to the standard output (usually the screen).
Syntax: printf(format_string, argument1, argument2, ...);
Example: printf("Hello, %s!\n", "World");
Input Functions:
scanf(): Reads formatted input from the standard input (usually the keyboard).
Syntax: scanf(format_string, argument1, argument2, ...);
Example: scanf("%d %s", &number, name);
Format Specifiers:
Format specifiers are used to specify the type of data to be read or written. Here are some
common format specifiers:
- %c: Character
- %d: Decimal integer
- %f: Floating-point number
- %s: String
- %x: Hexadecimal integer
- %o: Octal integer
- %p: Pointer
- %e: Exponential notation
- %g: General format
Modifiers:
Modifiers are used to modify the behavior of format specifiers. Here are some common
modifiers:
- h: Short integer
- l: Long integer
- L: Long double
- ll: Long long integer
- u: Unsigned integer
- +: Always include a sign
- 0: Pad with zeros
- -: Left-justify
- (space): Leave a space before the value
Flags:
Flags are used to control the behavior of format specifiers. Here are some common flags:
- #: Alternate form
- 0: Pad with zeros
- -: Left-justify
- (space): Leave a space before the value
- +: Always include a sign
These are the basic formatting input/output functions supported by C. By using these
functions, you can control the input/output operations and format the data according to your
needs.
12. What do you mean by streams? What are the applications of it? Differentiate text
and binary streams.
Ans.
In computer science, a stream refers to a sequence of data elements made
available over time, often in a continuous flow. Streams can be thought of as a conduit for
data to flow through, allowing programs to process the data in real-time.
Applications of streams:
1. File input/output: Streams are used to read and write files, allowing programs to process
file data in a continuous flow.
2. Network communication: Streams are used to transmit data over networks, enabling real-
time communication between devices.
3. Audio and video processing: Streams are used to process audio and video data in real-
time, enabling applications like video streaming and audio editing.
4. Data compression: Streams are used to compress data in real-time, reducing the amount
of data that needs to be stored or transmitted.
Text Streams:
Binary Streams:
In summary, text streams are designed for human-readable, formatted data, while
binary streams are designed for machine-readable, raw, unformatted data.